aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-034/dave-cross/perl5/ch-1.pl26
-rw-r--r--challenge-034/dave-cross/perl5/ch-2.pl23
2 files changed, 49 insertions, 0 deletions
diff --git a/challenge-034/dave-cross/perl5/ch-1.pl b/challenge-034/dave-cross/perl5/ch-1.pl
new file mode 100644
index 0000000000..68cd75d97b
--- /dev/null
+++ b/challenge-034/dave-cross/perl5/ch-1.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use feature 'say';
+
+open my $fh, '<', '/etc/passwd' or die $!;
+
+my @users;
+
+while (<$fh>) {
+ chomp;
+ my %user;
+
+ # Lvalue is a hash slice
+ # Rvalue is a list slice
+ @user{qw[user id shell]} = (split /:/)[0, 2, -1];
+
+ push @users, \%user;
+}
+
+for (@users) {
+ # Another hash slice
+ printf "User: %s, Shell: %s\n", @{$_}{qw[ user shell ]};
+}
diff --git a/challenge-034/dave-cross/perl5/ch-2.pl b/challenge-034/dave-cross/perl5/ch-2.pl
new file mode 100644
index 0000000000..94c66313bc
--- /dev/null
+++ b/challenge-034/dave-cross/perl5/ch-2.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use feature 'say';
+
+my %dispatch = (
+ '+' => sub { $_[0] + $_[1] },
+ '-' => sub { $_[0] - $_[1] },
+ 'x' => sub { $_[0] * $_[1] },
+ '/' => sub { $_[0] / $_[1] },
+);
+
+@ARGV == 3 or die "Usage: Num1 Op Num2\n";
+
+my ($x, $op, $y) = @ARGV;
+
+unless ($dispatch{$op}) {
+ die "'$op' is not a supported operator\n";
+}
+
+say "@ARGV is ", $dispatch{$op}->($x, $y);