http://qs1969.pair.com?node_id=11143892

hexcoder has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear monks,

in my latest project I wanted to replace hard coded literals with constants.
In one case there was a simple loop, which used an expression with a constant for the terminal range value.
Here i stumbled upon an unexpected behavior.
Can someone shed some light on what is going on?

Thanks a lot!

This test program

use strict; use warnings; sub ten { 10 } print "9 .. 10 - 1 without constant\n"; for my $i (9 .. 10 - 1) { print $i, "\n"; } print "-> expected\n\n"; print "9 .. 10 - 1 with constant()\n"; for my $i (9 .. ten() - 1) { print $i, "\n"; } print "-> expected\n\n"; print "9 .. 10 - 1 with constant\n"; for my $i (9 .. ten - 1) { print $i, "\n"; } print "-> unexpected\n\n"; print "9 .. 10 with constant\n"; for my $i (9 .. ten) { print $i, "\n"; } print "-> expected\n\n";
(run with Strawberry Perl 5.26.2) yields:
9 .. 10 - 1 without constant 9 -> expected 9 .. 10 - 1 with constant() 9 -> expected 9 .. 10 - 1 with constant 9 10 -> unexpected 9 .. 10 with constant 9 10 -> expected
It seems literals cannot simply be replaced by defined constants, which i consider rather unfortunate.