You will be able to access the main program's %hash1 variable directly
from a package if it is not declared with my
(whose visibility would be limited to the block (or file if it's
not in an explict block) that it is
declared in).
You will need to explictly specify the package of the
variable with like %::hash1 or %main::hash1 (and, if
you are using strict as you should be,
you must decalre %hash1 in the main program with our or
use vars.)
However, this is probably not good design. It would probably
be best if you made your module not be dependent
on what variables the main program is using. That is,
you would instead pass the relevant hash to the module's
functions (ideally as a reference.)
update: changed references link.
| [reply] [d/l] [select] |
Good programming practice would be to pass the variable
as a parameter into any function that required it.
Using "global" variables inside a function, particularly
one in a different package, is a sign of bad program
design.
--
<http://www.dave.org.uk>
"The first rule of Perl club is you don't talk about
Perl club."
| [reply] |
I'm not sure I understand what you are trying to ask...
However, if I do understand correctally...
If you have a variable in one namespace (package) and you want to get at it from another, you need to use a format similar to $package::variable
In your case, I believe you need to use %main::hash1 to acess your hash that was declared within x.pl, from the package X.
It is prefixed with main because that is the default namespace/package for any program unless another is declared with a package statement.
"Weird things happen, get used to it"
Flame ~ Lead Programmer: GMS
http://gms.uoe.org
| [reply] |
A little confused on about your email, however I'll stick my two cents in.
if you create a variable in X.pm, you can then export that
variable into X.pl.
in X.pm
package X;
vars $database1 = "database";
vars $username1 = "user1";
vars $password1 = "user1";
use vars qw(@ISA @EXPORT @EXPORT_OK);
use Exporter;
@ISA = qw(Exporter Shell);
@EXPORT = qw( $database1 $username1 $password1);
1;
now in X.pl
use X.pm
print "$database1,$username1,$password1";
you can also export sub routines.
Hope that helps.
Ant | [reply] |