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

howdy, y'all...

was working with some of the great tutorials here at the monestary and in doing so, begin playing with the -s switch...

while it is true that Getopt::Std or Getopt::Long are more flexible (and don't seem to have the same hangups with strict), i'm still interested in finding out whether the -s switch can work with strict...

below is posted a simple piece of code, just for the purposes of testing... if i was feeling compelled to use the -s switch, i found the following worked, meaning produced the expected output:

#!/usr/local/bin/perl -s use strict; my ($var) = "This is the line that should be changed."; my (@old) = split(/ /,$var); no strict; if ($z){ @old = map { tr/aeiou/AEIOU/; $_;} @old; #imagine many value +s here } else { @old = "\nBlah-Blah\n"; } use strict; #imagine more code here print "@old\n";

now if i run the script as myprog.pl -zit will give the expected result, and without the switch, the else result...
without the no strict; use strict wrap, it tells me i need to predefine $z... but if i predefine it, the -s switch fails...

my question: assuming i have to use the -s switch, and that my code will be more that 10 lines long and i want to use strict,
is there another way to use the -s switch and strict together?

Thanks in advance...

magnus

Replies are listed 'Best First'.
Re: -s and strict
by davorg (Chancellor) on Feb 28, 2001 at 17:53 UTC

    The $z variable that gets set is a package variable (actually $main::z). When you talk about predefining $z I'm guessing that you are talking about using my $z; This declares a new lexical variable called $z which is different to the package variable - hence the errors you're seeing.

    To get round it, you need to predeclare $z as a package variable:

    use vars qw($z);

    or always refer to $z using its fully qualified name - $main::tt (actually just $::tt will do).

    --
    <http://www.dave.org.uk>

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