Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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

by kcott (Chancellor)
on Oct 25, 2012 at 12:00 UTC ( #1000835=note: print w/replies, xml ) Need Help??

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

G'day HJO,

"Yes, but it is commonly considered bad practice. You can easily access global variables of the main program: $main::foo gives access to $foo in the main program"

I tried this and obviously it didn't work, ...

I don't see how this ties in with your question but what you claim obviously doesn't work, clearly does:

$ perl -le '$foo = "whatever"; print $main::foo;' whatever

-- Ken

  • 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 MidLifeXis (Monsignor) on Oct 25, 2012 at 12:08 UTC

    Subtle difference:

    perl -le 'my $foo = "whatever"; print $main::foo;'


      I may be missing the point you're making but HJO's statement referred to global variables. Other examples with variables explicitly declared as global:

      $ perl -le 'local $foo = "whatever"; print $main::foo;' whatever
      $ perl -le 'our $foo = "whatever"; print $main::foo;' whatever

      -- Ken

        Very true, but the OP also was using my variables in their original code. our, local, or undeclared (as you demonstrated) would work.


Re^2: Can I from within a module access variables from the calling program?
by HJO (Acolyte) on Oct 25, 2012 at 12:09 UTC

    Hi,thank you for answering.

    Well, the obvious part is that I was asking help here so I didn't manage to make this working.

    Could you please look at the code I gave, when I try to run the script, it says to me that there's an "Use of uninitialized value in concatenation (.) or string at..." in my module... so here is why I'm asking some help on the matter

      Use of uninitialized value
      Oh, I see. You were probably not following the whole conversation in the ChatterBox. If you are not mentioning the variable in a subroutine in the package, the code is executed before you assign any value to the variable in main::. You have to assign the value in a BEGIN block in order to be accessible from within the top level of the package.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        Well yes, to be honnest, I just saw those three lines, and when I wanted to learn more it was too late for me to see other answers... I'm don't understand quite well how the ChatterBox works and I was a bit too shy to ask again so I tried a private message, but it wasn't long enought to express my problem so I posted a message after a light search to avoid double posts...

        Thanks a lot, I was afraid that I had to use a method that would refactor my whole code ^^"

        I may have really bad luck, but it's not working either, even with the BEGIN {...} method :(

        BEGIN{ my %month = ( 'Jan' => '01', 'Feb' => '02', 'Mar' => '03', 'Apr' => '04', 'May' => '05', 'Jun' => '06', 'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12', ); my $localdate = localtime; my ($dname, $mname, $day, $time, $year) = split( " ",$localdate); #my $log_name = "./log/ST03_$year-$month{$mname}-$day\_$time.log"; our $log_name = "./log/ST03_$year-$month{$mname}-$day\_$time.log"; }

        Is something missing ?

      Hi HJO,

      Try this, In your module declare global variable, then export variable

      .... our %glo_var = ('a'=>100,'b'=>200); ... @EXPORT = qw(%glo_var ... members); ... 1;

      In your tool

      use SampleModule::TestMod; print $glo_var{'a'};exit;


        Hi perl_walker

        it appears to me that I wasn't enought specific on my post be cause what I'm trying to do is precisely the opposite, calling a variable FROM the principal script INTO the module

        But thanks anyway :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000835]
[james28909]: thanks for the vid holli, i do like it. thanks for sharing :)
[holli]: gives me goosebumps every time I watch it
holli gotta grab some groceries before dawn. l8ters
[erix]: s/dawn/dusk/
[erix]: (you're welcome :P)
[james28909]: you know, i had a thought a few mnths back. what if this universe were just a puzzle, waiting for life to figure it out!
[james28909]: what if one day we are able to grab the curtain that is "spacetime" and pull it back to reveal <insert imagination here> pulling at levers and ropes xD
[holli]: like dorothy?
[james28909]: see ya later holli
[james28909]: yeah like wizard of oz lol. its just a joke though

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (15)
As of 2017-12-15 14:41 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (433 votes). Check out past polls.