aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-028/feng-chang/perl5/ch-1.pl30
-rwxr-xr-xchallenge-028/feng-chang/perl5/ch-2.pl17
-rwxr-xr-xchallenge-028/feng-chang/perl6/ch-1.p610
-rwxr-xr-xchallenge-028/feng-chang/perl6/ch-2.p68
4 files changed, 65 insertions, 0 deletions
diff --git a/challenge-028/feng-chang/perl5/ch-1.pl b/challenge-028/feng-chang/perl5/ch-1.pl
new file mode 100755
index 0000000000..afc32c598b
--- /dev/null
+++ b/challenge-028/feng-chang/perl5/ch-1.pl
@@ -0,0 +1,30 @@
+#!/bin/env perl
+
+use Modern::Perl;
+
+die "usage: $0 <file-name>\n" unless @ARGV == 1;
+
+open(my $fh, '<', $ARGV[0]) or die "cannot open $ARGV[0] for read\n";
+my $buf;
+read $fh, $buf, 16;
+
+my $be_ascii = 1;
+for my $c (split //, $buf) {
+ unless (is_ascii($c)) {
+ $be_ascii = 0;
+ last;
+ }
+}
+
+print 'the file content is ';
+say $be_ascii ? 'ascii' : 'binary';
+
+close $fh;
+
+sub is_ascii {
+ my $c = ord(shift);
+
+ return 1 if 9 <= $c and $c <= 13;
+ return 1 if 32 <= $c and $c <= 126;
+ return 0;
+}
diff --git a/challenge-028/feng-chang/perl5/ch-2.pl b/challenge-028/feng-chang/perl5/ch-2.pl
new file mode 100755
index 0000000000..1633cfb362
--- /dev/null
+++ b/challenge-028/feng-chang/perl5/ch-2.pl
@@ -0,0 +1,17 @@
+#!/bin/env perl
+
+use Modern::Perl;
+use Date::Manip;
+
+use sigtrap 'handler' => \&quit_handler, 'INT';
+
+BEGIN { $| = 1 }
+
+while (1) {
+ print "\r", UnixDate(ParseDate('now'), '%H:%M:%S');
+}
+
+sub quit_handler {
+ say "\r", UnixDate(ParseDate('now'), '%H:%M:%S'), ' ';
+ exit 0;
+}
diff --git a/challenge-028/feng-chang/perl6/ch-1.p6 b/challenge-028/feng-chang/perl6/ch-1.p6
new file mode 100755
index 0000000000..921a792def
--- /dev/null
+++ b/challenge-028/feng-chang/perl6/ch-1.p6
@@ -0,0 +1,10 @@
+#!/bin/env perl6
+
+sub is-ascii(uint8 $c --> Bool)
+{ 9 <= $c <= 13 or 32 <= $c <= 126 }
+
+sub MAIN(Str:D $file-name where *.IO.e) {
+ print 'the file content is ';
+ say ([and] open($file-name, :r).read(16).list».&{ is-ascii($_) }) ??
+ 'ascii' !! 'binary';
+}
diff --git a/challenge-028/feng-chang/perl6/ch-2.p6 b/challenge-028/feng-chang/perl6/ch-2.p6
new file mode 100755
index 0000000000..a7f9277de8
--- /dev/null
+++ b/challenge-028/feng-chang/perl6/ch-2.p6
@@ -0,0 +1,8 @@
+#!/bin/env perl6
+
+my $clock = Supply.interval: 1;
+$clock.tap: { print "\r", DateTime.now.hh-mm-ss };
+
+signal(SIGINT).tap({ put "\r{ DateTime.now.hh-mm-ss } "; exit 0; });
+
+sleep ∞;