package and "global" vars

Discussion in 'Perl Misc' started by okey, Feb 4, 2009.

  1. okey

    okey Guest

    I have a package midway in a perl script. I that package there is a
    global at the top called varTwo. No matter what I do, I can access
    within the package. Why doesn't the below work? Also tried my, our
    and no package name at all.
    Thanks.


    #================================== begin file here
    my $varOne = 'This is var 1.';
    print qq{This is var one: $varOne\n}; # prints fine

    my $p = new MYPACKAGE();

    #=== a package
    package MYPACKAGE;

    $MYPACKAGE::varTwo = 'This is var two';

    sub new {
    my $class = shift;
    my $self = {};
    bless($self, $class);
    my $t = $FU::varTwo;
    print qq{Inside packet, var two is:$MYPACKAGE::varTwo\n}; #
    prints, but no value
    return $self;
    }
     
    okey, Feb 4, 2009
    #1
    1. Advertising

  2. okey wrote:
    > I have a package midway in a perl script. I that package there is a
    > global at the top called varTwo. No matter what I do, I can access
    > within the package. Why doesn't the below work? Also tried my, our
    > and no package name at all.
    > Thanks.
    >
    >
    > #================================== begin file here
    > my $varOne = 'This is var 1.';
    > print qq{This is var one: $varOne\n}; # prints fine
    >
    > my $p = new MYPACKAGE();
    >
    > #=== a package
    > package MYPACKAGE;
    >
    > $MYPACKAGE::varTwo = 'This is var two';
    >
    > sub new {
    > my $class = shift;
    > my $self = {};
    > bless($self, $class);
    > my $t = $FU::varTwo;
    > print qq{Inside packet, var two is:$MYPACKAGE::varTwo\n}; #
    > prints, but no value
    > return $self;
    > }


    Because assignment happens at runtime, and you create the MYPACKAGE
    object before the assignment of $MYPACKAGE::varTwo.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 4, 2009
    #2
    1. Advertising

  3. okey

    Dave Weaver Guest

    On Wed, 4 Feb 2009 05:21:18 -0800 (PST), okey <> wrote:
    > I have a package midway in a perl script. I that package there is a
    > global at the top called varTwo. No matter what I do, I can access
    > within the package. Why doesn't the below work? Also tried my, our


    It *does* "work", it just doesn't do what you're expecting.

    > and no package name at all.
    > Thanks.


    perl runs the code in order...

    > #================================== begin file here
    > my $varOne = 'This is var 1.';
    > print qq{This is var one: $varOne\n}; # prints fine


    So first it runs this:

    > my $p = new MYPACKAGE();


    which calls new(), which prints the current value of varTwo (i.e.
    undef).

    > #=== a package
    > package MYPACKAGE;


    and *then* it gets to this line where it sets the value of varTwo.

    > $MYPACKAGE::varTwo = 'This is var two';


    One solution to your problem is to wrap the initialisation in
    a BEGIN{} block, i.e:

    BEGIN { $MYPACKAGE::varTwo = 'This is var two'; }
     
    Dave Weaver, Feb 4, 2009
    #3
  4. okey

    okey Guest

    Thank you much.
     
    okey, Feb 4, 2009
    #4
  5. okey

    okey Guest


    >  BEGIN {  $MYPACKAGE::varTwo = 'This is var two'; }


    Is there a way to initialize all these types of "globals" in a single
    step.
    I suspect one way would be to move the package to the top.
     
    okey, Feb 4, 2009
    #5
  6. okey wrote:
    >> BEGIN { $MYPACKAGE::varTwo = 'This is var two'; }

    >
    > Is there a way to initialize all these types of "globals" in a single
    > step.
    > I suspect one way would be to move the package to the top.


    First you ought to ask yourself if that's really what you want to do. If
    you put "package MYPACKAGE" in the module MYPACKAGE.pm, and use or
    require it, the assignment takes place before the next step in your main
    program.

    use MYPACKAGE;
    my $p = new MYPACKAGE();

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 4, 2009
    #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. Replies:
    1
    Views:
    412
  2. Jon

    app vars and cache vars

    Jon, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    425
  3. George P
    Replies:
    3
    Views:
    707
    Alex Martelli
    Sep 11, 2004
  4. Linuxguy123
    Replies:
    7
    Views:
    731
    Paddy O'Loughlin
    Feb 20, 2009
  5. caccolangrifata
    Replies:
    18
    Views:
    428
    Chris Torek
    Jul 22, 2011
Loading...

Share This Page