Exercise on subroutine
hi there,
Am new to perl. been very determined make some lines of code fromRandal l. Swartz work.
It reads:
write a subroutine named greet, that welcomes the person you name by telling them the name of the last person it greets.
like; greet('fred');
greet('Barney');
This sequence of statement should print:
Hi Fred! you are the first one here!
Hi Barney i've seen Fred
Hi Wilma i;ve seen Fred Barney
HI Betty ive seen Fred Barney Wilma.
I have folloed the tips and even implemented defined operator and state but nothing seem to work. Anyone with extensive knowledge of this should please help me with the answer.
perl_learner, show the code that doesn't work
You may wish to try asking this on StackOverflow. Also, show the code you've got so far, and try to be a bit more specific as to what problems you're having - "nothing seems to work" is a bit vague.
That's looks like the exercise from Intermediate Perl, although we use Gilligan's Island characters instead. For our books, the answers to exercises along with their explanations are in one of the appendices.
This is my try:
#!/usr/bin/perl -w
print "who do i greet?\n";
@person = qw / wilma? fred? Barney? Betty? /;
foreach $person(@person) {
$person = "\t$person";
$person .="\n";
}
print "\n @person\n";
print "Pick one to greet now\n";
@greet = ;
greet('$last_person');
sub greet {
state $last_person;
my $name = shift;
print "Hi $name!";
if (defined($last_person)) {
print "$last_person is also here!\n";
}
else {
print "you are the first one here!\n";
}
Plese notice that the subroutine greet was a tip from the chapter 4 exercise of randal's learning perl, 5th edition for