gregory-nisbet has asked for the wisdom of the Perl Monks concerning the following question:

so I'm a bit confused why
$a = Math::BigInt->bone; $b = Math::BigInt->bone; ($a+$b)->bfac;
generates an error but replacing the last line with

 scalar($a+$b)->bfac

works fine. AFAIK, only scalars and (exceptionally) typeglobs support the method invocation / member access syntax using "->", so why isn't scalar context automatically applied here?

edit:

The original code snippet was "print ($a+$b)->bfac" and the "looks like function"-rule was applied.

Replies are listed 'Best First'.
Re: Scalar Context Not Applied During Method Call
by choroba (Cardinal) on Sep 24, 2014 at 08:51 UTC
    The code without print doesn't throw any error for me. With print, the usual trick is to add a +:
    print +($a + $b)->bfac;

    It is a hint to the parser that the left parenthesis doesn't start the argument list for print.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Scalar Context Not Applied During Method Call
by LanX (Saint) on Sep 24, 2014 at 09:29 UTC
    > I can't figure out how to delete the question

    You could use <strike> tags around the obsolete parts.

    Or <hr> and a emphasized "please ignore from here on"

    ATM its not obvious what still needs to be answered.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: Scalar Context Not Applied During Method Call
by tobyink (Canon) on Sep 24, 2014 at 10:48 UTC

    "I can't figure out how to delete the question though."

    You cannot delete a question. Even if you don't care about getting an answer any more, future visitors to the site might, so it is right that the question stays open for additional comments.