3

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?

Kallol
  • 302
  • 4
  • 16
  • Are the two packages Mod1 and Mod2 under the same file ? Please consider using some other name as Test is a core module. – xtreak Dec 08 '14 at 06:14
  • No they are different files and have different folder structures too. I made them access the Test module trough use lib which I have bit mentioned here. – Kallol Dec 08 '14 at 06:20
  • Ok. When I print the $data it prints but on return to Mod2 it gives undef. – xtreak Dec 08 '14 at 06:35

3 Answers3

1

I have figured out the problem. The setter code

package Mod1;
use Test;

Test::set_data(1);

is running in a threaded function. I figured out that inside the function the state of the variable is getting changed as expected and I can also access the latest data.

Once I am out of the threaded function the value of the variable no more persists. What I mean by out of the threaded function is after I joined all the running threads.

Kallol
  • 302
  • 4
  • 16
  • You can also put `$data` within a closure around those two functions, which'll make it effectively a private var. Threading code can lead to some interesting traps though. – Sobrique Dec 08 '14 at 09:51
0

Add some debugging (e.g. warn "setting data to $data"; at the end of set_data and warn "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).

ysth
  • 96,171
  • 6
  • 121
  • 214
  • @ysth So when I set the data from set_data of one file and access it from the get_data of another file it won't persist? – xtreak Dec 08 '14 at 06:59
  • it will, if you are doing it in the order you think you are. but running things during compilation makes it easy to make a mistake about the order. – ysth Dec 08 '14 at 07:09
0

When I have to do something like that I usually take help of Storable. You can use that method.

See this answer (https://stackoverflow.com/a/17082242/257635) for an example.

Community
  • 1
  • 1
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
  • Serialization and deserialization of the datastructure is always an option here. But the problem lies there is a cost involved in reading and writing the content to and from the file. I was looking for native perl solution. Thanks for your suggestion. – Kallol Dec 09 '14 at 03:35