Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Re: Re: using main package variables within a module

by mfriedman (Monk)
on Jun 17, 2002 at 18:11 UTC ( #175150=note: print w/replies, xml ) Need Help??

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://175150]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2021-06-18 12:55 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (89 votes). Check out past polls.