|
From: Fred on 15 Feb 2007 23:07 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; }
From: J�rgen Exner on 16 Feb 2007 00:40 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
From: anno4000 on 16 Feb 2007 06:03 Fred <itfred(a)cdw.com> 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
From: Heinrich Mislik on 16 Feb 2007 06:31 In article <DbGdnUWsuf6Qs0jYnZ2dnUVZ_syunZ2d(a)comcast.com>, itfred(a)cdw.com 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
From: Tad McClellan on 16 Feb 2007 07:00
J�rgen Exner <jurgenex(a)hotmail.com> 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::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. -- Tad McClellan SGML consulting tadmc(a)augustmail.com Perl programming Fort Worth, Texas |