We don't bite newbies here... much PerlMonks

### Re: Beginner's Object Orientation Exercises?

by munchie (Monk)
 on Mar 20, 2002 at 01:14 UTC ( #152897=note: print w/replies, xml ) Need Help??

in reply to Beginner's Object Orientation Exercises?

Ok, here's TRY 2. I'm almost 100% percent sure this is real object orientation. It's a package called AlienFarm that can make, teach, and give powers to aliens. (Constructed using the birth method.)

```#! Perl
use strict;

{
package AlienFarm;

sub birth {
my \$self=shift;
my (\$sec,\$min,\$hour,\$mday,\$mon,\$year,\$wday,\$yday,\$isdst)=localtime
+(time);
my \$name=shift;
\$mon+=1;
\$year+=1900;
my \$birth = "\$mon \$mday, \$year";
my \$alien = bless {
NAME => "\$name",
BIRTH => "\$birth",
POWERS => [ ],
"12th grade", "Harvard Freshman", "Harvard Sophmo
+re",
"Havard Junior", "Harvard Senior", "Employed", "C
+EO"],
LEVEL => "-1" }, "AlienFarm";
}
sub birthday {
my \$decide=shift;
ref \$decide ? \$decide->{BIRTH} : "an unknown time";
}
sub name {
my \$decide=shift;
ref \$decide ? \$decide->{NAME} : "an unknown name";
}
sub set_power {
my \$self=shift;
my \$power=shift;
push @{\$self->{POWERS}}, \$power;
}
sub get_powers {
my \$self=shift;
my \$num=1;
foreach my \$power ( @{\$self->{POWERS}} ) {
print "\$num) \$power\n";
\$num++;
}
print "\n";
}
sub teach_lessons {
my \$self=shift;
my \$level = \$self->{LEVEL};
\$level+=1;
\$self->{LEVEL} = \$level;
return "Lesson taught. Level: ". \$self->{BRAINS}[\$self->{LEVEL}] .
+"\n";

}
sub schooling {
my \$self = shift;
return \$self->{BRAINS}[\$self->{LEVEL}];
}
}

> munchie, the number munchin newb
Llama: The other other white meat!
(you had to be there :-P)

Replies are listed 'Best First'.
Re: Re: Beginner's Object Orientation Exercises?
by Necos (Friar) on Mar 21, 2002 at 00:45 UTC
Here's a slightly modified version of your AlienFarm module... There are some subtle differences... Can you see what they are?
```#!/usr/bin/perl -w
package AlienFarm;
use strict;

sub birth {
my \$pkg = shift;
my (\$sec,\$min,\$hour,\$mday,\$mon,\$year,\$wday,\$yday,\$isdst)=localtime
+(time);
my \$name = shift;
\$mon++;
\$year+=1900;
my \$birth = "\$mon \$mday, \$year";
my \$alien = bless {
NAME => "\$name",
BIRTH => "\$birth",
POWERS => [ ],
"12th grade", "Harvard Freshman", "Harvard Sophmo
+re",
"Havard Junior", "Harvard Senior", "Employed", "C
+EO"],
LEVEL => "-1" }, \$pkg;
}
sub birthday {
my \$decide=shift;
ref \$decide ? \$decide->{BIRTH} : "an unknown time";
}
sub name {
my \$decide=shift;
ref \$decide ? \$decide->{NAME} : "an unknown name";
}
sub set_power {
my \$self=shift;
my \$power=shift;
push @{\$self->{POWERS}}, \$power;
}
sub get_powers {
my \$self=shift;
my \$num=1;
foreach my \$power ( @{\$self->{POWERS}} ) {
print "\$num => \$power\n";
\$num++;
}
print "\n";
}
sub teach_lessons {
my \$self = shift;
\$self->{LEVEL}++;
return "Lesson taught. Level: ". \$self->{BRAINS}[\$self->{LEVEL}] .
+"\n";

}
sub schooling {
my \$self = shift;
return \$self->{BRAINS}[\$self->{LEVEL}];
}

Also, you can actually combine set_power and get_power into one sub, called power. But that's an exercise left for the reader...

Theodore Charles III
Los Angeles Senior High
4650 W. Olympic Blvd.
Los Angeles, CA 90019
323-937-3210 ext. 224
email->secon_kun@hotmail.com
I think that I could change
```sub set_power {
my \$self=shift;
my \$power=shift;
push @{\$self->{POWERS}}, \$power;
}
sub get_powers {
my \$self=shift;
my \$num=1;
foreach my \$power ( @{\$self->{POWERS}} ) {
print "\$num => \$power\n";
\$num++;
}
print "\n";
}
into something like:
```sub power {
my \$self=shift;
if (\$_[0]) {
my \$power=shift;
push @{\$self->{POWERS}}, \$power;
} else {
my \$self=shift;
my \$num=1;
foreach my \$power ( @{\$self->{POWERS}} ) {
print "\$num => \$power\n";
\$num++;
}
print "\n";
}
}
It would be called as \$obj->power("Power name") to set, and \$obj->power() to view.

> munchie, the number munchin newb
Llama: The other other white meat!
(you had to be there :-P)

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://152897]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2023-03-24 06:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Which type of climate do you prefer to live in?

Results (60 votes). Check out past polls.

Notices?