Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
because there is no such thing as a list in scalar context,

My take on it is that this is the exception that disproves that rule. Whilst others will disagree and probably decline to comment.

If you put that thing--two or more things separated by commas--that we're told is not a list, in a scalar context, at compile time, then the compiler knows the list is useless and can discard all but the last element. This is shown nicely by Deparse:

C:\test>perl -MO=Deparse -e"print scalar( 'fred', 'john', 'bill' )" print scalar('???', '???', 'bill'); -e syntax OK

And backed up by warnings:

C:\test>perl -we"print scalar( 'fred', 'john', 'bill' )" Useless use of a constant in void context at -e line 1. Useless use of a constant in void context at -e line 1. bill

But construct that same thing in a way that the context is not directly discernable at compile-time, and it has no choice but to construct a list er, leave the thing intact.

Again nicely shown by Deparse:

C:\test>perl -MO=Deparse -e"print scalar( sub{'fred', 'john', 'bill'}- +>() )" print scalar sub { 'fred', 'john', 'bill'; } ->(); -e syntax OK

And no warnings either:

C:\test>perl -we"print scalar( sub{'fred', 'john', 'bill'}->() )" bill

Just a list thing in a scalar context being reduce/resolved/resulted to the last element of that list thing.

what does \(LIST) become?

The only way left to describe it is that the thing (that would be a list in a list context), becomes the last element of that thing when it finds itself in a scalar context. :)


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^3: Why does foo() evaluate in array context in "${\foo()}"? by BrowserUk
in thread Why does foo() evaluate in array context in "${\foo()}"? by ELISHEVA

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 making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-03-29 08:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found