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 # Modify the previous program to tell each new person the names of all the people it has previously greeted:
5 ffd9a51f 2023-08-04 jrmu
6 ffd9a51f 2023-08-04 jrmu use v5.10;
7 ffd9a51f 2023-08-04 jrmu use warnings;
8 ffd9a51f 2023-08-04 jrmu use strict;
9 ffd9a51f 2023-08-04 jrmu use utf8;
10 ffd9a51f 2023-08-04 jrmu
11 ffd9a51f 2023-08-04 jrmu sub greet {
12 ffd9a51f 2023-08-04 jrmu state @seen;
13 ffd9a51f 2023-08-04 jrmu if (@seen) {
14 ffd9a51f 2023-08-04 jrmu print "Hi $_[0]! I've seen: @seen\n";
15 ffd9a51f 2023-08-04 jrmu } else {
16 ffd9a51f 2023-08-04 jrmu print "Hi $_[0]! You are the first one here!\n";
17 ffd9a51f 2023-08-04 jrmu }
18 ffd9a51f 2023-08-04 jrmu push @seen, $_[0];
19 ffd9a51f 2023-08-04 jrmu }
20 ffd9a51f 2023-08-04 jrmu
21 ffd9a51f 2023-08-04 jrmu greet("Fred");
22 ffd9a51f 2023-08-04 jrmu greet("Barney");
23 ffd9a51f 2023-08-04 jrmu greet("Wilma");
24 ffd9a51f 2023-08-04 jrmu greet("Betty");
25 ffd9a51f 2023-08-04 jrmu
26 ffd9a51f 2023-08-04 jrmu #This sequence of statements should print:
27 ffd9a51f 2023-08-04 jrmu
28 ffd9a51f 2023-08-04 jrmu #Hi Fred! You are the first one here!
29 ffd9a51f 2023-08-04 jrmu #Hi Barney! I've seen: Fred
30 ffd9a51f 2023-08-04 jrmu #Hi Wilma! I've seen: Fred Barney
31 ffd9a51f 2023-08-04 jrmu #Hi Betty! I've seen: Fred Barney Wilma