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

Hey guys , do you see a problem in this code :
my $ret = GetOptions( "u|updated:s"); my $update = $opt_u || die "Usage: $0 -u update_ID\n";
when I use strict , it gives me :
Global symbol "$opt_u" requires explicit package name at c:/Perl/MyScr +ipts/updlnt.pl line 27.
but without strict , it works fine .. thanks for help
Edit: Changed the title to be more search-friendly (myocom)

Replies are listed 'Best First'.
Re: strict
by dws (Chancellor) on Jul 24, 2002 at 22:27 UTC
    You're using GetOpt.pl, which is deprecated. From the header:
    # This library is no longer being maintained, and is included for back +ward # compatibility with Perl 4 programs which may require it.

    I think you'll be happier using Getopt::Std, which is part of the core distribution in 5.6. Getopt::Std provides and API for loading options into a hash, which can be lexically scoped using my.

Re: strict
by dimmesdale (Friar) on Jul 24, 2002 at 22:07 UTC
    you have to say my $opt_u, or the compiler will spit an error -- or you could say (assuming main package) $MAIN::opt_u and be gone with it.

    This is because use strict requires you're variables to be declared -- or, as I think on it, you could say use vars qw/$opt_u ... others here.../; (hey, TMTOWTDI :)

    May I suggest, however, that (I assume you're using a GetOpt:: module here) you don't let the GetOpt module assign $opt_<whatever>, but instead pass it a hash reference. This way you can say $opts->{<whatever>} and be done with it.

      I agree that you're probably better off just getting your opts out of the hash, but to clear up your initial comment, I'm pretty sure the (official/newest) way to declare it would be...

      our $opt_u;

      This makes it global and is necessary since you want to be accessing the same variable that Getopt is using.

      -Bird

      Update: Just read the comment by dws below. I was thinking of Getopt::Std. I have no idea how GetOpt works.

Re: strict
by simon.proctor (Vicar) on Jul 24, 2002 at 22:53 UTC
    Please take this as constructive criticism. You should really learn how to read the documentation that come with Perl and with the modules that you use.

    You'll get more out of Perl but more importantly there will be times when you'll need an answer quickly and you can't come here to ask.

    Update:

    You may also want to look at the tutorials section on this site. Heres the tutorial on strict which covers your question :). Use strict and warnings
Re: strict
by fuzzyping (Chaplain) on Jul 24, 2002 at 22:02 UTC
    Because you haven't declared $opt_u (at least not in the code example you've provided). Do you understand what the strict pragma does?

    -fp
Re: Error when using strict pragma
by sauoq (Abbot) on Jul 25, 2002 at 01:48 UTC

    You should probably be specifying destination variables for your options. Otherwise, you will need to predeclare your $opt_x variables with our or use vars. The man page for Getopt::Long is clear on this point:

    Default destinations

    When no destination is specified for an option, GetOptions will store the resultant value in a global variable named "opt_"XXX, where XXX is the primary name of this option. When a progam executes under "use strict" (recommended), these variables must be pre-declared with our() or "use vars".

    It is always a good idea to read the documentation for the modules you choose to use.

    -sauoq
    "My two cents aren't worth a dime.";