in reply to Nested Class::Struct

use strict; use Class::Struct; struct child_t => { type => '$', }; struct parent_t => { type => '$', child => 'child_t', children=>'@', }; #--- For a single child ---- my $Dad = new parent_t(child=> new child_t(type=>"teenager")); print $Dad->child->type() . "\n"; #------ To use Multiple Children --- my $index=0; for (2..6){ $Dad->children($index++, new child_t(type=>"Type $_")); } for (@{ $Dad->children() }){ print "Kid type " . $_->type() . "\n"; }

     Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

Replies are listed 'Best First'.
Re^2: Nested Class::Struct
by a.kumaresh (Initiate) on Aug 29, 2010 at 04:40 UTC
    thanks :) that worked. I was trying to use push and friends. like this push @{Dad->children}, child_t_new();Resulted in undefined object method children error.
      Well - that is the more perlish approach to adding elements to the array.
      This works:
      push @{ $Dad->children }, new child_t(type=>"Flower Child");
      This is closer to the syntax you were attempting (also works):
      push @{ $Dad->children }, child_t::new('child_t','type',"Wild Child") +;
      However, as others have pointed out, Class::Struct is a stepping stone into OO perl programming.
      I abandoned it fairly soon after learning the many more powerful constructs in OO perl.

      I'm still learning (after using perl for 8 years), and heading towards Moose (Which is sort of built-in to perl6).

      Update: The second example above is unnatural - here is a better way:

      push @{ $Dad->children }, child_t->new(type=>"Natural Child");

           Syntactic sugar causes cancer of the semicolon.        --Alan Perlis