Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

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


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

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.


Comment on Re^3: Mini-Tutorial: Scalar vs List Assignment Operator
Re^4: Mini-Tutorial: Scalar vs List Assignment Operator
by ikegami (Pope) on Aug 21, 2009 at 13:29 UTC

    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.

      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?

        huh?

        Are you saying I wrote a tutorial on contexts? I didn't, but someone else did over here.

        Are you saying you're writing a tutorial on contexts? All the power to you, but maybe should start a new thread (where people will actually see it) instead of placing it in a reply to some unrelated post.

      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"; };

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (13)
As of 2014-10-20 10:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (75 votes), past polls