Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Accessing main::Constant

by BrowserUk (Pope)
on Dec 16, 2009 at 05:32 UTC ( #812973=note: print w/ replies, xml ) Need Help??


in reply to Accessing main::Constant

Use: sub pr { print main::FO1(); }


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: Accessing main::Constant
Download Code
Re^2: Accessing main::Constant
by nagalenoj (Friar) on Dec 16, 2009 at 05:52 UTC
    Thanks. It works fine.

    I need another clarification, Is it possible to access a variable in main package like below,

    package pq; . use constant { VAR => "main::$VAR" }; . . sub pr { print main::$VAR; print VAR; }

    I'm getting error. So I thought, 'As the constants are expanded when compilation, it isn't working as what I expected'. $VAR is a our variable in main package.

      Firstly, the syntax would be $main::VAR

      Secondly, the value of variables in main are not set until after the uses at the top of the code, so you would get undef. You could address that by setting the $VAR in a BEGIN block:

      ... BEGIN{ our $VAR = 'fred'; } use pg; ...

      But the question is why would you want to do that?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I've a similar scenario to be addressed,

        I've a hash in the pl file, which I need access it from a module. when Instead of using like $main::VAR->{a}->{b} everytime, Instead I wanted to make it($main:VAR) as a constant and When I need to do that operation, I will be calling it like CONSTANT->{a}->{b}.

        Am I doing it right?!
      $main::VAR is the appropriate syntax.

      ( Woops, this was said in an earlier reply. I had missed it )

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2014-12-17 22:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (39 votes), past polls