Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Why can't I access variable from other subroutine?

Reply
Thread Tools

Why can't I access variable from other subroutine?

 
 
Fred
Guest
Posts: n/a
 
      02-16-2007
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;

}




 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      02-16-2007
Fred wrote:
> 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


 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      02-16-2007
Fred <> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
Heinrich Mislik
Guest
Posts: n/a
 
      02-16-2007
In article <>, says...
>
>
>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

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      02-16-2007
Jürgen Exner <> wrote:
> Fred wrote:
>> 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:ackage::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.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      02-18-2007
Fred wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
How to access web.sitemap file from other virtual directories other than the current application root folder? hvajja@gmail.com ASP .Net 0 08-07-2006 08:26 PM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
Name of variable is value of other variable Bart Van der Donck Perl Misc 12 09-11-2004 09:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57