On Tue, Apr 20, 2010 at 2:23 PM, Marc Perry <marcperrys...@gmail.com> wrote:
> As if $var is being declared in a list context; what, if anything, do I get
> by including parentheses when declaring a single variable?

I'm new to this myself, but it appears to alter the context of the
statement. Take the following:

    #!/usr/bin/env perl

    use strict;
    use warnings;

    $, = $\ = "\n";

    my @a = ("element1", "element2", "element3");

    my $x = @a;
    my ( $y ) = @a;

    print $x, $y;

    # EOF

The output should be:

3
element1

In the first case, the context is scalar, which results in the element
count being assigned to $x instead of the element(s). In the second
case, the context is list context, I think, which results in the first
element of the array being assigned to the variable $y. I'm not clear
on how to properly explain this, but to me it appears to make a list
of lvalues (assignables) and then moves the data from corresponding
positions on the right side of the equals sign to the left. Again,
there's probably a much better (and more correct) way to explain that.
;)

In other words, using or not using parenthesis can be context
sensitive so you need to know what you're doing with it. If you're
just declaring a normal variable, as in your example where you assign
it 1, then it doesn't seem to technically matter which you use, but
you're probably preferred to NOT use parenthesis because it's
unnecessary and might even be confusing if you are (and if the context
of the right-side changes then the meaning will change).

???

`perldoc perldata' appears to cover context somewhat:

http://perldoc.perl.org/perldata.html#Context

-- 
Brandon McCaig <bamcc...@gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamcc...@castopulence.org>

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to