Pakman
asked on
require and my
I need a hash with hard-coded values in a 'shared' file. Other files would use the hash in the shared file. However, in the example below %hash from the shared file isn't recognized. What do I need to do to make it work?
********************** a.pl ************************** ***
use strict;
my (%hash);
require "b.pl";
print %hash;
********************** shared.pl **************************
sub gethash {
%hash = ("number" => 123);
}
1;
********************** a.pl **************************
use strict;
my (%hash);
require "b.pl";
print %hash;
********************** shared.pl **************************
sub gethash {
%hash = ("number" => 123);
}
1;
#my (%hash);
use vars qw(%hash);
use vars qw(%hash);
#or
use strict;
my (%hash);
require "b.pl";
%hash=gethash();
print %hash;
use strict;
my (%hash);
require "b.pl";
%hash=gethash();
print %hash;
ASKER
Okay thanks.
In a little bit more modern fashion:
File B.pm (included among many)
package B;
use Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(%hash);
%hash = (
hashval1 => 1,
hashval2 => 2,
hashval3 => 3,
hashval4 => 4,
);
1;
## End of file B.pm
File A.pl
#!/usr/local/bin/perl -w
use B qw(%hash);
print "Got third element as $hash{hashval3}\n";
## End of file a.pl
File B.pm (included among many)
package B;
use Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(%hash);
%hash = (
hashval1 => 1,
hashval2 => 2,
hashval3 => 3,
hashval4 => 4,
);
1;
## End of file B.pm
File A.pl
#!/usr/local/bin/perl -w
use B qw(%hash);
print "Got third element as $hash{hashval3}\n";
## End of file a.pl
ASKER
Okay okay now how do I get this question to become locked so that I can grade it?
Ask ozo to submit his idea as an answer, then grade it when he does so.
ASKER
Ozo submit your idea as an answer.
Which one?
You might also consider b2pi's suggestion as a more long term solution.
You might also consider b2pi's suggestion as a more long term solution.
ASKER
This one:
Comment
From: ozo
Date: Monday, August 31 1998 - 10:17AM PDT
#my (%hash);
use vars qw(%hash);
Comment
From: ozo
Date: Monday, August 31 1998 - 10:17AM PDT
#my (%hash);
use vars qw(%hash);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
gethash();
Problem still holds.