http://www.perlmonks.org?node_id=1014125


in reply to variable declaration question

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.

--MidLifeXis

Replies are listed 'Best First'.
Re^2: variable declaration question
by tobyink (Canon) on Jan 18, 2013 at 21:19 UTC

    I updated. It was an afterthought as it didn't seem relevant to the question. (The question included semicolons after the declaration, so the variable is not being initialized.)

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^2: variable declaration question
by 7stud (Deacon) on Jan 18, 2013 at 22:08 UTC
    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.

      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...

        Quite right. ++

        --MidLifeXis