Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Mini-Tutorial: Scalar vs List Assignment Operator

by ikegami (Patriarch)
on Aug 21, 2009 at 03:15 UTC ( [id://790231]=note: print w/replies, xml ) Need Help??


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

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 }

Replies are listed 'Best First'.
Re^3: Mini-Tutorial: Scalar vs List Assignment Operator
by dec (Beadle) on Aug 21, 2009 at 12:35 UTC
    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.

        Further: approaching the question of "what is the difference between list and scalar context, and when are they applied?" via an experiential/experimental approach with wantarray is eminently practical. To wit, here's a simple test script I wrote when I wanted to clarify the matter for myself. This most definitely constitutes Another Way To Do It:
        #!/usr/bin/perl -w sub polymorphic { my $context=wantarray(); if ($context) { print "list context\n"; return @_; } elsif (defined($context)) { print "scalar context\n"; return join ":", @_; } else { warn "value of polymorphic() used in void context. Values were (\n +"; # so no point in returning anything, but do print print join ",", @_; print ")\n"; } } sub show_args { print "got ", scalar(@_), " args:\n"; map {print " $_\n" } @_; } show_args("a", polymorphic("b","c","d")); my $string=polymorphic(1,2,3); my @array=polymorphic(4,5,6); show_args($string,@array); # make warning 1 polymorphic("unused"); # following is tricky because we don't know context until tricky is # actually called. sub tricky { polymorphic(@_); } my $string2=tricky(qw(a b c d)); my @array2=tricky(qw(s t u v)); show_args($string2,@array2); # make warning 2 tricky(qw(fee fi fo fum)); # what happens if we use return keyword explicitly? sub tricky_2 { return polymorphic(@_); } my $string3=tricky_2(qw(a e i o u)); my @array3=tricky_2(qw(w x y z)); show_args($string3,@array3); # (turns out we can use return or not; same result--no warning) # (but this should generate one) tricky_2(5,6,7,8); sub tricky_3 { my $first=shift; my $second=shift; my @remaining=@_; print "$first, $second, ("; print join ",", @remaining; print ")\n"; };
        It's not

        Question: what is the point in writing a tutorial explaining the difference between list and scalar contexts?

        Answer: so that people who don't know the difference can gain some enlightenment.

        Question: Besides poring over man pages or searching the Internet, what other way is there to gain an understanding of when a list context is implied and when a scalar context is implied

        Answer: write a sub that calls wantarray and divine the context directly

        Have I made my point?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-03-19 02:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found