package and "global" vars

O

okey

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;
}
 
G

Gunnar Hjalmarsson

okey said:
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.
 
D

Dave Weaver

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'; }
 
O

okey

 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.
 
G

Gunnar Hjalmarsson

okey said:
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();
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top