Ambiguous use of -CONSTANT resolved as -&CONSTANT()

perl

Solution

First, some background. Let's look at the following for a second:

$_ = -foo;

`-foo` is a string literal[1].

$ perl -Mstrict -wE'say -foo;'
-foo

Except if a sub named `foo` has been declared.

$ perl -Mstrict -wE'sub foo { 123 } say -foo;'
Ambiguous use of -foo resolved as -&foo() at -e line 1.
-123

Now back to your question. The warning is wrong. A TERM (`7`) cannot be followed by another TERM, so `-` can't be the start of a string literal or a unary minus operator. It must be the subtraction operator, so there is no ambiguity.

This warning is still issued in 5.20.0[2]. I have filed a bug report.

Look ma! No quotes!

system(grep => ( -R, $pat, $qfn ));

Well, 5.20.0 isn't out yet, but we're in a code freeze running up to its release. This won't be fixed in 5.20.0.

Problem

I'm trying to declare magic numbers as constants in my Perl scripts, as described in perlsub. However, I get warnings: ``` $ cat foo.perl use warnings ; use strict ; sub CONSTANT() { 5 } print 7-CONSTANT,"\n" ; $ perl foo.perl Ambiguous use of -CONSTANT resolved as -&CONSTANT() at foo.perl line 3. 2 $ ``` The warning goes away if I insert a space between the minus and the `CONSTANT`. It makes the expressions more airy than I'd like, but it works. I'm curious, though: What is the ambiguity it's warning me about? I don't know any other way it could be parsed. (Perl 5.10.1 from Debian "squeeze").

Original source