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

symbolic reference to variable in callers name space

by spq (Friar)
on Aug 06, 2002 at 17:02 UTC ( #188080=perlquestion: print w/replies, xml ) Need Help??
spq has asked for the wisdom of the Perl Monks concerning the following question:

I'm developing CGI's and modules for use under Apache with mod_perl. I would _like_ to have a module access the value of a variable in the callers package, namely $DEBUG.

$main::DEBUG works fine in the module when it is run from (make test). But when called from the CGI, it's undefined. OK, I looked at (caller)[0] (the callers package), which looks like (in my current project:

So, since it will change, I thought I'd try to use the package name as a symbolic reference(slightly abridged):

my $pkg = (caller)[0]; no strict; warn("${$pkg::DEBUG} in $pkg"); # see what it gets

Which was still undefined. But if I use the package string literally:
warn("$Apache::ROOTweb_2emgh_2eharvard_2eedu::dnacore::cgi_2dbin::synthesis::CE_QC::DEBUG in $pkg");

I get the value expected. I've thrashed around and skimmed the docs for a while now, trying different ways of bracketing, attempting to typeglob, etc. Now it's way past time I ate and I'm frustrated. Please, if anyone can help solve this, I would, as always, be very appriciative!


Replies are listed 'Best First'.
Re: symbolic reference to variable in callers name space
by Fletch (Chancellor) on Aug 06, 2002 at 17:11 UTC

    ITYM ${"${pkg}::DEBUG"}.

    ${$pkg::DEBUG} is the variable named by the value contained in the variable $DEBUG in package pkg.

Re: symbolic reference to variable in callers name space
by danboo (Beadle) on Aug 06, 2002 at 18:05 UTC
    for your warn example, i believe you want:
    warn(${"${pkg}::DEBUG"} . " in $pkg"); # see what it gets

    - danboo
      Thank you both! That does indeed work. I did need to change:
      warn("DEBUG = ${"${pkg}::DEBUG"} in $pkg");

      in my code (which threw a syntax error about missing bracket!?), to:
      warn("DEBUG = " . ${"${pkg}::DEBUG"} . " in $pkg");

      Which did the trick for testing. Thanks again! Sean

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://188080]
Approved by Ovid
What's the matter? Cat got your tongue?...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2018-02-24 16:04 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (310 votes). Check out past polls.