dicty has asked for the wisdom of the Perl Monks concerning the following question:
What's the difference between "my $var;" and "my ($var);" in variable declaration?
Thanks,
Dicty
Re: variable declaration question
by Tommy (Chaplain) on Jan 18, 2013 at 21:49 UTC
|
What's the difference between "my $var;" and "my ($var);" in variable declaration?
The difference is that the first version enforces scalar context on the assignment, and the second imposes list context. (Mark Jason Dominus explains this better.)
These may seem like insignificant differences, but can lead to nasty bugs in code if, say, you have a manager who decides to make it a "coding practice" to put parens (around) (all) (variable) (declarations) (everywhere) in the name of "consistency". *see footnote1
Parens() aren't a matter of style. For the most part, parenthesis serve three purposes in perl: they impose list context, they denote/force sub/method invocation, and assist in forcing precedence in order of operations on an expressions with multiple invocants.
For single variable assignment, you usually want scalar context, which is why it should _NEVER_ be a coding practice to use one form or the other because it isn't an issue of style -- it's an issue of behavior.
Furthermore, when you use my($foo) = $obj->method() as a matter of practice, then it completely breaks upstream interfaces using the wantarray() builtin to produce polymorphic results for you. And if you're a real jerk, you'll blame the designer of the upstream interface for your own lack of understanding. *facepalm*
This wouldn't be such a big deal to me if it hadn't been such a bad experience to learn it the hard way. I'm pretty sure that the perils of misunderstanding scalar vs. list context are a big reason why it is one of the first things many Perl books and instructors cover nowadays, and if they don't, they #$%@! well should.
1: my($sad_experience) = (it, has, happened, to, me, ;_;)
Tommy
A mistake can be valuable or costly, depending on how faithfully you pursue correction
| [reply] [d/l] [select] |
Re: variable declaration question
by MidLifeXis (Monsignor) on Jan 18, 2013 at 21:04 UTC
|
To add to what tobyink said, "none" - in context.
my $foo = @some_array_value and my ($foo) = @some_array_value will give two very different results.
print(my $foo = (1, 2, 3), "\n");
print(my ($foo) = (1, 2, 3), "\n");
__END__
3
1
Update: Alright, either QuickDraw McGraw updated his node, or I skipped over the second and following line when I read it. :-) We stated basically the same thing.
| [reply] [d/l] [select] |
|
| [reply] |
|
print(my $foo = (1, 2, 3), "\n");
print(my ($foo) = (1, 2, 3), "\n");
__END__
3
1
Ah, I see. The line: print(my $foo = (1, 2, 3), "\n");
is a convenient way of assigning the last element of an array to a variable.
| [reply] [d/l] [select] |
|
Try:
print(my $foo = ('a', 'b', 'c'), "\n");
vs:
my @foo = ('a', 'b', 'c'); my $bar = @foo; print $bar, "\n";
I like using letters vs numbers to avoid confusion...
| [reply] [d/l] [select] |
|
Re: variable declaration question
by tobyink (Canon) on Jan 18, 2013 at 21:00 UTC
|
None.
If you're declaring and assigning multiple variables at the same time, the version with the parentheses is more useful...
my ($one, $two, $three) = 1 .. 3;
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
| [reply] [d/l] |
Re: variable declaration question
by BillKSmith (Monsignor) on Jan 18, 2013 at 21:09 UTC
|
my $string = 'aaa1234bbb';
my ($number) = $string =~ /(\d+)/;
| [reply] [d/l] |
Re: variable declaration question
by blue_cowdawg (Monsignor) on Jan 18, 2013 at 21:34 UTC
|
What's the difference between "my $var;" and "my ($var);" in variable declaration?
Strictly speaking, the way you've presented the above not much except you fingers are slightly more tired. That said consider the following code:
sub marine {
my ($fore,$mid,$aft) = @_;
}
In the above example I have an array on the LHS and an array on the RHS. This is equivalent to:
sub marine {
my $fore = $_[0];
my $mid = $_[1];
my $aft = $_[2];
}
When coding parens, just like in written English, are used to group things together.
Clear as mud?
Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
|