• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 180
  • Last Modified:

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;
0
Pakman
Asked:
Pakman
  • 5
  • 4
  • 2
1 Solution
 
PakmanAuthor Commented:
Woops! Forgot to add this line after the require statement:

gethash();

Problem still holds.
0
 
ozoCommented:
#my (%hash);
use vars qw(%hash);
0
 
ozoCommented:
#or
use strict;
my (%hash);
require "b.pl";
%hash=gethash();
print %hash;
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
PakmanAuthor Commented:
Okay thanks.
0
 
b2piCommented:
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

0
 
PakmanAuthor Commented:
Okay okay now how do I get this question to become locked so that I can grade it?
0
 
b2piCommented:
Ask ozo to submit his idea as an answer, then grade it when he does so.
0
 
PakmanAuthor Commented:
Ozo submit your idea as an answer.
0
 
ozoCommented:
Which one?
You might also consider b2pi's suggestion as a more long term solution.
0
 
PakmanAuthor Commented:
This one:

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

      #my (%hash);
      use vars qw(%hash);
0
 
ozoCommented:
#my (%hash);
use vars qw(%hash);

#although I like %hash=gethash(); better:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now