On Tue, Sep 22, 2009 at 11:13, Telemachus <telemac...@arpinum.org> wrote:
> On Tue Sep 22 2009 @ 10:56, Chas. Owens wrote:
>> On Tue, Sep 22, 2009 at 09:40, Bryan R Harris
>> <bryan_r_har...@raytheon.com> wrote:
>> > Can you explain how perl interprets this?  I would've incorrectly thought:
>> >
>> > 1)  "() = func_that_return_list_value" tries to assign the list to "()"
>> > which perl would complain about.
>> >
>> > 2)  Then assign the result to $size, which it wouldn't like either.
>> >
>> > Just when you think you're starting to understand perl...
>> snip
>>
>> The number of "catcher" elements in a list does not need to be equal
>> to the number of "thrown" items:
>>
>> my ($x, $y) = (1, 2, 3);
>>
>> List assignment behaves differently in list and scalar contexts.  In
>> list context, it returns the list that was successfully assigned to
>> variables.  In scalar context, it returns the number of items that
>> tried to be assigned.  So, in
>>
>> perl -le '$z = ($x, $y) = (qw/a b c/); print "$x, $y, $z"'
>>
>> $z is 3 (because there were three items in RHS of the assignment), $x
>> is "a", and $y is "b".  "c" is silently discarded.
>
> All of this makes sense, but I have to admit I find it odd that the
> following doesn't even produce a "void context" warning:
>
>    my @array = (1, 2, 3);
>    my $num = () = @array;
>    print $num, "\n";
snip

Ah, but it isn't a "Useless use of (%s) in void context".  Even if "my
$num =" weren't there it wouldn't be useless.  Examine this code:

#!/usr/bin/perl

use strict;
use warnings;

sub foo {
        my $want = wantarray;
        print defined $want ? $want ? "list" : "scalar" : "void", "\n";
}

foo;
scalar foo;
() = foo;


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

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