http://www.perlmonks.org?node_id=175150


in reply to Re: Re: using main package variables within a module
in thread using main package variables within a module

I'm really wanting to understand why my $one isn't working, I think it's in scope!

But it's not in scope. :)

The problem you're having is because you're trying to access a lexical (my) variable outside of its lexical scope. That's a no-no. Only package variables can be accessed via their full package name.

As an example of why this is, consider the following:

if ($foo == 3) { my $bar = 1; } else { my $bar = 1; }

Here you've got two distinct $bar's, each one in a different lexical block. If you said $main::bar, and Perl allowed outside access to lexicals, how would Perl know which $bar to give you? It can't. That's why $main::bar can only refer to a package global. Since you're using strict, you have to declare it with a fully qualified name. That means you'll need to call it $main::bar (or $::bar, for short) when you declare it in test.pl.