Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method invocation does not supply scalar context... seems strange

Tags:

perl

This behavior isn't Math::BigInt specific, but the following code breaks on the last line.

use strict;
use warnings;
use Math::BigInt;

my $a = Math::BigInt->bone;
my $b = Math::BigInt->bone;

print ($a+$b)->bfac;

This code, however, works fine:

use strict;
use warnings;
use Math::BigInt;

my $a = Math::BigInt->bone;
my $b = Math::BigInt->bone;

print scalar($a+$b)->bfac;

My question is this... why isn't scalar context imposed automatically on the left argument of "->"? AFAIK, "->" only works on scalars and (exceptionally) on typeglobs.

like image 442
Gregory Nisbet Avatar asked Sep 24 '14 06:09

Gregory Nisbet


2 Answers

You need one more set of parens,

print (($a+$b)->bfac);

as your code is interpreted as,

(print ($a+$b))->bfac;

and warnings also gave you print (...) interpreted as function ..

like image 78
mpapec Avatar answered Oct 13 '22 22:10

mpapec


Need a + so it's not interpreted as arguments to print.

print +($a+$b)->bfac;
like image 35
Miller Avatar answered Oct 13 '22 21:10

Miller