What caught my attention was the "top of the file" - this is why I was referring to doing a file. A little example will serve more than a thousands words, anyway.

First script /tmp/prova1.pl uses wantarray at the top level of a file:

#!/usr/bin/perl wantarray ? print("wantarray!\n") : defined wantarray ? print("wantarray defined but false\n") : print("wantarray undefined!\n"); wantarray ? qw( ciao a tutti ) : defined wantarray && "howdy!";
When called directly, it yelds:
wantarray undefined!
but here I agree with you, we cannot rely on it. This is why I built the second script /tmp/prova2.pl:
#!/usr/bin/perl use strict; use warnings; $" = ", "; print "First: list context\n"; my @a = do '/tmp/prova1.pl'; print "array \@a = (@a)\n\n"; print "Second: void context\n"; do '/tmp/prova1.pl'; print "\nThird: scalar context\n"; my $c = do '/tmp/prova1.pl'; print "scalar \$c = $c\n";
which yelds:
First: list context wantarray! array @a = (ciao, a, tutti) Second: void context wantarray undefined! Third: scalar context wantarray defined but false scalar $c = howdy!
So, the wantarray is used "at the top of the file", but it seems that wantarray is working fine. Can we rely on it? If yes, I think that the docs should be made a little clearer.

Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Don't fool yourself.

In reply to Re^2: wantarray documentation in 5.8.7 by polettix
in thread wantarray documentation in 5.8.7 by polettix

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.