in reply to Re: set context for method's return values
in thread set context for method's return values

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?
  • Comment on Re^2: set context for method's return values

Replies are listed 'Best First'.
Re^3: set context for method's return values
by Fletch (Bishop) on Jan 25, 2022 at 15:40 UTC

    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.

Re^3: set context for method's return values
by ikegami (Patriarch) on Jan 25, 2022 at 17:23 UTC
Re^3: set context for method's return values
by BillKSmith (Monsignor) on Jan 26, 2022 at 14:16 UTC
    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
Re^3: set context for method's return values (unary plus)
by LanX (Saint) on Jan 25, 2022 at 16:36 UTC
    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 :)