Variable scope across packages
[email protected] ("Stephen Criddle") Fri, 31 Aug 2001 15:30:52 +0100
| Newsgroups | perl.macperl.webcgi |
|---|---|
| Message-ID | <[email protected]> |
--------------F33C745D3FB156F2EC222E0F
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
I'm sure there is a simple answer to this, but it's currently driving me
mad...
I want to write some packages with some regularly-used routines in. I
also want to have a file which initialises some global variables (rather
than having to define them in every script that uses the packages).
I can get this to work if I put all of my routines in a single package,
and initialise my global variables at the start of that routine. What I
can't do is get the scope of a variable to go from one package to
another.
If I set the value in my "global" package, the script which calls it can
see the value. However, the other package can't see it.
To summarise:
TEST.PL:
use Test::Global;
use Test::Other;
&Test::Init;
print "Main:".$globalAttempt."\n";
&Test::Other;
-----
GLOBAL.PM:
package Test::Global;
use Exporter;
@ISA = ('Exporter');
@EXPORT = qw($globalAttempt);
sub Test::Init {
$globalAttempt = "This is a test";
print "Global:".$globalAttempt."\n";
return(0);
}
1;
---
OTHER.PM:
package Test::Other;
sub Test::Other {
print "Other:".$globalAttempt."\n";
return($output);
}
1;
---
When I run this, I get:
Global:This is a test
Main:This is a test
Other:
I want the Test::Other package to be able to see the variables I set in
Test::Global
Steve Criddle
This email and the files sent with it are confidential and for the use of the
intended recipient(s) only. If you are not the intended recipient(s), please
note that any use, distribution or copying of this communication or the
information in it, is strictly prohibited. If you have received this
communication in error, please notify the sender or forward this message to
[email protected] and then destroy the email and any copies of it. This
communication is from Saga Holidays Ltd whose registered office is The Saga
Building, Enbrook Park, Folkestone, UK CT20 3SE. Registered number 02174052.
--------------F33C745D3FB156F2EC222E0F--