develooper Front page | perl.beginners | Postings from March 2002

tie/bless interactions?

Thread Next
Dave Storrs
March 13, 2002 15:55
tie/bless interactions?
Message ID:

I've got a class called APC::Event.  The idea is that when you instantiate
one of these objects, you are signalling that a particular event has
happened; ergo, it takes the information you specified, deduces a bunch
more stuff, and logs all of it to a database.  Afterwards it sticks around
and provides an interface to that record (or the whole table, actually).

I've just discovered the Tie::DBI modules, and they seem like exactly what
I want here, but I'm wondering if the following is going to get me in

sub new {
    my ($proto, %args) = @_;

    my %self;
    my $database_handle =    $args{ dbh }
                          || DBH(\%default_database_params,
                                 {PrintError => 0, RaiseError => 1}
    tie %self, Tie::DBI, { db       => $database_handle,
                           CLOBBER  => 1,   #  Allow INSERT and UPDATE,
                                            #  but not DELETE

    my $class =    ref $proto    # Are we being called as a class or
                || $proto        # instance method?
                || 'APC::Event';

    bless \%self, $class;

        I haven't done that much with tie in the past, and I've never done
anything where I tied an object and then blessed it, so I'm not quite sure
what the implications are.  Specifically,

1) As I understand it, tie() uses bless() under the hood; is this correct?

2) I want the object to be an APC::Event, because there is more in the
class than just a constructor.  Assuming that the answer to question 1 is
"yes", what happens when I rebless a reference?  I believe it "forgets"
all about its original class, but I've never done it before (actually,
I've tried very hard to avoid it).

3) If I bless %self out of the "Tie::Hash" space and into the "APC::Event"
space, are the "Tie::Hash" functions going to stop working?  (I assume

4) If the answer to 3 is "yes", can I solve this by doing this:
        my $rh_self = \%self;
        unshift @{$rh_self->ISA}, 'APC::Event';

5) Given the above constructor code, am I likely to have any problems with
circular references?

Thanks in advance,

Dave Storrs

PS  If you wanted to rewrite the code snippet in question 4 without taking
a reference, how would you do it?  This doesn't seem right:

	unshift @self{ISA}, 'APC::Event';

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About