Link to home
Start Free TrialLog in
Avatar of Pakman
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;
Avatar of Pakman
Pakman

ASKER

Woops! Forgot to add this line after the require statement:

gethash();

Problem still holds.
Avatar of ozo
#my (%hash);
use vars qw(%hash);
#or
use strict;
my (%hash);
require "b.pl";
%hash=gethash();
print %hash;
Avatar of Pakman

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

Avatar of Pakman

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.
Avatar of Pakman

ASKER

Ozo submit your idea as an answer.
Which one?
You might also consider b2pi's suggestion as a more long term solution.
Avatar of Pakman

ASKER

This one:

Comment
      From: ozo
                                                        Date: Monday, August 31 1998 - 10:17AM PDT

      #my (%hash);
      use vars qw(%hash);
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial