in reply to Object Oriented Programming in Perl.
So for your constructor, you'd have:
I think it's conventional to put an underscore before your object variable names too, eg _beverage.package Food; sub new { my $class = shift; my $self = bless {}, $class; $self->initialize; $self; } sub initialize { my $self = shift; $self->{_beverage} = "Pepsi"; $self->{_entree} = "soup"; $self->{_dessert} = "ice cream"; }
This may look like a bit of a waste of space, but its neatness might become a bit more obvious when you are actually passing initialisation info to the constructor (as opposed to everything starting off as Pepsi, soup, ice cream). Then you'd change the line in new to $self->initialize(@_);, and then have
Call it with something likesub initialize { my $self = shift; $self->{_beverage} = shift; $self->{_entree} = shift; $self->{_dessert} = shift; }
HTHmy $meal = new Food("tea", "sushi", "creme egg");
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Object Oriented Programming in Perl.
by frankus (Priest) on Aug 05, 2002 at 09:42 UTC |