Skip to content

Commit 2ba986a

Browse files
committed
Update Perl to 5.40
1 parent 84cbb50 commit 2ba986a

File tree

12 files changed

+42
-62
lines changed

12 files changed

+42
-62
lines changed

perl/binary_search.pl

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
# A hand coded binary search, to illustrate some Perl features
22

3-
use strict;
4-
use warnings;
5-
use utf8;
6-
use open qw(:std :utf8);
3+
use v5.40;
74

8-
sub binary_search {
9-
# Arguments are passed as the list @_
10-
my ($element_to_find, @ordered_array) = @_;
5+
sub binary_search($element_to_find, @ordered_array) {
116

127
my $array_length = scalar @ordered_array;
138
my $start_index = 0;
@@ -32,10 +27,11 @@ sub binary_search {
3227

3328
# No booleans in Perl, 0 means false
3429
return 0;
35-
}
30+
}
3631

3732
die unless binary_search(3, (1, 3, 5, 7, 9)) == 1;
3833
die unless binary_search(5, (1, 3, 5, 7, 9)) == 1;
3934
die unless binary_search(8, (1, 3, 5, 7, 9)) == 0;
4035
die unless binary_search(5, ()) == 0;
4136
die unless binary_search(5, (5)) == 1;
37+
say "All tests passed";

perl/cafe.pl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# An illustration of hashes and user input
22

3-
use strict;
4-
use warnings;
3+
use v5.40;
54
use utf8;
6-
use open qw(:std :utf8);
75

86
my %drinks_available = (
97
"mocha" => 1,
@@ -18,18 +16,18 @@
1816
"vanilla" => 1
1917
);
2018

21-
print("What drink would you like to order? We have mocha, matcha, and tea.\n");
19+
say "What drink would you like to order? We have mocha, matcha, and tea.";
2220
my $customer_drink = <STDIN>;
2321
chomp($customer_drink);
2422
$customer_drink = lc($customer_drink);
2523

26-
print("Which syrup? We have raspberry, lavender, blueberry, and vanilla.\n");
24+
say "Which syrup? We have raspberry, lavender, blueberry, and vanilla.";
2725
my $customer_syrup = <STDIN>;
2826
chomp($customer_syrup);
29-
$customer_syrup = lc($customer_syrup);
27+
$customer_syrup = lc($customer_syrup);
3028

3129
if ($drinks_available{$customer_drink} and $syrups_available{$customer_syrup}) {
32-
print("One order of $customer_drink with $customer_syrup!\n");
30+
say "One order of $customer_drink with $customer_syrup!";
3331
} else {
34-
print("Unfortunately, we do not have at least one of those items.\n");
32+
say "Unfortunately, we do not have at least one of those items.";
3533
}

perl/guessing_game.pl

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
use strict;
2-
use warnings;
1+
use v5.40;
32

43
my $random_number = int(rand(9)) + 1;
54
my $user_guess = 0;
65

7-
print("Welcome to the guessing game!\n");
8-
print("Type \'exit\' to leave the program\n");
6+
say "Welcome to the guessing game!";
7+
say "Type 'exit' to leave the program";
98

109
while ($user_guess != $random_number and lc($user_guess) ne "exit") {
11-
print("Please enter an integer between 1-10: \n");
12-
10+
say "Please enter an integer between 1-10:";
1311
$user_guess = <STDIN>;
1412
chomp($user_guess);
1513

@@ -18,15 +16,15 @@
1816
}
1917

2018
if ($user_guess == $random_number) {
21-
print("You guessed correctly!\n");
19+
say "You guessed correctly!";
2220
}
2321
elsif ($user_guess < $random_number) {
24-
print("Too low!\n");
22+
say "Too low!";
2523
}
2624
elsif ($user_guess > $random_number) {
27-
print("Too high!\n");
25+
say "Too high!";
2826
}
2927
else {
30-
print("There was an error\n");
28+
say "There was an error";
3129
}
3230
}

perl/hello.pl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use strict;
2-
use warnings;
1+
use v5.40;
32

4-
print "Hello World!\n";
3+
say "Hello World!";

perl/palindrome.pl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
use strict;
2-
use warnings;
1+
use v5.40;
32
use utf8;
4-
use open qw(:std :utf8);
53

6-
sub palindrome {
7-
my ($word) = @_;
4+
sub palindrome($word) {
85
my $reversed = reverse($word);
96
return $word eq $reversed
107
}
@@ -15,3 +12,4 @@ sub palindrome {
1512
die unless palindrome("ab") == 0;
1613
die unless palindrome("aba") == 1;
1714
die unless palindrome("キツツキ") == 1;
15+
say "All tests passed";

perl/permutations.pl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use strict;
2-
use warnings;
1+
use v5.40;
32

4-
sub print_permutations {
5-
my ($a, $n) = @_;
3+
sub print_permutations($a, $n) {
64
if ($n <= 0) {
75
print join("\t", @$a), "\n";
86
} else {

perl/primes.pl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
# command line argument. Each thread writes its value to STDOUT if it is
33
# prime.
44

5-
use strict;
6-
use warnings;
5+
use v5.40;
76
use threads;
87
use threads::shared;
98

109
my $output_lock: shared;
1110

12-
sub print_primes {
13-
my $n = shift;
11+
sub print_primes($n) {
1412
my @checkers;
1513
for my $i (2 .. $n) {
1614
$checkers[$i-2] = threads->new(

perl/ses.pl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use strict;
2-
use warnings;
1+
use v5.40;
32
use List::Util qw(sum0);
43

5-
sub sum_of_even_squares {
6-
sum0 map { $_**2 } grep { $_ % 2 == 0 } @_;
4+
sub sum_of_even_squares(@a) {
5+
sum0 map { $_**2 } grep { $_ % 2 == 0 } @a;
76
}
87

98
die unless sum_of_even_squares() == 0;
@@ -12,4 +11,4 @@ sub sum_of_even_squares {
1211
die unless sum_of_even_squares(1) == 0;
1312
die unless sum_of_even_squares(2) == 4;
1413
die unless sum_of_even_squares(1, 2, 3, 4, 5) == 20;
15-
print "All tests passed\n";
14+
say "All tests passed\n";

perl/summer.pl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# Script illustrating a concurrent list summer.
22

3-
use strict;
4-
use warnings;
3+
use v5.40;
54
use threads;
65

76
# Returns the sum of the values in its first argument which
87
# should be an array reference.
9-
sub sum {
10-
my $array = shift;
8+
sub sum($array) {
119
my $length = scalar @$array;
1210
my $mid = $length/2;
1311

@@ -36,3 +34,4 @@ sub sum {
3634
die unless (sum [100, 200, 300, 400]) == 1000;
3735
die unless (sum []) == 0;
3836
die unless (sum [(7) x 20000]) == 140000;
37+
say "All tests passed";

perl/top_ten_scorers.pl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use strict;
2-
use warnings;
1+
use v5.40;
32
use utf8;
43
use open qw(:std :utf8);
54

0 commit comments

Comments
 (0)