>> > use strict;
>> >
>> > use vars qw( $a $b $c # this is how you declare variables
>> > $myvar ); # for use with "use strict"
>>
>> I've always seen it done this way:
>>
>> #[code]
>> my ($a, $b, $c, $myvar);
>> #[/code]
>>
>> ...so that the variables are part of the current package.
>> I kind of like the way you've done it, though.
>> Why would you choose one of these over the other?
>
>Both approaches will work equally well for most general applications.
>You start to see differences when you get into more specialized
>applications. For example, if you're writing a Perl module, and you
>want to export variables using Exporter.pm, you can't use "my". "My"
>variables don't exist outside the file in which they're defined, so
>obviously you can't refer to them from any file that's going to be
>using your module.
>
>The other example that comes to mind is if you ever get write CGI
>scripts that run under mod_perl's CGI engine (dang, brain cramp, what's
>that mod_perl thing called again?). Anyway, there is a really esoteric
>interaction that happens with "my" variables that causes them to hold
>on to their initial value unexpectedly. You see it sometimes in scripts
>that have a user log in--the second person to log in (and everybody
>thereafter) gets logged in as the first person who logged in, because a
>"my" variable held on to its first value. Look up "closure" in the Perl
>reference if you want more info.
>
>Anybody else want to chime in on this?
yes .. you should almost ALWAYS use lexical variables (those declared with
the 'my' keyword) in preference to package variables (those declared with
'our' or 'use vars' or explicit package names)
the majority of variables used in your program should be declared using the
'my' keyword
my $x = 5;
my @x = (1,2,3);
my %x = ( one => 1, two => 2, three => 3);
my($a,$b,$c) = @x;
# ..etc.
package variables should be thought of as special variables that can be
viewed and accessed from outside the package .. you should only use them
when you specifically want that behaviour
lexical variables have priority over package variables when it comes to
using them bare (which you can only do without 'strict' - and this is
actually a good reason to use strict .. example follows)
#!perl -l
use vars qw/$x/;
$x = 5; # package variable
print $x;
__END__
fairly simple - prints '5' .. but let's say at some point later - you add a
lexical variable to your code
#!perl -l
use vars qw/$x/;
$x = 5; # package variable
# some other code
my $x = 'foo';
print $x;
__END__
this will print out 'foo' because lexical variables have priority .. so
always use lexical variables
their scope is either within the block that they're declared in - or from
where they're declared to the end of the file if it's not in a block ..
example
#!perl -l
my $x = 'foo';
print $x;
{ # start of a block - could be a sub or while loop - anything
my $x = 'bar';
print $x;
}
print $x;
__END__
the code prints because the scope of the internal $x is limited by the block
.. and the first declaration resumes after the block has ended
foo
bar
foo
--
jason king
In Spearfish, South Dakota, if three or more Indians are walking down
the street together, they can be considered a war party and fired
upon. - http://dumblaws.com/