How to force EXPR instead of GLOB dereference?

Viewed 55
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?

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;
Related