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

Hello monks! I come seeking wisdom... or at least proper syntax. I have a hash of arrays like so:
my %whatever = ( 'a' => [1,2,3,4], 'b' => [5,6,7,8], );
normally, I would extract the values in classic camel fashion:
my $extract = $whatever{'a'}[0]; #should = 1
but in this case I'm writing a rule action for the excellent module Parse::RecDescent, so its in another namespace, and must be fully qualified. I tried doing
my $extract = $::whatever{'a'}[0]; #should = 1
and
my $extract = $main::whatever{'a'}[0]; #should = 1
as well as various attempts to play with references, but all to no avail. RecDescent always complains that it must be fully qualified, or I get an undef returned. Anybody experienced this ? Or better yet, know the answer?
Thanks for listening...
Jim

Replies are listed 'Best First'.
Re: hashes of arrays in another name space
by TGI (Parson) on Jan 11, 2001 at 05:32 UTC

    merlyn has, as usual, sussed it. Here's how you create a global/package variable and avoid running afoul of strict.

    If you also use vars qw(%whatever) you can have a nice, global, package variable. Set %whatever to some value without using my. Then you should be able to access your variable with my $extract = $main::whatever{'a'}[0];

    Good luck!


    moo
Re: hashes of arrays in another name space
by merlyn (Sage) on Jan 11, 2001 at 03:25 UTC
    A lexical variable (declared with "my") is not in a package, and is thus visible only lexically (hence the name) where it is declared.

    You'll have to rethink whether you want a package variable instead of a lexical variable.

    -- Randal L. Schwartz, Perl hacker

      Thanks merlyn. I was actually reading your Coping with Scope, but it hadn't clicked yet. I think I'll try as moo has suggested. -jim