Blame


1 ffd9a51f 2023-08-04 jrmu #!/usr/bin/perl
2 ffd9a51f 2023-08-04 jrmu
3 ffd9a51f 2023-08-04 jrmu #Write a subroutine named greet that welcomes the person you name by telling them the name of the last person it greeted:
4 ffd9a51f 2023-08-04 jrmu
5 ffd9a51f 2023-08-04 jrmu use v5.10;
6 ffd9a51f 2023-08-04 jrmu use warnings;
7 ffd9a51f 2023-08-04 jrmu use strict;
8 ffd9a51f 2023-08-04 jrmu use utf8;
9 ffd9a51f 2023-08-04 jrmu
10 ffd9a51f 2023-08-04 jrmu sub greet {
11 ffd9a51f 2023-08-04 jrmu state $lastperson;
12 ffd9a51f 2023-08-04 jrmu if (!defined($lastperson)) {
13 ffd9a51f 2023-08-04 jrmu print "Hi $_[0]! You are the first one here!\n";
14 ffd9a51f 2023-08-04 jrmu } else {
15 ffd9a51f 2023-08-04 jrmu print "Hi $_[0]! $lastperson is also here!\n";
16 ffd9a51f 2023-08-04 jrmu }
17 ffd9a51f 2023-08-04 jrmu $lastperson = $_[0];
18 ffd9a51f 2023-08-04 jrmu }
19 ffd9a51f 2023-08-04 jrmu
20 ffd9a51f 2023-08-04 jrmu greet("Fred");
21 ffd9a51f 2023-08-04 jrmu greet("Barney");
22 ffd9a51f 2023-08-04 jrmu
23 ffd9a51f 2023-08-04 jrmu #This sequence of statements should print:
24 ffd9a51f 2023-08-04 jrmu
25 ffd9a51f 2023-08-04 jrmu #Hi Fred! You are the first one here!
26 ffd9a51f 2023-08-04 jrmu #Hi Barney! Fred is also here!
27 ffd9a51f 2023-08-04 jrmu
28 ffd9a51f 2023-08-04 jrmu # Modify the previous program to tell each new person the names of all the people it has previously greeted:
29 ffd9a51f 2023-08-04 jrmu
30 ffd9a51f 2023-08-04 jrmu greet("Fred"):
31 ffd9a51f 2023-08-04 jrmu greet("Barney"):
32 ffd9a51f 2023-08-04 jrmu greet("Wilma");
33 ffd9a51f 2023-08-04 jrmu greet("Betty"):