Search code examples
perlstrawberry-perl

How to access a hash in the main namespace from inside a module


I have my main Perl script which contains

my $System = {
    Path =>
    {
        root => 'hello'
    }
}

print $System->{'Path'}->{'root'}; # prints 'hello'

How can I access the $System->{'Path'}->{'root'} variable from an external module?

I know I can use the main:: namespace to retrieve global variables, such as $main::x, but the following doesn't work $main::System->{'Path'}->{'root'}.

I tried also different syntaxes but I'm not able to get it.

What am I doing wrong?


Solution

  • Variables declared with my only have lexical scope and are not visible in a package.

    Declare it with our instead.