Re: perl -s and use strict



On Mar 29, 2:39 am, p...@xxxxxxxxxxxxx (Jeff Pang) wrote:
#!/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.

This is because you didn't declare that variable $k before using it.

No, it's because strict 'vars' prevents using a global variable
without fully qualifying it. The variable exists. There is no need
to declare it.

You may do:

my $k; # declare $k as a lexical variable

That $k is 100% different from the $main::k that the -s switch set on
the command line. If the OP were to use 'my' in this case and then
print the value of $k, it would be an empty string, regardless of the
value of the -s switch.

our $k; # declare $k as a package variable

The statement is correct, the comment is not. 'our' does not
*declare* anything. It simply lets you use an existing package
variable without fully qualifying it. -s already set the value of
$main::k. Saying `our $k` simply lets you refer to $main::k as just
$k.

Paul Lalli

.



Relevant Pages

  • Re: Pragmas use strict and use warnings
    ... disable strict 'vars' on a variable-by-variable case using 'our' (the ... you to declare your variables". ... strict 'vars' as a way to force oneself to declare variables. ... if the resulting error message was something along the ...
    (perl.beginners)
  • Re: Pragmas use strict and use warnings
    ... snip> Whats the exact purpose of use strict ... The strict pragma has three effects: ... disable strict 'vars' on a variable-by-variable case using 'our' (the ... you to declare your variables". ...
    (perl.beginners)
  • Re: Pragmas use strict and use warnings
    ... global variables must be fully qualified. ... or to disable strict 'vars' on a variable-by-variable ... assert "use strict forces you to declare your variables". ... if the resulting error message was something along the ...
    (perl.beginners)
  • Re: Over 100 Microsoft MVPs Have Signed Online Petition - Give UsBack VB!!
    ... >> Welllll, you know, even in dotNet, where you can declare vars inline ... I declare them all at the top. ... What tools do you use in writing code? ... paper), and then actually execute it, is damned handy. ...
    (borland.public.delphi.non-technical)
  • Re: Syntax error
    ... > You should always declare all variables as lexically scoped in the ... With regards to your advice, I am not familiar with the term ... I do "use strict" in all of my normal perl scripts. ... added both the "use strict" and "use warnings" line, ...
    (comp.lang.perl.misc)