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

Greeting,
I just trying this code,
use strict; use warnings; use constant HEAD => qw/ABC DEF GHI/; use constant ABC=> qw/wet ear rtf/; print $_ foreach(HEAD);
Can anyone please tell me, why it's not printing 'wet ear rtf'in place of 'ABC' and Is there any way to read the value of that constant array 'ABC' in same way ??
Thanks,
-kulls

Replies are listed 'Best First'.
Re: Reading constant
by Joost (Canon) on Dec 09, 2006 at 12:55 UTC
    here are the docs, pay special attention to the section named "List constants". Constants are always scalars so you need to provide an array reference as the value. But qw// returns a list and not an array reference. Update: erm nope. Let me check this.

    update2 Ah. I see. Your problem is that qw// makes a list of strings, and the string "ABC" is not the same as the value of the constant named "ABC".

    If you want to use a constant as an element in a list, don't use qw, use (), and make sure the constant is defined before you use it

    use constant ABC => qw/wet ear rtf/; use constant HEAD => (ABC,"DEF","GHI"); print $_ for HEAD;
      Hi,
      Thanks for your reply.I understood the problem.
      - kulls
Re: Reading constant
by MaxKlokan (Monk) on Dec 09, 2006 at 13:04 UTC
    If you define HEAD as a constant, then you cannot change it anymore because it is, indeed, a constant. "ABC" is a string and it is its first element.
    If you first define the constant ABC and then the constant HEAD, then you can do the following:
    use strict; use warnings; use constant ABC=> qw/wet ear rtf/; use constant HEAD => ABC,qw/DEF GHI/; print $_ foreach(HEAD);
    Is this what you wanted?