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

Hey All, Im a beginner in PERL and I';ve been instructed to use the follwing: use strict; to safeguard against any possible erroroneus uses of contructs,uniinitialized vars etc. So far so good. However, now, I want to use command line options, sample is provided below:
# perl -s testCLO.pl -useTR use strict; no strict "vars"; if ($useTR) { # do TR processing. print "useTR=$useTR\n"; }
Problem is that if use "use strict;", then I get the error: "Variable "$useTR" is not imported at testCLO.pl line 5." Possible solutions: Use
use strict; no strict "vars";
INSTEAD, BUt i want to comply with the norms of using "use strict;", is there anything else that I can do to get about this problem? Thanks so much.

Replies are listed 'Best First'.
Re: use strict; and command line options
by etcshadow (Priest) on Feb 11, 2004 at 05:17 UTC
    Dude, these guys who responded above obviously didn't understand your question, or were just too quick to answer without checking.

    Anwyay, the solution is actually quite simple, you just need to add use vars qw($useTR);. Here's some sample code and example run:

    #!/usr/bin/perl use strict; use vars qw($a $b); print "a: $a\n"; print "b: $b\n";
    and sample run:
    [me@host]$ perl -s tmp.pl -a a: 1 b: [me@host]$
    OK, though... all that being said I'd still recomend using Getopt::Long instead of just the -s switch, if for no other reason than that the user has to supply "-s" before your script when they run it (yes, folks, before you jump on me... I tested putting -s in the shebang line and it didn't work... which, frankly, shocked the heck out of me... maybe it's a bug in 5.005_03, or maybe it's a "feature").
    ------------ :Wq Not an editor command: Wq
      Erg... I spoke to soon about the -s switch not being honored on the shebang line... it's a little bit odder than that. It is honored if the script is invoked by the shebang, but not if it is invoked by a direct call to the interpretter (and, thus, that the shebang line just gets parsed by the interpretter):
      [me@host]$ cat t2.pl #!/usr/bin/perl -s use strict; use vars qw($a $b); print "a: $a\n"; print "b: $b\n"; [me@host]$ perl t2.pl -a a: b: [me@host]$ ./t2.pl -a a: 1 b: [me@host]$
      This is really weird, as most switches should not behave differently if the script is invoked by the shebang rather than by calling the interpretter explicitly... Really frickin' odd.
      ------------ :Wq Not an editor command: Wq
      -s on the shebang line had a bug and was fixed (I think) in 5.8.0.

      Update: the bug only applied when foo.pl was run as perl foo.pl, not ./foo.pl.

        Nice. I never use it myself (I always use Getopt::Long for switches)... I only looked it up to answer this guy's question.
        ------------ :Wq Not an editor command: Wq
Re: use strict; and command line options
by Roger (Parson) on Feb 11, 2004 at 02:55 UTC
    You have to declare your variables before using them.
    use strict; my $useTR = "SOMETHING"; # or: our $useTR = "SOMETHING"; # or: use vars qw/ $useTR /; if ($useTR) { print "useTR = $useTR\n"; }

      Well, my objectve here is to provide MY own switches for the PERL program and as I understand it:If an option is specified , the scalar variable whose name is the same as the option is automatically set to 1 before program execution begins. By providing the variable with a value I am unable to read the command line option that may have been provided byu the user. Note this is an option, not an argument. Thanks.
        That's not how you use command line options.

        There are many ways to retrieve command-line parameters, below are two that I use often:
        # method 1 use strict; die "you must provide command-line parameters" if ! @ARGV; print "command-line option: $_\n" for @ARGV; # method 2 use strict; use Getopt::Long; GetOptions ( 'useTR' => \{ my $useTR = 0 }, ); unless ( defined $useTR ) { die "Usage: $0 -useTR"; } # Then you use the $useTR variable onwards

        The @ARGV array holds all command line options. You may want to process it using your favorite flavor of loop.

        Want to support the EFF and FSF by buying cool stuff? Click here.