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

I must be just a goober, but I can't seem to figure out how to use the variables from a module while using use strict;

I have this snippet of code:

#!/usr/bin/perl -w use strict; use Getopt::Std; getopts('hd:t'); my ($num_days, $dait, $today); # local vars used later my ($opt_h, $opt_d); # vars from Getopt::Std # if I don't use my() here I get the following #Global symbol "$opt_h" requires explicit package name at ./checklogs. +pl line 12. #Global symbol "$opt_d" requires explicit package name at ./checklogs. +pl line 13. # I tried Getopt::Std::$opt_h and it didn't like it either. # if ( $opt_d ) { $num_days = $opt_d; } print ("num_days = $num_days\n");

$opt_d never gets initialized when I use my(); I'm assuming that it is creating the variable locally and not from the package. How do I get the variables from the package Getopt::Std into my program?

Any help would be greatly appreciated.
Thanx, L8on

Replies are listed 'Best First'.
(jeffa) Re: how do i use module vars while using use strict; ?
by jeffa (Bishop) on Apr 10, 2001 at 18:33 UTC
    Put this in right after use strict:
    use vars qw($opt_h $opt_d $opt_t);

    UDPDATE:
    jerones suggested me to point you to our and a great discussion on how it works: Why is 'our' good?.

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: how do i use module vars while using use strict; ?
by davorg (Chancellor) on Apr 10, 2001 at 18:41 UTC

    The variables that getopts sets are package variables, not lexical variables. You can't declare them with my, you have to use use vars or use their full names (e.g. $main::opt_h>).

    Personally, I like to use the version of getopts that takes a reference to a hash and puts the values in there.

    #!/usr/bin/perl -w use strict; use Getopt::Std; my %opt; getopts('hd:t', \%opt); my ($num_days, $dait, $today); # local vars used later if ( $opt{d} ) { $num_days = $opt{d}; } print ("num_days = $num_days\n");
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: how do i use module vars while using use strict; ?
by Caillte (Friar) on Apr 10, 2001 at 18:43 UTC

    jeffa is absolutely right. Here is why:

    hen you declare a variable without strict, ie by just using it, that variable is automatically considered to be global. As it is global it can be read by both the main script and Getopt::Std. However, strict bans the use of such variable because it is too easy to accidentally create a global variable where you wanted a local one. So, with strict, you use my to declare a variable that has scope only within the current block (ie between { and } ) or you can declare a variable to be global. Not kowing what version of perl you have the best way to advise you how to do that is to use vars, as jeffa suggested.

    $japh->{'Caillte'} = $me;

Re: how do i use module vars while using use strict; ?
by Rhandom (Curate) on Apr 10, 2001 at 19:35 UTC
    From the code shown in the question, it makes it appear that Getop::Std is setting package main variables? That seems a little bit magic to me. It would make more sense if they showed up at least with the package space of Getopt::Std, in which case they could be accessed via $Getopt::Std::opt_h and $Getopt::Std::opt_d. I don't know that I would trust a subroutine that messes with my package space in a context other than import.

    As