[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: question about r8708 (Perl warning pragmas)

From: Ben Reser <ben_at_reser.org>
Date: 2004-03-04 19:44:18 CET

On Thu, Mar 04, 2004 at 02:41:38PM +0000, Colin Watson wrote:
> On Tue, Mar 02, 2004 at 11:57:49PM -0600, kfogel@collab.net wrote:
> > r8708 applies a patch like this to three Perl hook scripts:
> >
> > -# The warning switch is set here and not in the shebang line above
> > -# with /usr/bin/env because env will try to find the binary named
> > -# 'perl -w', which won't work.
> > -BEGIN
> > - {
> > - $^W = 1;
> > - }
> > -
> > +require 5.6.0; # minimum Perl version for "warnings" module
> > +use warnings;
> >
> > Some time ago, Ben Collins-Sussman and I looked at this and wondered
> > why the "require 5.6.0;" line wasn't controversial.
>
> For an entirely separate reason than the version requirement itself,
> 'require 5.6.0;' isn't a very good idea. Here's why:
>
> $ perl -MConfig -le 'print $Config{version}'
> 5.00503
> $ perl -e 'require 5.6.0;'
> Can't locate 5.60 in @INC (@INC contains: /usr/lib/perl5/5.005/i386-linux /usr/lib/perl5/5.005 /usr/local/lib/site_perl/i386-linux /usr/local/lib/site_perl /usr/lib/perl5 .) at -e line 1.
> $ perl -e 'require 5.006;'
> Perl 5.006 required--this is only version 5.00503, stopped at -e line 1.
>
> Versions older than 5.6.0 didn't understand the "5.6.0" syntax for
> versions, so you should use the "5.006" form instead. Both will fail,
> but the former gives a very confusing and misleading error message.

Even changing the versioning syntax to the old style will still not give
a nice version error:

$ perl5.00503 -e 'require 5.006_000; use warnings;'
Can't locate warnings.pm in @INC (@INC contains:
/usr/local/lib/perl5/5.00503/sun4-solaris /usr/local/lib/perl5/5.00503
/usr/local/lib/perl5/site_perl/5.005/sun4-solaris
/usr/local/lib/perl5/site_perl/5.005 .) at -e line 1.
BEGIN failed--compilation aborted at -e line 1.
Exit 2

It works. But the use gets run before it anyway. So the require is
pointless:

$ perl5.00503 -e 'require 5.006_000; print q(foo)'
Perl 5.006 required--this is only version 5.00503, stopped at -e line 1.
Exit 255

So the require line will never run unless they have 5.6.0 anyway. That
means at this point in time it's more of a documentation than an
effective requirement.

Wrapping it in a BEGIN block would avoid this:
$ perl5.00503 -e 'BEGIN { require 5.006_000;} use warnings;'
Perl 5.006 required--this is only version 5.00503, stopped at -e line 1.
BEGIN failed--compilation aborted at -e line 1.
Exit 255

Someone else suggested we wrap it in an eval block. We can do something
like this and then it'll work on all perl5 versions:
$ perl5.00503 -e 'BEGIN { if (eval "require 5.006_000;") { require
warnings; import warnings; print qq(warnings\n); } else { $^W = 1; print
qq(^W\n);}} $x = 1;'
^W
Name "main::x" used only once: possible typo at -e line 1.

$ perl5.6.0 -e 'BEGIN { if (eval "require 5.006_000;") { require
warnings; import warnings; print qq(warnings\n); } else { $^W = 1; print
qq(^W\n);}} $x = 1;'
warnings
Name "main::x" used only once: possible typo at -e line 1.

Using the better method on the newer perls and the old method on the
older ones. Before someone asks. I tried to use $] and $^V to handle
the version test. It wasn't very cooperative. I didn't "use warnings"
here because that creates its own BEGIN block which will end up getting
evaluated before the one I wrote.
 

-- 
Ben Reser <ben@reser.org>
http://ben.reser.org
"Conscience is the inner voice which warns us somebody may be looking."
- H.L. Mencken
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Received on Thu Mar 4 19:43:06 2004

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.