[EMAIL PROTECTED] wrote:
> use constant PART_NUMBER => 'P/N';
>
> print PART_NUMBER;
>
> The above prints, "P/N", as I would expect. Later in the script I
> want to access a hash value using the constant like this:
> my $part = $parts{ $key }{ PART_NUMBER }; <- this doesn't work, but
> this does:
> my $part = $parts{ $key }{ 'P/N' }; <- works
>
> Interesting enough if the constant is another package this would work:
> my $part = $parts{ $key }{ SOME_PACKAGE::PART_NUMBER };
>
> Any idea what I am doing wrong?
If Perl sees a 'bareword' (a string composed entirely of alphanumerics
and underscore) use as a hash key it will use it as a literal string.
This makes the code tidier when you are using a constant string value
for keys as there is no need to put it in quotes. In this case, however,
you are accessing
$parts{ $key }{ 'PART_NUMBER' }
which isn't what you want.
There is a fix though. What 'use constant' does is to define a
subroutine with the given name that returns the value you specify. You
can force that subroutine to be called by writing
$parts{ $key }{ PART_NUMBER() }
and you will get the desired result.
HTH,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/