Re: perl -s and use strict



On Mar 29, 2:35 am, sarthak.patn...@xxxxxxxxxxx (Sarthak Patnaik)
wrote:
Hello,

I have a small script:

#!/usr/bin/perl -ws

use strict;

print $k;

As I am using "use strict;", it is giving the following message:

Variable "$k" is not imported at ./test.pl line 3.

Global symbol "$k" requires explicit package name at ./test.pl line 3.

Execution of ./test.pl aborted due to compilation errors.

Is there any workaround for this, so that I can use strict and -s together.

There's no work around needed, because there's no issue. Both -s and
strict are working correctly... you're just making false assumptions.

-s created the global variable $main::k. That variable exists, and
you can use it, no problem.

'use strict' prevents you from referring to global variables by their
"short" names (ie, without "fully qualifying" them). So you can't say
"$k" when you mean "$main::k".

The 'our' keyword is how you tell Perl that you want to refer to a
variable by its short name for the duration of this lexical scope,
even though strict is in use.

#!/usr/bin/perl -s
use strict;
use warnings;
print "K: $main::k\n";
our $k; #now, I can call that variable just $k
print "K: $k\n";
__END__

Hope this helps,
Paul Lalli

.



Relevant Pages

  • Re: help: having trouble evaluating expressions with EVAL
    ... on "use strict" I get a bunch of new errors. ... Global symbol "$exp" requires explicit package name at ...
    (comp.lang.perl.misc)
  • Re: why does this script not work?
    ... #use strict; ... Global symbol "$email" requires explicit package name ... Putting aside the error message because of the global symbol, ...
    (comp.lang.perl.misc)
  • Re: help: having trouble evaluating expressions with EVAL
    ... on "use strict" I get a bunch of new errors. ... my ($str, $exp, $answer); ... Global symbol "$exp" requires explicit package name at ...
    (comp.lang.perl.misc)
  • Re: Global symbol "$var1" requires explicit
    ... # Since you are use strict you must declare all variables before using # ... Global symbol "$var1" requires explicit package name at sample.pl line 3. ... Global symbol "$var2" requires explicit package name at sample.pl line 4. ...
    (perl.beginners)
  • Re: use / circular dependency issue
    ... > I have a relationship between MyA and MyB. ... Global symbol "$foo" requires explicit package name at MyB.pm line 18. ... If you're using 'strict' in MyB.pm, then you can't declare the variables as ...
    (comp.lang.perl.misc)