in reply to declaration of variables
I'll respond to that question first. The best way depends on what you're trying to accomplish, and the 'scope' you're trying to achieve.
my declares variables to exist within the innermost block. In this sense, I mean block as a literal { } pair, or as a subroutine, or as a file, or even as an eval. What this means is that when you declare a variable with my, it lives within the block in which it was declared, and falls out of scope when the block itself passes from scope. It probably gets a little more complicated than that sometimes (if you make it), but generally just count on variables declared with 'my' as existing within the block in which they were declared. But that probably wasn't really your question.
You were asking what is the difference between some of the commonly seen usages of 'my'. I'll try to respond accurately. I'm sure if I miss something we'll hear about it. ;)
my $var; # Declares a single variable.
The following example assigns the variable a value:
my $var = "Hello world!\n"; my $var = 10; my $var = 0; my $var = ""; my $var = \@some_array; my $var = \%some_hash; my $var = \$some_scalar; my $var = \&some_function;
In the preceeding examples, you're assigning $var some value. It is not always necessary to assign $var the value of "" (the null string). Unlike C, variables in Perl are already empty when you create them. But empty in this case means undefined. In the context of "What is false" and "What is true", undef, "" (null string), and 0, are all "false". But if you are counting on a null string, assign it as in your examples. Redundancy isn't always bad either; in some cases you'll want to assign something, even if it's nothing, to add clarity.
What if you want to declare multiple variables? my binds more tightly than the comma, so you cannot just say, my $this, $that;. You must use paranthesis:
my ( $this, $that );
The parenthesis are also serving the dual function of (1) Specifying order of precedence so that the comma binds more closely than the my. And (2) the parenthesis have also created a list. That means you can legitimately do this:
my ( $this, $that ) = ( "the", "other" );
It is not wrong to say,
my ($var);
or
my ($var) = "This";
It's just not *necessary* in this situation to use the parens. ...but it doesn't hurt anything.
Some additional things you can do:
my ( $this, $that ) = "The other";
Now $this has been assigned a value, and $that has not. ...and you can do this:
my @array = ("This", "That", "and", "The", "Other"); my %hash = ( "This" => "That", "The" => "Other" ); my $var = ("This", "That"); # $var now contains "That". my ( $this, $that ) = \( $the, $other ); # The reference # constructer is distributive and # applies to both items inside the rvalue # list.
And there are plenty of other things you can do. Just remember, if there is more than one variable on the left, use parenthesis so that 'my' binds to both. The rest you'll find behaves pretty much how you expect it to when you need it.
Dave
"If I had my life to do over again, I'd be a plumber." -- Albert Einstein
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: declaration of variables
by bradcathey (Prior) on Sep 02, 2003 at 14:55 UTC | |
by jeffa (Bishop) on Sep 02, 2003 at 15:32 UTC | |
by bradcathey (Prior) on Sep 02, 2003 at 15:47 UTC | |
by davido (Cardinal) on Sep 02, 2003 at 17:01 UTC | |
by tilly (Archbishop) on Sep 03, 2003 at 04:56 UTC |