Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Can you create *real* global variables?

by lepre (Initiate)
on Mar 11, 2007 at 13:40 UTC ( #604225=note: print w/ replies, xml ) Need Help??

in reply to Can you create *real* global variables?

Try solving this one...

#!/usr/bin/perl -w use strict; my $V1 = "1.2.3"; { package foo; use strict; sub fetchvar { print $main::V1; print $main::V2; } } my $V2 = "3.2.1"; foo::fetchvar();
The ^ or . trick is not allowed in my version of perl.

Comment on Re: Can you create *real* global variables?
Download Code
Re^2: Can you create *real* global variables?
by Anno (Deacon) on Mar 11, 2007 at 16:45 UTC
    Please put <c>...</c> tags around code.

    What's to solve? Are you asking why the package variables $main::V1 and $main::V2 aren't defined when foo::fetchvar() is called?

    That's because they were never set. Instead, you set the lexical variables (of file scope) $V1 and $V2.

    Change the my declarations for $V1 and $V2 to our and your code will do what (I suppose) you expect it to do.


      The "our" declaration is not documented in the second edition of Wall's book (1996). Oh, gosh, it is 10 years old... Time passes by. Thanks.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2015-07-04 03:46 GMT
Find Nodes?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...

    Results (57 votes), past polls