|
Prev: Passing object from subroutine problem
Next: FAQ 2.6 What modules and extensions are available for Perl? What is CPAN? What does CPAN/src/... mean?
From: Ralph Moritz on 27 Sep 2006 10:34 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 -- Ralph Moritz Quantum Solutions Ph: +27 315 629 557 GPG Public Key: http://ralphm.info/public.gpg
From: Paul Lalli on 27 Sep 2006 10:39 Ralph Moritz wrote: > 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
From: Brian McCauley on 27 Sep 2006 12:41 Ralph Moritz wrote: > 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... }
From: Ralph Moritz on 27 Sep 2006 15:38 "Paul Lalli" <mritty(a)gmail.com> writes: [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. -- Ralph Moritz Ph: +27 846 269 070 GPG Public Key: http://ralphm.info/public.gpg "Faith is believing something you know ain't true."
From: Ralph Moritz on 27 Sep 2006 15:51
"Brian McCauley" <nobull67(a)gmail.com> writes: > Ralph Moritz wrote: >> Hi, >> >> I've got some class data which I want to initialize once. [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. -- Ralph Moritz Ph: +27 846 269 070 GPG Public Key: http://ralphm.info/public.gpg "Faith is believing something you know ain't true." |