how to access a (global) variable across files

  • Thread starter Gunnar Hjalmarsson
  • Start date
G

Gunnar Hjalmarsson

Newbie said:
3> When I declare $ini as out (global) in A.pl and try accessing it
in B.pm as:
my $iniFile = Config::IniFiles->val(file =>$ini)
It says: Global symbol "$ini" requires explicit package name at
B.pm

Try:

my $iniFile = Config::IniFiles->val(file =>$main::ini)
 
N

Newbie

Hi

Scenario is like this:
1>I have a file A.pl. It makes a call to the new() functions of other
2 files B.pm and C.pm.
2> I am accepting a cfg.ini filename as parameter while running A.pl
and storing it in a variable $ini, which I want to access to B.pm and
C.pm.
3> When I declare $ini as out (global) in A.pl and try accessing it in
B.pm as:
my $iniFile = Config::IniFiles->val(file =>$ini)
It says: Global symbol "$ini" requires explicit package name at B.pm

Please tell me how to get the solution for this. P.S.: I dont want to
pass this variable as argument to new().

Pl. reply ASAP.
thanks
 
B

Ben Morrow

Gunnar Hjalmarsson said:
Try:

my $iniFile = Config::IniFiles->val(file =>$main::ini)

While this is the correct answer to the question the OP asked :), it
would almost certainly be better to either create the variable in the
B namespace, ie. in A.pl use
$B::ini = "foo";
rather than simply
our $ini = "foo";
and then declare it as 'our $ini' in B.pm; or to put
my $ini;
sub set_ini {
$ini = shift;
}
in B.pm and then call 'B::set_ini("foo")' in A.pl. It's cleaner, and
makes your module more likely to be reusable.

Ben
 
G

Gunnar Hjalmarsson

Ben said:
While this is the correct answer to the question the OP asked :),
it would almost certainly be better to either create the variable
in the B namespace, ie. in A.pl use
$B::ini = "foo";
rather than simply
our $ini = "foo";
and then declare it as 'our $ini' in B.pm; or to put
my $ini;
sub set_ini {
$ini = shift;
}
in B.pm and then call 'B::set_ini("foo")' in A.pl. It's cleaner,
and makes your module more likely to be reusable.

No objection. Global variables in package main is normally not advisable.

Maybe the very first advise to OP should be to study the relevant
docs, such as:

http://www.perldoc.com/perl5.8.0/pod/perlmod.html
 
N

Newbie

thanks 4 ur replies... but thats not working... something trivial is
going wrong somewhere

i m doing it from scratch. m creating 2 files A.pl:
#!/user/local/bin/perl

use strict;
use B;

my $varA = "cfgA";
$B::varG = "cfgG";

print "Inside PL file....varA = $varA\n";
print "Inside PL file....varG = $B::varG\n";

&B::test ();

and B.pm:

package B;

local $varG;
@EXPORT = qw (&test $varG);

sub test () {
print "a: $main::varA\n";
print "varG = $varG\n";
}

1;

running A.pl, it gives:
Undefined subroutine &B::test called at A.pl line 12.

P.S.: I have set the env var PERL5LIB=mydir
Please reply
 
A

Anno Siegel

Newbie said:
thanks 4 ur replies... but thats not working... something trivial is

Oh please... use complete English words. This kind of baby talk has
been out since the last 300 Baud modems disappeared from the market.
going wrong somewhere

i m doing it from scratch. m creating 2 files A.pl:
#!/user/local/bin/perl
^
You mean "/usr/...".
use strict;
use B;

Don't call your module "B". More generally, don't call your module
like an existing module. The Perl standard distribution has a module
B.pm, and that is loaded, not yours.

my $varA = "cfgA";
$B::varG = "cfgG";

print "Inside PL file....varA = $varA\n";
print "Inside PL file....varG = $B::varG\n";

&B::test ();

and B.pm:

package B;

local $varG;
@EXPORT = qw (&test $varG);

If you want to export, you must load the Exporter module. "require Exporter"
is missing. You also need to let your module inherit the import() routine
from Exporter. "BEGIN { our @ISA = qw( Exporter) }" is missing. See
"perldoc Exporter". You should always look at the documentation of things
if something isn't working as you expect.

The failure to use Exporter correctly is irrelevant here, because you
never call the exported version of test(), you are calling it fully
qualified.
sub test () {
print "a: $main::varA\n";
print "varG = $varG\n";
}

1;

running A.pl, it gives:
Undefined subroutine &B::test called at A.pl line 12.

Because the B.pm you actually loaded doesn't define a test() subroutine.
P.S.: I have set the env var PERL5LIB=mydir

Why would that matter? Is B.pm in mydir?

Anno
 
T

Tassilo v. Parseval

Also sprach Anno Siegel:
If you want to export, you must load the Exporter module. "require Exporter"
is missing. You also need to let your module inherit the import() routine
from Exporter. "BEGIN { our @ISA = qw( Exporter) }" is missing. See
"perldoc Exporter". You should always look at the documentation of things
if something isn't working as you expect.

Maybe we can come to a conclusion in this group not to use this
cumbersome idiom of subclassing Exporter by manually pushing onto
@ISA. We recently had a thread ("what is @$?") on this where no one
quite could explain why it is exemplified that way throughout the Perl
documentation. So unless someone can point out any potential (or
non-potential) flaws of

use base qw(Exporter);

we should recommend this notion. After all, this is what the base pragma
was initially made for.

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top