Reblessing yourself is a fairly common thing, although I'm not sure I've seen it used in exactly this way. The more common use for reblessing yourself is if you don't know exactly what type of object you will be retrieving until you've retrieved some information about it (think of a database table containing 'objects' that includes a field describing the type of object).

Just recently in fact I was working on some code that did something along these lines:

package MyApp::Object; use base qw( Class::Accessor::Fast ); use Carp qw( croak ); sub new { my $self = shift::SUPER->new( {} ); $self->load_config_file( shift() ); my $type = join( '::', ref( $self ), $self->config->{ 'type' } || 'General' ); eval "use $type"; if ( $@ ) { croak "Couldn't load $type: $@" } bless( $self, $type ); return $self; }

This worked pretty well for the application I needed it for, and it allowed other code to very simply instantiate new objects without worrying about what type they should be.

The only downside I can see to your plan is that if the two classes have drastically different methods, you will probably find that people are tending to wrap them up in code like this:

if ( $obj->isa( 'Product::Real' ) ) { # do some real stuff } else { # do some potential stuff }

Simply because the differences in the classes causes people to think of them as entirely separate entities, and if people are doing this anyway, does $obj->isa( 'Product::Real' ) really save you anything over $obj->is_real?


We're not surrounded, we're in a target-rich environment!

In reply to Re: Reblessing yourself by jasonk
in thread Reblessing yourself by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.