Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: set context for method's return values

by Discipulus (Canon)
on Jan 25, 2022 at 08:26 UTC ( [id://11140839]=note: print w/replies, xml ) Need Help??


in reply to set context for method's return values

Hello demoralizer,

you can also:  print +(split /,/, 'A,B,C,D')[2]

where the + sign avoid the warning print (...) interpreted as function

> return value as an array ..

Technically using () we are creating a list, not an array: we can access the Nth element but, for example, we cannot push to it.

L*

PS the phrase about parens was related to this usage, not their general use; see below..

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: set context for method's return values
by LanX (Saint) on Jan 25, 2022 at 10:20 UTC
    > Technically using () we are creating a list,

    Sorry that's not fully correct, ()[] resp. (EXPR)[SLICE] is "creating a list"

    (EXPR) alone affects only precedence, no list context is propagated.

    for completeness:

    • () is an empty list
    • (EXPR)= is a list assignment

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re^2: set context for method's return values
by demoralizer (Beadle) on Jan 25, 2022 at 15:25 UTC
    Every once in a while I see that plus sign you used to suppress warnings, but so far I haven't been able to find an explanation for it.
    Do you have any keywords for me to search for to dig a little deeper into this topic, please?

      Not exactly sure but "unary plus" might get you on the right track. Basically the plus doesn't change the value but tells the parser after it's seen print that the next parens it sees aren't for it ("I am altering the parse tree. Pray I don't alter it further." — Darth Syntaxus).

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

      The explanation of +( is in the first section of the documentation of print.
      Be careful not to follow the print keyword with a left parenthesis unless you want the corresponding right parenthesis to terminate the arguments to the print; put parentheses around all arguments (or interpose a +, but that doesn't look as good).
      Bill
      I have to admit that it confused me for a long time too.

      I thought it's akin to 0+ which enforces scalar context and numeric casting (also in other languages like JS).

      But it's basically a No-OP which separates a function from the parens, such that the parens are not interpreted as part of the function call.

      It has sufficiently low precedence and doesn't meddle with context:

      DB<9> say ("a".."c")[1] syntax error at (eval 18)[c:/Strawberry/perl/lib/perl5db.pl:738] line +2, near ")[" DB<10> say (("a".."c")[1]) # how it works b DB<11> say +("a".."c")[1] # avoid extra parens b DB<12> say +("a".."c")[1..2] # LIST uneffected bc DB<13>

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Thanks again for all the really interesting answers, it's a bit clearer now but still confusing. I'm sure a deeper understanding will come with time :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-26 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found