This is closely related to the concept of the default input operator
What are you talking about???
- $_ is not an operator, at least no more so than $x.
- Some functions default to using $_ as an argument, but that is unrelated to this thread.
- $_ has nothing to with input.
- How can an input operator have anything to do with this?
And the rest of the post is no better. So many errors!
sub a{ return 1, 2, 3 } a();
This evaluates what a() returns, in scalar context. $_ gets the last
value returned by a(). In this case, $_ is 3, or True if evaluated in
Boolean context. If you tested $x = a(), you'd get the same True
result, with $x equal to 3.
- That code doesn't provide enough information to determine in which context a() is evaluated. If anything, void context is implied.
- Nothing is assigned to $_.
- If a() is evaluated in scalar context, then it makes no sense to talk about the last value it returns as it only returns one.
sub a{ return 1, 2, 0 } a();
$_ gets the last value returned by a(), as before. In this case, $_ is
0 in scalar context, or False in Boolean context!
- Nothing is assigned to $_.
- The same value is returned by the comma operator in both "boolean context" and scalar context.
@a = (undef, undef); @a;
This array is evaluated in scalar context because we haven't done
anything to force list context. An array, evaluated in scalar context,
is always the number of elements it contains.
- That code doesn't provide enough information to determine in which context a() is evaluated. If anything, void context is implied.
- There is no default context.
sub a{ return 1, 2, 0 } ($x,$y) = a();
We're still evaluating what a() returns, but here we've
forced list context. That means that $_ is assigned the
number of elements in the list returned, not the elements. It
doesn't matter what a() returns -- if it returns one or more
elements, $_ will be set to non-zero, or True in Boolean
context.
- List context isn't being forced, if there is such a thing.
- Nothing is assigned to $_, so most of that is just nonsense.
- The same value is returned by the list assignment operator in both "boolean context" and scalar context.
sub a{ return 1, 2, 0 } @arr = a();
Exactly the same as above.
- List context isn't being forced, if there is such a thing.
- Nothing is assigned to $_, so most of that is just nonsense.
- The same value is returned by the list assignment operator in both "boolean context" and scalar context.
(undef, undef);
An array is a container for a list. This is NOT an array,
and we haven't done anything to force list context, so we
have a list of scalars evaluated in scalar context. $_ is
set to the last element, which is undef -- or False in
Boolean context.
- There is no default context.
- That code doesn't provide enough information to determine in which context a() is evaluated. If anything, void context is implied.
- Nothing is assigned to $_.
- The same value is returned by the comma operator in both "boolean context" and scalar context.
sub a { return undef, undef } ($x, $y) = a(); if(not $x && not $y) { print "False\n" }
I think this is closer to what you're looking for. Whenever you don't
specifically lay out the condition you are testing, you are probably
testing $_.
No, it's not what he wants.
- It doesn't loop.
- The condition of the "if" statement relies on incorrect assumptions about precedence.
- A truth test is two levels removed from optiomal. (A defined test would be better, but testing whether the iterator returned anything at all would be best.)
I hope this helps.
Pure harm. Good thing the formatting makes it unreadable.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.