in reply to Re: OO problems
in thread OO problems

Yeah, that's pretty much what I'm doing, and it works when I do it like that. I tried writing a minimal example to get the error and the code works. I'll have to re-examine my code, must be something wrong going on in there.
Thanks
One more note. Is it legal to tie a hash using Apache::Session and then bless that hash into an object. This is what I'm doing and maybe that is the cause of my problem?
sub new { my $class = shift; my ($id) = shift; my $hr = {}; tie %{$hr}, 'Apache::Session::MySQL', $id { etc... }; my $self = bless $hr,$class; return $self; }

Replies are listed 'Best First'.
RE: RE: Re: OO problems
by chromatic (Archbishop) on Jul 24, 2000 at 06:18 UTC
    According to Damian Conway (master of most things related to bless and a whole lot of things that aren't), it's legal to bless a tied hash into an object.

    However, your method doesn't look quite right. You create an anonymous hash, assign its reference to a scalar, and then dereference and bless it. In theory, that should work, but you'll have to do two layers of indirection when trying to get at the tied hash. I think something like the following is more in line with what you want. (Disclaimer: I haven't tried this, and I've never used Apache::Session, but it's a little cleaner.)

    sub new { my $class = shift; my $id = shift; my %h = (); tie %h, 'Apache::Session::MySQL', $id { # whatever goes here }; return bless(\%h, $class); }
      Thanks, I see what your saying as far as that code goes. It actually acts the same way, but is probably better the way you wrote it.
      I've written a smaller version for debugging and it seems that the problem is in getting Apache::Session to store the lower level data structures before the object goes away. I timestamp it on creation and in DESTROY before I untie it, but it still isn't working for me.
      I'll keep playing and let you know if I figure anything out. It seems to me to be a error on my part of the implementation of Apache::Session, I just have to figure out what it is! :)
      Thanks Again.
      I discovered my problem was in calling new() on an object that already existed in the tied hash and thus overwrote the stored information. I fixed it with the following very simple code. Damn I've been banging my head over this one. Feel so stupid! :)

      sub set_items { $_[0]->{_items} = $_[0]->{_items} || Order->new() }
      Thanks again everyone!