develooper Front page | perl.perl5.porters | Postings from April 2003

possible bug in 'our' and compile-time directives with Getopt::Long and use constant

Thread Next
From:
Scott R. Godin
Date:
April 18, 2003 23:37
Subject:
possible bug in 'our' and compile-time directives with Getopt::Long and use constant
Message ID:
1701965.kNOg3yv3VV@pcp02404936pcs.univde01.de.comcast.net
whooooeee this one was fun figuring out. 

12:40am {37} pcp02404936pcs:/home/webdragon>$ cat bugtest2.pl
#!/usr/bin/perl 
 
require 5.006;
use warnings; 
use strict;

use Getopt::Long;
Getopt::Long::Configure ("bundling"); #allow bundled switches ala -rf = -r 
-f
use File::Spec; 

our($VERSION, %opts) = (1.3);
# use vars qw/%opts/; #oops, this is deprecated

BEGIN {
        GetOptions(

                'version' => \$opts{'v'},
                'file|f:s' => sub { $opts{'f'} = $_[1] ? $_[1] : "" },

        ) or die $!;
}

# had to move this outside the GetOptions due to the begin block.
if ($opts{'v'})
{
        print "This is $0 Version: $VERSION\n";
        exit 0;
}
        
# sets to 0 if you don't specify -f switch to have the output 
# redirected to a file.

#print exists($opts{'f'}) ? 1 : 0;
use constant USE_SAVEFILE => exists($opts{'f'}) ? 1 : 0; 
#print exists($opts{'f'}) ? 1 : 0;

my $savefile  = File::Spec->catfile( 
    File::Spec->curdir(), 
    ($opts{'f'} ? $opts{'f'} : 'ModuleCheck.txt') 
);

print( USE_SAVEFILE, ", ", $savefile, "\n" );

__END__ 

12:44am {44} pcp02404936pcs:/home/webdragon>$ ./bugtest2.pl --version
0, ./ModuleCheck.txt
12:44am {45} pcp02404936pcs:/home/webdragon>$ ./bugtest2.pl 
0, ./ModuleCheck.txt
12:45am {46} pcp02404936pcs:/home/webdragon>$ ./bugtest2.pl -f
1, ./ModuleCheck.txt
12:45am {47} pcp02404936pcs:/home/webdragon>$ ./bugtest2.pl -f module
1, ./ModuleCheck.txt

hrm

what'd I finally do (by accident after shuffling variables around and stuff 
and finally trying moving the 'our' declaration INSIDE the BEGIN {} block, 
to no avail, I moved the declaration of %opts back out but stuck it on a 
separate line (from line 11 to line 13) thusly: (otherwise the two programs 
are exactly the same) 

12:44am {39} pcp02404936pcs:/home/webdragon>$ grep -C5 'our %opts' 
bugtest1.pl
Getopt::Long::Configure ("bundling"); #allow bundled switches ala -rf = -r 
-f
use File::Spec; 

our $VERSION = 1.3;
# use vars qw/%opts/; #oops, this is deprecated
our %opts;

BEGIN {
        GetOptions(

                'version' => \$opts{'v'},


... and watch what happens: 
12:46am {40} pcp02404936pcs:/home/webdragon>$ ./bugtest1.pl --version
This is ./bugtest1.pl Version: 1.3
12:48am {41} pcp02404936pcs:/home/webdragon>$ ./bugtest1.pl 
0, ./ModuleCheck.txt
12:48am {42} pcp02404936pcs:/home/webdragon>$ ./bugtest1.pl -f
1, ./ModuleCheck.txt
12:48am {43} pcp02404936pcs:/home/webdragon>$ ./bugtest1.pl -f module
1, ./module


Am I just being stupid or obtuse about this, or is this a genuine bug ? Has 
it been caught already? Anyone got any hair tonic? yeeeeeeesh :)


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About