Why can't I access variable from other subroutine?

Discussion in 'Perl Misc' started by Fred, Feb 16, 2007.

  1. Fred

    Fred Guest

    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;

    }
     
    Fred, Feb 16, 2007
    #1
    1. Advertising

  2. 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
     
    Jürgen Exner, Feb 16, 2007
    #2
    1. Advertising

  3. Fred

    -berlin.de Guest

    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
     
    -berlin.de, Feb 16, 2007
    #3
  4. 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
     
    Heinrich Mislik, Feb 16, 2007
    #4
  5. 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::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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 16, 2007
    #5
  6. Fred

    Joe Smith Guest

    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
     
    Joe Smith, Feb 18, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    917
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,033
    Smokey Grindel
    Dec 2, 2006
  3. ThunderMusic
    Replies:
    1
    Views:
    156
    ThunderMusic
    Feb 22, 2007
  4. king
    Replies:
    5
    Views:
    189
  5. Ela
    Replies:
    27
    Views:
    238
Loading...

Share This Page