Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Can I from within a module access variables from the calling program?

by tobyink (Abbot)
on Oct 25, 2012 at 12:25 UTC ( #1000844=note: print w/ replies, xml ) Need Help??


in reply to Can I from within a module access variables from the calling program?

use v5.10; use strict; use warnings; use PadWalker (); { package Some::Module; sub what_is_x { my $value = $Other::Module::x; say "\$x is $value"; } # Just because PadWalker exists, doesn't mean it's a good idea... sub what_is_y { my $value = ${ PadWalker::peek_my(1)->{'$y'} }; say "\$y is $value"; } } { package Other::Module; our $x = 123; my $y = 456; Some::Module::what_is_x(); Some::Module::what_is_y(); }
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: Can I from within a module access variables from the calling program?
Download Code
Replies are listed 'Best First'.
Re^2: Can I from within a module access variables from the calling program?
by HJO (Acolyte) on Oct 25, 2012 at 12:47 UTC

    Hi,

    Well thanks for your answer, I didn't understood it at first but now it get it ^^

    I just have two "problems" with your answer, first I'm using Perl 5.8.8 and I'm not sure that your technique is compatible with this version since you're using Perl 5.10

    Secondly, I would have prefered not to use another Module to do so

    but thansk anyway, maybe your answer will help somebody else later

      The only 5.10-specific feature I used was say which is a new (well... five years old) keyword which works like print but automatically appends a new line.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        Oh indeed that's quite usefull. Thanks I think i'll be able to adapt this

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2015-07-29 13:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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









    Results (263 votes), past polls