Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: blessed confusion

by chromatic (Archbishop)
on Sep 08, 2010 at 23:51 UTC ( #859442=note: print w/ replies, xml ) Need Help??


in reply to blessed confusion

Moose (I know, but hold on) separates object creation from object initialization. When you construct a new Moose object, its constructor calls for you a method (BUILDALL, I believe but haven't confirmed before posting) which calls every BUILD method in the appropriate class hierarchy. Each BUILD method performs the appropriate initialization.

bless only occurs once.

If you can't migrate to Moose right now, you could do something similar. Move the responsibility for blessing an object into the parentmost new and create an initialize() method in each class where that's useful. Within new, after blessing, call initialize. Be sure to call the parent implementation:

sub initialize { my $self = shift; $self->SUPER::initialize( @_ ); ... }


Comment on Re: blessed confusion
Select or Download Code
Re^2: blessed confusion
by perl-diddler (Hermit) on Sep 09, 2010 at 02:42 UTC
    Wait, wait, wait...'bless only once'...?? Um...I don't think so...

    Each object's creation code must return a blessed reference to indicate that the returned 'object' is now part of that class (has been blessed into that class) -- meaning that the reference can be now called on any method in the new class.

    W/o the blessing @ each level, you couldn't use the reference to call subclass methods, as it's not a pointer of any class (yet), until it's done being initialized into that class.

    So something like 'cacheable' wouldn't be able to use the 'path' method of 'url' to derive a cacheable's path. Example:

    package url (methods:new 'host' 'path') package url::cacheable sub new { $package=shift; $up=new url(@_); # host & path passed in args $up->path($cpath) if ($cpath=_canonical_path($up->path)) ne $path; bless $up, $package; }
    So when $up comes back from 'new url', it's a ref to a url-blessed object and is used as such. But not until the path of the 'url' object has been tested as (and possibly set to) '_canonical_path', is it suitable to be blessed as a 'cachceable' object.

    Am I missing something?

      W/o the blessing @ each level, you couldn't use the reference to call subclass methods,

      No, only the subclass's blessing is required. In fact, all the other blessings are removed by it. chromatic is simply suggesting that you bless it correctly the first time instead of correcting the incorrect blessing with a second blessing.

      Am I missing something?

      Inside Perl 5, a reference only has one slot to associate it with a class. You can replace that association by blessing the reference into a different class, but there's only ever one class associated with a reference.

      I don't know what object system you're thinking of, but this is how Perl 5 has worked since the beginning. Test it and see:

      package Grandkid; use parent 'Kid'; sub new { bless {}, shift } package Kid; use parent 'Parent'; package Parent; sub inherited_method { 'Yep, inherited!' } package main; use Test::More 'no_plan'; my $gkid = Grandkid->new(); isa_ok( $gkid, 'Grandkid' ); isa_ok( $gkid, 'Kid' ); isa_ok( $gkid, 'Parent' ); is( $gkid->inherited_method(), 'Yep, inherited!', 'method inherited fr +om grandparent' ); done_testing();

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://859442]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-09-03 08:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (35 votes), past polls