Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Mini-Tutorial: Scalar vs List Assignment Operator

by dec (Beadle)
on Aug 21, 2009 at 00:48 UTC ( #790216=note: print w/ replies, xml ) Need Help??


in reply to Mini-Tutorial: Scalar vs List Assignment Operator

You should maybe look up the wantarray documentation, since it seems it could be very relevant here. In case you don't know, it lets a function determine what context it has been called in (list, scalar or void). Handy when you want to be able to respond differently to different calling contexts ... or even just raise an error/warning when called with the wrong context, eg:

sub pedantic_syswrite { my $context=wantarray; if (defined ($context)) { if ($context) { # caller wants an array, which is (mostly) silly f +or write die "write doesn't return an array, silly!\n"; } else { # scalar context # no warning, because user should always check return value syswrite @_; } } else { # void context die "I'm not going to write if you don't check my return value\n"; } }
So, ok, maybe the code shouldn't die when called in a list context :-/ But it demonstrates how you might use it.


Comment on Re: Mini-Tutorial: Scalar vs List Assignment Operator
Download Code
Re^2: Mini-Tutorial: Scalar vs List Assignment Operator
by ikegami (Pope) on Aug 21, 2009 at 03:15 UTC

    I disagree with your strategy of preventing a function that returns a scalar from being called in list context. It limits options without adding benefits. (The other way around makes some sense.) But deciding what to have a sub return is a lengthy topic entirely unrelated to the behaviour of the assignment operators. Discussions specifically touching on the appropriateness of using wantarray have surfaced before, once not too long ago. Your thoughts would be better suited in that conversation or in a new conversation.

    Also note that our Tutorials section has a tutorial on context.

    By the way, syswrite(@_) doesn't work. Due to syswrite's prototype, the call is equivalent to syswrite(scalar(@_)) and results in a compilation error. You need something like:

    if (@_ == 2) { syswrite($_[0], $_[1] ) } elsif (@_ == 3) { syswrite($_[0], $_[1], $_[2] ) } elsif (@_ == 4) { syswrite($_[0], $_[1], $_[2], $_[3]) } else { die }
      On "preventing a function that returns a scalar from being called in list context".. yes, I agree. I did say that it probably wasn't a good idea. The idea was just to show how to use wantarray: ie, how to test the various return values.

      I had a suspicion as well when I wrote the code that the syswrite wasn't correct. Thanks for pointing it out.

      With all that was wrong with my post, don't I at least get some credit for suggesting that programmers should test the return value of syswrite instead of assuming it works? Besides flagging that wantarray could be useful here (if only so the programmer has a way to test directly whether a particular construct implies a list or array context) that's the other point I was trying to make.

        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://790216]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-12-21 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (101 votes), past polls