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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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"; };

In reply to Re^5: Mini-Tutorial: Scalar vs List Assignment Operator by dec
in thread Mini-Tutorial: Scalar vs List Assignment Operator by ikegami

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-03-19 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found