Reputation: 774
I have a perl script and a perl module (just simple skeletons) as shown below:
module.pm
package My::Package;
use strict;
sub mySub() {
return "Answer is: ".$checkIt;
}
1
script.pl
#!/usr/bin/env perl
use strict;
my $checkIt = "someTextHere";
require My::Package;
print mySub(); #or print My::Package->mySub();
But it returns just Answer is:
I need My::Package (module.pm) to be able to load vars from the caller script (script.pl), like $checkIt.
Can you help me with this, please?
Any help is appreciated!
Upvotes: 0
Views: 44
Reputation: 9296
The whole purpose of being able to break programs down into smaller pieces (classes, modules, subroutines etc), is so that we can minimize and in many cases eliminate sharing global variables. Won't setting up your subroutine to accept parameters work?
Module.pm
package Module;
use Exporter qw(import);
our @EXPORT_OK = qw(my_sub);
sub my_sub {
my ($param) = @_;
return "Answer is: $param\n";
}
1;
script.pl
use warnings;
use strict;
use Module qw(my_sub);
my $string = "hello, world!\n";
my $return = my_sub($string);
print $return;
Output:
Answer is: hello, world!
Upvotes: 1