Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force EXPR instead of GLOB dereference?

Tags:

perl

use strict;
use warnings;

sub XX { 30 };

my $rnd =  3;
my $z =  -XX * $rnd;

Give error: Can't use string ("3") as a symbol ref while "strict refs" in use

This does not help:

my $z =  -XX * ($rnd);

I get next error:

Scalar found where operator expected at game4.pl line 7, near "* ($rnd"
    (Missing operator before $rnd?)
syntax error at game4.pl line 7, near "* ($rnd"
Execution of game4.pl aborted due to compilation errors.

How to force EXPR instead of GLOB dereference?

like image 612
Eugen Konkov Avatar asked Feb 01 '26 03:02

Eugen Konkov


1 Answers

A few options.

Explicitly tell Perl that you're not passing parameters to XX.

my $z =  -XX() * $rnd;

Use the old-style calling conventions for subroutines (I really don't recommend this one).

my $z =  -&XX * $rnd;

Define the subroutine as not taking parameters.

sub XX() { 30 };

But the best solution is to use the built-in constant pragma.

use constant XX => 30;
like image 126
Dave Cross Avatar answered Feb 02 '26 17:02

Dave Cross



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!