Solved

require and my

Posted on 1998-08-31
11
167 Views
Last Modified: 2010-03-05
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
Comment
Question by:Pakman
  • 5
  • 4
  • 2
11 Comments
 

Author Comment

by:Pakman
ID: 1204617
Woops! Forgot to add this line after the require statement:

gethash();

Problem still holds.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1204618
#my (%hash);
use vars qw(%hash);
0
 
LVL 84

Expert Comment

by:ozo
ID: 1204619
#or
use strict;
my (%hash);
require "b.pl";
%hash=gethash();
print %hash;
0
 

Author Comment

by:Pakman
ID: 1204620
Okay thanks.
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1204621
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Pakman
ID: 1204622
Okay okay now how do I get this question to become locked so that I can grade it?
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1204623
Ask ozo to submit his idea as an answer, then grade it when he does so.
0
 

Author Comment

by:Pakman
ID: 1204624
Ozo submit your idea as an answer.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1204625
Which one?
You might also consider b2pi's suggestion as a more long term solution.
0
 

Author Comment

by:Pakman
ID: 1204626
This one:

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

      #my (%hash);
      use vars qw(%hash);
0
 
LVL 84

Accepted Solution

by:
ozo earned 150 total points
ID: 1204627
#my (%hash);
use vars qw(%hash);

#although I like %hash=gethash(); better:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now