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