Re: perl -s and use strict
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 29 Mar 2007 04:33:42 -0700
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
.
- References:
- Re: perl -s and use strict
- From: Jeff Pang
- Re: perl -s and use strict
- Prev by Date: Re: perl -s and use strict
- Next by Date: Re: perl -s and use strict
- Previous by thread: Re: perl -s and use strict
- Next by thread: XML parsing REST responses
- Index(es):
Relevant Pages
|