DigitalKitty has asked for the wisdom of the Perl Monks concerning the following question:
Hi all.
In my never-ending quest for perl knowledge, I am teaching myself how to work with objects, classes, packages, etc. The following code is my first such script. I would be most appreciative if you could make some helpful comments / criticisms / etc. regarding how I might improve this fragile foundation.
Thanks,
-Katie.
In addition, if you know of a good OOP book for perl ( aside from the excellent Conway text ), could you post it? My former college professor ( Jerry ), a few other students and I are going to start work on a major perl project this Fall. He hasn't provided us with any other details but I'm already looking forward to it.
In my never-ending quest for perl knowledge, I am teaching myself how to work with objects, classes, packages, etc. The following code is my first such script. I would be most appreciative if you could make some helpful comments / criticisms / etc. regarding how I might improve this fragile foundation.
Thanks,
-Katie.
#!/usr/bin/perl -w #class file. package Food; use strict; sub new { my $meal = { beverage => "Pepsi", entree => "soup", dessert => "ice cream", }; bless( $meal ); return $meal; } sub dessert { my $self = shift(); $self->{ dessert } = shift() if ( @_ ); return $self->{ dessert }; } sub entree { my $self = shift(); $self->{ entree } = shift() if ( @_ ); return $self->{ entree }; } sub beverage { my $self = shift(); $self->{ beverage } = shift() if ( @_ ); return $self->{ beverage }; } sub setMeal { if ( @_ == 4 ) { my $self = shift(); $self->dessert( $_[ 0 ] ); $self->entree( $_[ 1 ] ); $self->beverage( $_[ 2 ] ); } else { print( "Method setMeal requires additional args.\n" ); } } sub display { my $self = shift(); print( $self->dessert ); print( ", " ); print( $self->entree ); print( ", and " ); print( $self->beverage ); } 1;
#!/usr/bin/perl -w use Food; use strict; my $meal = new Food; $meal->setMeal( "iced tea", "salad", "cheesecake" ); print "I would like "; $meal->display(); print " please."; print( "\n" );
In addition, if you know of a good OOP book for perl ( aside from the excellent Conway text ), could you post it? My former college professor ( Jerry ), a few other students and I are going to start work on a major perl project this Fall. He hasn't provided us with any other details but I'm already looking forward to it.
Edit kudra, 2002-08-05 Added a READMORE tag
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Object Oriented Programming in Perl.
by fuzzyping (Chaplain) on Aug 05, 2002 at 06:12 UTC | |
Re: Object Oriented Programming in Perl.
by frankus (Priest) on Aug 05, 2002 at 09:10 UTC | |
Re: Object Oriented Programming in Perl.
by Basilides (Friar) on Aug 05, 2002 at 09:30 UTC | |
by frankus (Priest) on Aug 05, 2002 at 09:42 UTC | |
Re: Object Oriented Programming in Perl.
by fruiture (Curate) on Aug 05, 2002 at 11:24 UTC | |
by aufrank (Pilgrim) on Aug 05, 2002 at 19:04 UTC |
Back to
Seekers of Perl Wisdom