laughingboy has asked for the wisdom of the Perl Monks concerning the following question:
I've just started defining constants with "use constant", and I'm seeing some, er, non-intuitive behavior. :-)
Here is a code snippet and its output:
I fixed the problem by using "," instead of "=>" when ititializing the hash, but I don't understand why the code doesn't work as written. Since the constant definitions are in a BEGIN block, shouldn't their values be substituted throughout the source before the hash is initialized?use strict; BEGIN { use constant ROTATE => 1; use constant HOLD => 2; use constant FLASH => 3; use constant NUM_MODES => 3; # etc... } my %modestring = ( ROTATE => "a", HOLD => "b", FLASH => "c", # etc... ); my $mode = int(rand NUM_MODES) + 1; print "random mode string is ", $modestring{$mode} || "empty", "\n"; + print "string for 1 is ", $modestring{1} || "empty", "\n"; print "string for ROTATE is ", $modestring{ROTATE} || "empty", "\n"; + random mode string is empty string for 1 is empty string for ROTATE is a
laughingboy
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Behavior of compile-time constants?
by Abigail-II (Bishop) on Jun 14, 2002 at 17:58 UTC | |
Re: Behavior of compile-time constants?
by Chmrr (Vicar) on Jun 14, 2002 at 18:00 UTC | |
Re: Behavior of compile-time constants?
by perrin (Chancellor) on Jun 14, 2002 at 17:59 UTC | |
by particle (Vicar) on Jun 14, 2002 at 18:09 UTC | |
by Abigail-II (Bishop) on Jun 14, 2002 at 18:13 UTC | |
by particle (Vicar) on Jun 14, 2002 at 18:33 UTC | |
by chromatic (Archbishop) on Jun 14, 2002 at 19:59 UTC | |
by perrin (Chancellor) on Jun 14, 2002 at 18:15 UTC | |
by Abigail-II (Bishop) on Jun 14, 2002 at 18:19 UTC | |
by perrin (Chancellor) on Jun 14, 2002 at 18:24 UTC | |
|
Back to
Seekers of Perl Wisdom