Object IO (Typedef problem)

Tkil tkil at scrye.com
Mon Jul 22 19:09:26 CDT 2002

>>>>> "Jason" == Jason Annin-White <jasona at inetarena.com> writes:

Jason> I am still pretty much in the same boat however.  I can't find
Jason> any perl documentation on how to typedef a reference to a
Jason> custom object.

"typedef" is a concept from C, and there's no particular correlate in
Perl (that I can think of, anyway).  Perhaps you mean "typecast"?

Jason> I have a package which I've named PDX::service. When I store
Jason> and retrieve an instance of PDX::service I am left with a
Jason> reference.  I know how to typedef a reference to a scalar,
Jason> array, or hash, however, I do no know how to typedef the
Jason> reference to be of type PDX::service.

Um ... it already is (or should be) a reference to a PDX::service
object; why do you need to cast it further?

Jason> ref($objectref) returns main.
Jason> ref($$objectref) returns PDX::service

Ah.  It looks like you tried to freeze it using something like:

   my $serialized = freeze \$obj;

Which freezes a reference to a reference, and that's what you get
back.  Since you already have a reference, you don't need the extra
level of indirection; just freeze $obj itself.

Jason> This makes me think that THIS is the appropriate way to access
Jason> this object:

Jason>    PDX::service$$objref->method()

I think that ($$objref)->method() should work... let me try it.  Yup:

| #!/usr/bin/perl -w
| use strict;
| use Storable qw( freeze thaw );
| # ----------------------------------------------------------------------
| package Foo;
| sub new
| {
|     my ($package, $info) = @_;
|     return bless { info => $info };
| }
| sub babble
| {
|     my ($self, $label) = @_;
|     print "$label: I'm a " . ref($self) . ", and I know $self->{info}\n";
| }
| # ----------------------------------------------------------------------
| package main;
| my $foo1 = Foo->new("eeba deeba!");
| $foo1->babble('$foo1');
| my $stored_foo1 = freeze $foo1;
| my $foo2 = thaw $stored_foo1;
| $foo2->babble('$foo2');
| my $stored_foo_ref = freeze \$foo1;
| my $foo3 = thaw $stored_foo_ref;
| print 'ref($foo3)=', ref($foo3), "\n";
| print 'ref($$foo3)=', ref($$foo3), "\n";
| ($$foo3)->babble('$$foo3');
| exit 0;

Which gives us:

| $ ./jason1
| $foo1: I'm a Foo, and I know eeba deeba!
| $foo2: I'm a Foo, and I know eeba deeba!
| ref($foo3)=REF
| ref($$foo3)=Foo
| $$foo3: I'm a Foo, and I know eeba deeba!

Jason> This returns no errors, like my other attempts did, however, it
Jason> retuned no data either.  Does retrive() is it possible that my
Jason> retrieved object has members but no methods and doesn't "truly"
Jason> belong to type PDX::service?

What you got back was a reference to an object.  If you dereference it
once, you have just the object, and you can do normal method calls on

Does this clear it up?



More information about the Pdx-pm-list mailing list