hexcoder has asked for the wisdom of the Perl Monks concerning the following question:
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
(run with Strawberry Perl 5.26.2) yields: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";
It seems literals cannot simply be replaced by defined constants, which i consider rather unfortunate.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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: replacing literals with constants
by Corion (Patriarch) on May 14, 2022 at 09:15 UTC | |
by hexcoder (Curate) on May 14, 2022 at 09:20 UTC | |
by kcott (Archbishop) on May 14, 2022 at 13:17 UTC | |
by LanX (Saint) on May 14, 2022 at 22:35 UTC | |
Re: replacing literals with constants
by tybalt89 (Monsignor) on May 14, 2022 at 09:13 UTC | |
Re: replacing literals with constants (empty prototype needed)
by LanX (Saint) on May 14, 2022 at 09:25 UTC | |
Re: replacing literals with constants
by atcroft (Abbot) on May 14, 2022 at 16:10 UTC | |
by kcott (Archbishop) on May 14, 2022 at 18:08 UTC |