Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > package and "global" vars

Reply
Thread Tools

package and "global" vars

 
 
okey
Guest
Posts: n/a
 
      02-04-2009
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;
}
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-04-2009
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
 
Reply With Quote
 
 
 
 
Dave Weaver
Guest
Posts: n/a
 
      02-04-2009
On Wed, 4 Feb 2009 05:21:18 -0800 (PST), okey <(E-Mail Removed)> 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'; }



 
Reply With Quote
 
okey
Guest
Posts: n/a
 
      02-04-2009
Thank you much.
 
Reply With Quote
 
okey
Guest
Posts: n/a
 
      02-04-2009

> *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.
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-04-2009
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
 
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
Use self.vars in class.method(parameters, self.vars) caccolangrifata Python 18 07-22-2011 10:22 PM
How do I declare global vars or class vars in Python ? Linuxguy123 Python 7 02-20-2009 06:45 PM
single package import v/s the entire package Parvinder Java 6 02-27-2005 02:02 PM
app vars and cache vars Jon ASP .Net 3 12-14-2004 08:52 PM
Importing a package and looping through modules in the package Dave Python 2 02-10-2004 08:14 PM



Advertisments