in reply to Large datastructure caching

Lemme see if I get this - you're caching your object handles in the @handle array, which contains references back to your blessed hash. This sounds almost like flyweight objects from the book. I don't think converting this to a flyweight object implementation will gain you much speed or anything, but it will gain you some elegance, as you no longer have to pass $id around to your methods.

Basically, a flyweight object is a blessed scalar which contains an index into an array of references to something (sound familiar? :) ). So, instead of passing $id around, why don't you make $id your object by blessing it, and put references the hashes containing {string} keys in an array, indexed by $id. Your constructor can then either take no argument, which creates a new entry in the array, or take an id to give you a handle on the object with that id.

Something like this:

use strict; package Demo; my @handles; sub new { my $thingy=shift; my $args=shift; my $id=$args->{'id'}; # Note that $args->{'id'} is autovivified here, but # this is no problem as we're testing for definedness # further on my $class=ref($thingy)||$thingy; unless (defined $id) {$id=scalar @handles}; my $self=bless $id, $class; $handle[$self]={string => $args->{'string'}}; return $self; }
Note that this code is untested, and that I used hash style argument passing (new({id=>11, string=>"camel"})) in the constructor to account for the optional presence of id.

I also have no idea how this plays with SOAP, as I have no experience on that matter.

Your accessor functions then do no have to have $id passed to them, they can just use $self. This will gain you a minor improvement in elegance, for what it's worth :)

CU
Robartes-

Replies are listed 'Best First'.
Re: Re: Large datastructure caching
by rdfield (Priest) on Nov 18, 2002 at 10:01 UTC
    After a bit of work (not too much, this is Perl after all) just a couple of small changes to your code were required to make it work:
    my $self=bless $id, $class;
    becomes
    my $self=bless \$id, $class;
    and all references to $self as indicies to the array of handles become $$self. The calling code looks much more elegant and the internal calls are a bit better too.

    rdfield

    Update: I've simplified the internal calls a bit further...$self = $handles[${shift()}];and the rest of the code remains unchanged

    Update 2: Blessed Scalar references are not handled very well in the serialisation of SOAP transactions, so I've had to kludge the code as follows:
    Constructor:

    my $self = bless {id => $id},$class;
    Method:
    my $self = $handles[shift()->{id}];
Re: Re: Large datastructure caching
by rdfield (Priest) on Nov 16, 2002 at 15:14 UTC
    A blessed scalar - but of course!

    Elegance achieved, cheers robartes++

    rdfield