smile4me has asked for the wisdom of the Perl Monks concerning the following question:
Here's a cheesy (but accurate) example of the technique I am talking about:
#!/usr/bin/perl -w use Data::Dumper; use strict; package Apple; sub new { my $obj = shift; my %self = ( color => 'red' ); bless \%self, $obj; } sub serve { return 'sliced'; } package Orange; sub new { my $obj = shift; my %self = ( color => 'orange' ); if ( @_ ) { $self{apple} = shift; ## other objects $self{shape} = shift; } bless \%self, $obj; } sub serve { my $self = shift; return ( $self->{apple} ) ? $self->{apple}->serve() : 'squeezed'; } package main; my $red = new Apple; my $yel = new Apple; $yel->{color} ='yellow'; ## or is it better to pass to the new() for +initialization. my $foo = new Orange(); my $baz = new Orange( $red ); ## is it wise to pass an object ref vs. + "use'ing" the package for my $obj ( $red, $yel, $foo, $baz ) { print 'served: ', $obj->serve, "\n"; } print "\n fruit stand: \n" , Dumper($red) , Dumper($yel) , Dumper($foo) , Dumper($baz) , "\n";
Thanks for any guidance or recommendations!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl OO best practice?
by ikegami (Patriarch) on Feb 08, 2010 at 20:17 UTC | |
by smile4me (Beadle) on Feb 09, 2010 at 20:07 UTC | |
by ikegami (Patriarch) on Feb 09, 2010 at 21:18 UTC | |
|
Re: Perl OO best practice?
by Fletch (Bishop) on Feb 08, 2010 at 20:31 UTC | |
by smile4me (Beadle) on Feb 09, 2010 at 20:08 UTC | |
|
Re: Perl OO best practice?
by chromatic (Archbishop) on Feb 08, 2010 at 20:53 UTC | |
|
Re: Perl OO best practice?
by repellent (Priest) on Feb 09, 2010 at 07:27 UTC | |
by TGI (Parson) on Feb 12, 2010 at 01:38 UTC | |
by smile4me (Beadle) on Feb 09, 2010 at 20:25 UTC | |
by repellent (Priest) on Feb 10, 2010 at 01:14 UTC |