Re: Run code on object creation after new() and bless?

[email protected] (Stevan Little)
Newsgroups perl.moose
Message-ID <[email protected]>
Tod,

You want the BUILD method, it is documented (kind of) in  
Moose::Object under the BUILDALL method. You can also find an example  
of exactly what you are trying to do in Recipe 4 of the cookbook  
(http://search.cpan.org/~stevan/Moose-0.40/lib/Moose/Cookbook/ 
Recipe4.pod). The Company and Employee classes have a bi-directional  
relation. You may also want to look into the weak_ref => 1 option as  
well so that you don't end up with circular refs.

- Stevan

On Apr 3, 2008, at 9:14 PM, Tod Hagan wrote:
> Hi all,
>
> I've got a container object with a hashref of other objects. Given one
> of the contained objects, the code is going to need to be able to  
> get a
> reference to the container without a brute force search of all
> containers.
>
> The hashref is 'ro' and passed to new as the data structure is  
> fixed and
> not subject to change after creation.
>
> One way to create the backreferences is with a method to be called  
> after
> new returns, but this is subject to bugs if the method call is omitted
> after creating a container. This ends up looking much like a dynamic
> container which starts empty to which items are added via an add  
> method.
>
> I tried creating a base class and extending it using a method modifier
> on 'new', but the problem is that it gets control before the object  
> has
> been blessed and thus there's no reference.
>
> Is there a way to run code following object creation and the  
> blessing of
> the object? Or should I give up trying to hide the extra method  
> call and
> require calling a method following the creation of the object?
>
> Thanks.
>
> Tod
>
> -- 
> Tod Hagan
> Information Technologist
> AIRMAP/Climate Change Research Center
> Institute for the Study of Earth, Oceans, and Space
> University of New Hampshire
> Durham, NH 03824
> Phone: 603-862-3116
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.