rbc has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

What am I doing wrong here?

#!/usr/bin/perl -w use strict; use Getopt::Std; my $width = 500; my $height = 500; getopts( 'w:h:' ); if( $opt_w ) { $width = $opt_w ; } if( $opt_h ) { $height = $opt_h ; } print "$height:$width\n";

I get errors like so ...

Global symbol "$opt_w" requires explicit package name at ./adjust.pl l +ine 14.


... and if I change my code to like so ...
#!/usr/bin/perl -w use strict; use Getopt::Std; my $width = 500; my $height = 500; my $opt_w; my $opt_h; getopts( 'w:h:' ); if( $opt_w ) { $width = $opt_w ; } if( $opt_h ) { $height = $opt_h ; } print "$width:$height\n";

... and run like so ...

$ ./my.pl -w 1000 -h 9

... I get this output ...

500:500

... Looks like my.pl ignored the switches?


Please help.
Thanks

--
Its like a dog that can sing and dance.
It's remarkable because it can do it.
Not that it can do it well.

Replies are listed 'Best First'.
Re: use strict and use Getopts
by runrig (Abbot) on Jan 10, 2002 at 02:27 UTC
    opt_w and opt_h must be package variables for getopts() to work, so declare them with 'use vars' or 'our', not 'my'. Or alternatively, use a hash (which may be declared with my) and pass a reference to it to getopts().
Re: use strict and use Getopts
by lestrrat (Deacon) on Jan 10, 2002 at 02:29 UTC

    From perldoc Getopt::Std

    Note that, if your code is running under the recommended use strict 'vars' pragma, it may be helpful to declare these package variables via use vars perhaps something like this:

    use vars qw/ $opt_foo $opt_bar /;

    For those of you who don't like additional variables being created, getopt() and getopts() will also accept a hash reference as an optional second argument. Hash keys will be x (where x is the switch name) with key values the value of the argument or 1 if no argument is specified.

    I personally like using hashes for this kind of thing.

Re: use strict and use Getopts
by Rich36 (Chaplain) on Jan 10, 2002 at 02:28 UTC
    Declare your variables using the use vars pragma. This will allow you to use those variables for use with Getopt::Std without having to declare them with my. Check the use vars link for more info.
    #!/usr/bin/perl -w use strict; use Getopt::Std; my $width = 500; my $height = 500; use vars qw/$opt_h $opt_w/; getopts( 'w:h:' ); if( $opt_w ) { $width = $opt_w ; } if( $opt_h ) { $height = $opt_h ; } print "$width:$height\n";

    Rich36
    There's more than one way to screw it up...

Re: use strict and use Getopts
by lachoy (Parson) on Jan 10, 2002 at 02:30 UTC

    You're running into scoping issues. The easiest thing I've found is to pass a hashref as the second argument. The args get stuck in there. For instance:

    #!/usr/bin/perl use strict; use Getopt::Std; { my %opts = (); getopts( 'w:h:', \%opts ); print "w: $opts{w}; h: $opts{h}\n"; }

    Running like:

    $ perl opts.pl -w wassup -h hey

    Produces:

    w: wassup; h: hey

    Chris
    M-x auto-bs-mode

      Thanks everyone!