Rob Dixon wrote:
Moon, John wrote:
How can I use a "constant" as a hash key?

$ perl -e 'use constant CAT=>A;
$hash{CAT} = q{Bobby};
$hash{"CAT"} = q{Muffy};
$hash{'CAT'} = q{Fluffy};
$hash{qq{CAT}} = q{Tuffy};
print "$_ = $hash{$_}\n" foreach (keys %hash);'
CAT = Tuffy
$

Want...

A=Bobby

If you remember that constants are implemented as subroutines then all you have
to do is write the hash key as an expression that forces the subroutine to be
called. So

  use constant CAT => 'A';

is equivalent to

  sub CAT { 'A' }

It is actually equivalent to:

   sub CAT () { 'A' }

See the "Constant Functions" section of perlsub.pod

perldoc perlsub



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to