I want to set a variable in a module from one calling module and want to retrieve that value inanother calling module.
I have done something line this:
package Test;
our $data = undef;
sub set_data
{
$data = shift @_;
}
sub get_data
{
return $data
}
I am setting the data as :
package Mod1;
use Test;
Test::set_data(1);
I am retrieving the data as:
package Mod2;
use Test;
print Test::get_data();
But I am getting undef while retrieving the value.
What is wrong in my implementation?
Add some debugging (e.g.
warn "setting data to $data";
at the end of set_data andwarn "getting data: $data";
at the beginning of get_data) and verify things are happening in the order you think they are.Note that the main code of a module (which both your get_data and set_data calls are) runs when the module is loaded at compile time; if you are depending on something else that happens at run time to get the value, that won't work.
If all else fails, strip out as much of your code as you can and still have it fail and show us the compete failing case (including whatever loads Mod1 and Mod2).