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