Restrictions on constant
List constants are not currently inlined the way scalar constants are. And it is not possible to have a subroutine or keyword with the same name as a constant. This is probably a Good Thing.
You cannot declare more than one named constant at a time as a list:
use constant FOO => 4, BAR => 5; # WRONG
That defines a constant named FOO whose return list is (4, "BAR", 5). You need this instead:
use constant FOO => 4 use constant BAR => 5;
or even:
use constant {
FOO => 4,
BAR => 5,
};You can get yourself into trouble if you use a constant in a
context that automatically quotes bare names. (This is true for any
subroutine call, not just constants.) For example, you can’t say
$hash{CONSTANT} because CONSTANT
will be interpreted as a string. Use $hash{CONSTANT()} or $hash{+CONSTANT} to prevent the quoting mechanism from
kicking in. Similarly, since the => operator quotes its left operand if that
operand is a bare name, you must say CONSTANT() => "value" instead of
CONSTANT=>
"value" .
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access