I have a script (we'll call it test.pl), which "use"s a module (we'll call it my_module.pm).
test.pl is kicked off with some varying arguments provided to it (i.e. different arguments each time it's run), and I would like to share one of these variables with my_module.pm, so that when my_module.pm first gets loaded it'll do something different according to the variable's value.
So, examples of what I have are below:
package my_module; $VERSION = '1.00'; use strict; our $gMessage; sub import { $gMessage = @_; print "Yep, I'm in here ... \n\n"; } &my_sub(); sub my_sub { print $gMessage; }
test.pl looks like this:
#!/usr/bin/perl use strict; use my_module 'xyz'; ... ... ...
In this test.pl, just for an example, I'm just providing a static argument, but in reality it's a varying value.
Nevertheless, what I'm anticipating is that test.pl will "use" my_module, and pass in "xyz" and then the module will print the "Yep, I'm in here" message, followed by "xyz" when it gets to my_sub, which is called when my_module is loaded.
However, I guess I must be misunderstanding something because the "Yep, I'm in here" message never gets printed, and the variable is empty when it gets to my_sub.
Thus, I guess the import sub is never being called, but I thought that by default an import sub is always called, if it exists, when "use"ing a module.
Noting that I don't want to share a variable from the module to the script, but vice-versa, if anyone could shed some light, or suggest a better method, then I'd be grateful. Thanks!
In reply to Share Variable in Script with a Module by DanielSpaniel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |