Why can't I access variable from other subroutine?

F

Fred

The two subs below are in a package named Mypkg.pm.
The LoadConfig sub uses AppConfig. In this sub I
can print the host variable using $config->host().
So I added the line our $hostx = $config->host(),
to assign this to $hostx. Now in the TestSub sub,
I try and print Mypkg::LoadConfig::hostx, but it
never prints. How can I access hostx from the
TestSub subroutine?

-Thanks


sub LoadConfig
{
shift @_;
my ($cfgfile, $prn) = @_;
my $config = '';

$config = AppConfig->new(
{
CASE => 1,
PEDANTIC => 0,
CREATE => 1,
ERROR => sub {},
GLOBAL => { ARGCOUNT => ARGCOUNT_ONE }
}
);

$config->file($cfgfile);


####### Can't access this below in DBConnect ########
our $hostx = $config->host();


if ($prn eq 'p') {
print "Configuration file: $cfgfile\n";
print "dbname:\t\t".$config->dbname()."\n";
print "host:\t\t".$config->host()."\n";
print "port:\t\t".$config->port()."\n";
print "username:\t".$config->username()."\n";
}

}


sub TestSub
{

LoadConfig('/etc/my.conf');

######## Cannot print host variable from LoadConfig above ########
print Mypkg::LoadConfig::hostx;

}
 
J

Jürgen Exner

Fred said:
The two subs below are in a package named Mypkg.pm.
The LoadConfig sub uses AppConfig. In this sub I
can print the host variable using $config->host().
So I added the line our $hostx = $config->host(),
to assign this to $hostx. >
sub LoadConfig [...]
our $hostx = $config->host();

The "our" makes this a variable that is visible only within the sub
LoadConfig()

[...]
}


sub TestSub
{
LoadConfig('/etc/my.conf');

######## Cannot print host variable from LoadConfig above ########
print Mypkg::LoadConfig::hostx;

}
Now in the TestSub sub,
I try and print Mypkg::LoadConfig::hostx, but it
never prints. How can I access hostx from the
TestSub subroutine?

Technically correct but from a software engineering point of view rather
ugly: Make $hostx a global variable.
Better: have LoadConfig() return() the value of $hostx to the caller.

jue
 
A

anno4000

Fred said:
The two subs below are in a package named Mypkg.pm.
The LoadConfig sub uses AppConfig. In this sub I
can print the host variable using $config->host().
So I added the line our $hostx = $config->host(),
to assign this to $hostx. Now in the TestSub sub,
I try and print Mypkg::LoadConfig::hostx, but it
never prints. How can I access hostx from the
TestSub subroutine?

-Thanks

You don't show the package line that precedes this. I'll
assume it's

package Mypkg::LoadConfig;

[snip]
####### Can't access this below in DBConnect ########
our $hostx = $config->host();
[snap]

######## Cannot print host variable from LoadConfig above ########
print Mypkg::LoadConfig::hostx;

Scalar variables have a "$" in front of them. Try

print "$Mypkg::LoadConfig::hostx\n";


Anno
 
H

Heinrich Mislik

The two subs below are in a package named Mypkg.pm.
The LoadConfig sub uses AppConfig. In this sub I
can print the host variable using $config->host().
So I added the line our $hostx = $config->host(),
to assign this to $hostx. Now in the TestSub sub,
I try and print Mypkg::LoadConfig::hostx, but it
never prints. How can I access hostx from the
TestSub subroutine?

-Thanks


sub LoadConfig
{
shift @_;
my ($cfgfile, $prn) = @_;
my $config = '';

$config = AppConfig->new(
{
CASE => 1,
PEDANTIC => 0,
CREATE => 1,
ERROR => sub {},
GLOBAL => { ARGCOUNT => ARGCOUNT_ONE }
}
);

$config->file($cfgfile);


####### Can't access this below in DBConnect ########
our $hostx = $config->host();


if ($prn eq 'p') {
print "Configuration file: $cfgfile\n";
print "dbname:\t\t".$config->dbname()."\n";
print "host:\t\t".$config->host()."\n";
print "port:\t\t".$config->port()."\n";
print "username:\t".$config->username()."\n";
}

}


sub TestSub
{

LoadConfig('/etc/my.conf');

######## Cannot print host variable from LoadConfig above ########
print Mypkg::LoadConfig::hostx;

print $Mypkg::hostx,"\n";

greetings

Heinrich Mislik
 
T

Tad McClellan

Jürgen Exner said:
Fred said:
The two subs below are in a package named Mypkg.pm.
The LoadConfig sub uses AppConfig. In this sub I
can print the host variable using $config->host().
So I added the line our $hostx = $config->host(),
to assign this to $hostx. >
sub LoadConfig [...]
our $hostx = $config->host();

The "our" makes this a variable that is visible only within the sub
LoadConfig()


No, it makes the short name ($hostx) visible only within the (sub) block.

The long name ($Some::package::hostx) can still be used to access
that variable.

Make $hostx a global variable.


It already is a package (global) variable.

our() does not scope variables, it only scopes a _name_
for the variable.
 
J

Joe Smith

Fred said:
The two subs below are in a package named Mypkg.pm.
The LoadConfig sub uses AppConfig. In this sub I
can print the host variable using $config->host().
So I added the line our $hostx = $config->host(),
to assign this to $hostx. Now in the TestSub sub,
I try and print Mypkg::LoadConfig::hostx, but it
never prints. How can I access hostx from the
TestSub subroutine?

LoadConfig is the name of a sub, not the name of a package.

[For $Mypkg::LoadConfig::hostx to be valid, the name of the
package has to be Mypkg::LoadConfig when $hostx is referenced.
But you've told us that the package is Mypkg, not Mypkg::LoadConfig.]
sub LoadConfig {
our $hostx = $config->host();

There you are defining a global variable named $hostx in package Mypkg,
so the name of the variable in this case is $Mypkg::hostx. The fact that
the 'our' statement is seen inside sub LoadConfig{} is irrelevant and does not
change the name of the package, which is still Mypkg.

-Joe
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top