Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: OO problems

by perlmonkey (Hermit)
on Jul 24, 2000 at 01:28 UTC ( [id://24016]=note: print w/replies, xml ) Need Help??


in reply to OO problems

Perhaps I dont understand what you are doing, but this might be what you want:
my $foo = new Foo; $foo->add_item("Cheese"); $foo->add_item("Mice"); $foo->add_item("Rat", "Cat"); use Data::Dumper; print Data::Dumper->Dump([$foo], ['foo']); package Foo; sub new { bless { _orderid => '', _items => [] }, $_[0]; } sub add_item { push @{ $_[0]->{_items} }, @_[1 .. $#_]; }
Results
$foo = bless( { '_orderid' => '', '_items' => [ 'Cheese', 'Mice', 'Rat', 'Cat' ] }, 'Foo' );

Replies are listed 'Best First'.
RE: Re: OO problems
by steveAZ98 (Monk) on Jul 24, 2000 at 02:20 UTC
    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; }
      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!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 08:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found