shemp has asked for the wisdom of the Perl Monks concerning the following question:

Ive been having trouble embedding Class::Struct as the type of an element of another Class::Struct Following the example in The Perl Cookbook section 13.5 "Using Classes as Structs", i've created the following code:
#!/usr/bin/perl -w use Class::Struct; struct Person => { name => '$', age => '$', }; struct Family => { head => 'Person', address => '$', members => '@', }; { my $folks = Family->new(); my $dad = $folks->head(); print "dad = $dad , its a " . ref($dad) . "\n"; $dad->name("John"); $dad->age(34); }
Now, the output from this program is:
Use of uninitialized value in concatenation (.) or string at cook_test.pl line 21.
dad = , its a
Can't call method "name" on an undefined value at cook_test.pl line 23.


shouldn't ref($dad) say "Person" ?
it appears that $folks->head() is returning an unitialized scalar.
BTW i'm running Perl 5.8.0 for sun4-solaris
any insight into what im missing would be great.
related: anybody know if Class:StructTemplate has evolved at all. the CPAN documentation is from 12-26-2000, and i do want to serialize structs into MySQL
thanks much

Replies are listed 'Best First'.
Re: Embedded Class::Struct Misbehaviour
by shemp (Deacon) on Nov 06, 2002 at 18:30 UTC
    Alright, i figured out the problem. The Perl 5.8.0 distro includes Struct.pm v0.61 which doesnt work right. I copied v0.59 from another box i had and it worked.
Re: Embedded Class::Struct Misbehaviour
by jdporter (Paladin) on Nov 06, 2002 at 18:34 UTC
    shouldn't ref($dad) say "Person" ?
    Not automatically. As the example in the Class::Struct doc shows, you need to initialize the field. Class::Struct enforces that it's the right type, but doesn't create the variable for you. You can do this:
    $folks->head( new Person .... );
    to initialize the field.
      Actually, with the code from the original post, with Class::Struct 0.59, the print statement does report that $dad is a 'Person'.

      I'm new to Class::Struct, (should've started using it years ago), and don't exactly know what the proper behaviour is supposed to be, just writing little test progs to figure it out. I love it though.

      thanks