How to initialize class data?

R

Ralph Moritz

Hi,

I've got some class data which I want to initialize once.
I was thinking of doing it like this:

{ package Foo;

my $FirstTime = 1;

sub new {
my $class = shift;
my $self = {};
bless $self, $class;
$class->init() if ($FirstTime);
}

sub init {
# Initialize class data...
$FirstTime = 0;
}
}

Is this okay, or is there a generally accepted way
to do this?

Thanks,
Ralph
 
P

Paul Lalli

Ralph said:
I've got some class data which I want to initialize once.
I was thinking of doing it like this:

{ package Foo;

my $FirstTime = 1;

sub new {
my $class = shift;
my $self = {};
bless $self, $class;
$class->init() if ($FirstTime);
}

sub init {
# Initialize class data...
$FirstTime = 0;
}
}

Is this okay, or is there a generally accepted way
to do this?

What do you mean by "class data" in this case? Variables scoped to the
package, that all class-methods and object-methods would have access
to? If that's the case, I have a dumb question - why aren't you just
declaring these variables to be initialized to whatever they need to be
initialized to?

That is, instead of:
{
package Foo;
my $FirstTime = 1;
my $data;

sub new {
#...
initialize_data() if $FirstTime;
}

sub initialize_data {
$data = "Stuff";
$FirstTime = 0;
}
}

why aren't you just doing:

{
package Foo;
my $data = "Stuff";

sub new {
#...
}
}

?

If I've over simplified things so as to remove the point of your
question, please give us a better idea of what it is you're trying to
accomplish.

Paul Lalli
 
B

Brian McCauley

Ralph said:
Hi,

I've got some class data which I want to initialize once.
I was thinking of doing it like this:

{ package Foo;

my $FirstTime = 1;

sub new {
my $class = shift;
my $self = {};
bless $self, $class;
$class->init() if ($FirstTime);
}

sub init {
# Initialize class data...
$FirstTime = 0;
}
}

Is this okay, or is there a generally accepted way
to do this?

The canonical way would be an INIT block.

Or sometimes you'll see people just not put the code in a subroutine at
all.

But both of these will make the initialization code be called even if
Foo is never instanciated.

If you really want to defer until the first object is instanciated then
what you are doing is approximately correct. However you should not be
calling $class->init() you should call Foo->init() or simply init().
With your code if the first Foo object that's instanciated happens to
be a of a subclass you'll call init() in that subclass.

Oh and it's probably more ideomatic to reverse the sense of your flag.

my $initialized;

sub init {
$initialized++; # Slower than =1 but the more common idiom
# etc...
}

sub new {
init unless $initialized;
my $class = shift;
my $self = bless {}, $class;
# etc...
}
 
R

Ralph Moritz

[snip]
That is, instead of:
{
package Foo;
my $FirstTime = 1;
my $data;

sub new {
#...
initialize_data() if $FirstTime;
}

sub initialize_data {
$data = "Stuff";
$FirstTime = 0;
}
}

why aren't you just doing:

{
package Foo;
my $data = "Stuff";

sub new {
#...
}
}

?

Because I need to read the value of $data from a config file.
 
R

Ralph Moritz

[snipped the rest of my original post]
The canonical way would be an INIT block.

Or sometimes you'll see people just not put the code in a subroutine at
all.

But both of these will make the initialization code be called even if
Foo is never instanciated.

If you really want to defer until the first object is instanciated then
what you are doing is approximately correct. However you should not be
calling $class->init() you should call Foo->init() or simply init().
With your code if the first Foo object that's instanciated happens to
be a of a subclass you'll call init() in that subclass.

Oh and it's probably more ideomatic to reverse the sense of your flag.

my $initialized;

sub init {
$initialized++; # Slower than =1 but the more common idiom
# etc...
}

sub new {
init unless $initialized;
my $class = shift;
my $self = bless {}, $class;
# etc...
}

Excellent. Thank you.
 

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,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top