in reply to Re^3: Mini-Tutorial: Scalar vs List Assignment Operator
in thread Mini-Tutorial: Scalar vs List Assignment Operator
Besides flagging that wantarray could be useful here
It's not. You don't pick an assignment operator then design the function it's operand will call. You design the function first, then you write the code that will assign the return value or the part of the return value that's desired (whether it uses wantarray or not).
# f() returns three elements my $first = f(); # XXX, unless f() also allows this my ($first) = f(); # OK, uses list assignment my $first = ( f() )[0]; # OK, extracts desired scalar first my (undef, $second) = f(); # OK, uses list assignment my $second = ( f() )[1]; # OK, extracts desired scalar first # g() returns a variable number of elements my $last = g(); # XXX, unless g() also allows this my $last = ( g() )[-1]; # OK, extracts desired scalar first my $count = @all = g(); # OK, list assignment in scalar cx my $count = () = g(); # OK, list assignment in scalar cx
Like I already said, deciding what to have a sub return is a lengthy topic entirely unrelated to the behaviour of the assignment operators.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^5: Mini-Tutorial: Scalar vs List Assignment Operator
by dec (Beadle) on Aug 22, 2009 at 01:35 UTC | |
Re^5: Mini-Tutorial: Scalar vs List Assignment Operator
by dec (Beadle) on Aug 22, 2009 at 01:23 UTC | |
by ikegami (Patriarch) on Aug 22, 2009 at 03:46 UTC |