diff options
| author | PerlMonk Athanasius <PerlMonk.Athanasius@gmail.com> | 2019-10-06 05:32:19 -0700 |
|---|---|---|
| committer | PerlMonk Athanasius <PerlMonk.Athanasius@gmail.com> | 2019-10-06 05:32:19 -0700 |
| commit | fc13960f7017092aafb27c466b4aa97db81c1e42 (patch) | |
| tree | c639ca1e25018c3df8f796e72907369579b9119c | |
| parent | 27cd66cc7098cf120fa0de1c6e63b347c15cb5c0 (diff) | |
| download | perlweeklychallenge-club-fc13960f7017092aafb27c466b4aa97db81c1e42.tar.gz perlweeklychallenge-club-fc13960f7017092aafb27c466b4aa97db81c1e42.tar.bz2 perlweeklychallenge-club-fc13960f7017092aafb27c466b4aa97db81c1e42.zip | |
Perl 5 & 6 solutions to Tasks 1 & 2 of the Perl Weekly Challenge #028
On branch branch-for-challenge-028
Changes to be committed:
new file: challenge-028/athanasius/perl5/ch-1.pl
new file: challenge-028/athanasius/perl5/ch-2.pl
new file: challenge-028/athanasius/perl6/ch-1.p6
new file: challenge-028/athanasius/perl6/ch-2.p6
| -rw-r--r-- | challenge-028/athanasius/perl5/ch-1.pl | 55 | ||||
| -rw-r--r-- | challenge-028/athanasius/perl5/ch-2.pl | 64 | ||||
| -rw-r--r-- | challenge-028/athanasius/perl6/ch-1.p6 | 56 | ||||
| -rw-r--r-- | challenge-028/athanasius/perl6/ch-2.p6 | 57 |
4 files changed, 232 insertions, 0 deletions
diff --git a/challenge-028/athanasius/perl5/ch-1.pl b/challenge-028/athanasius/perl5/ch-1.pl new file mode 100644 index 0000000000..6071c94f2d --- /dev/null +++ b/challenge-028/athanasius/perl5/ch-1.pl @@ -0,0 +1,55 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 028 +========================= + +Task #1 +------- +Write a script to check the file content without actually reading the content. +It should accept file name with path as command line argument and print *"The +file content is binary."* or else *"The file content is ascii."* accordingly. + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2019 PerlMonk Athanasius # +#--------------------------------------# + +use strict; +use warnings; +use Const::Fast; + +const my $USAGE => "USAGE: perl $0 <Path>\n"; + +BEGIN +{ + $| = 1; + print "\n"; +} + +#=============================================================================== +MAIN: +#=============================================================================== +{ + @ARGV == 0 and die $USAGE; + @ARGV > 1 and die "Too many command line arguments\n$USAGE"; + + $_ = $ARGV[0]; + + # See https://perldoc.perl.org/5.30.0/functions/-X.html + + my $description = ! -e ? 'This does not exist' : + -d ? 'This is a directory' : + ! -f ? 'This is not a plain file' : + -z ? 'The file is empty' : + -T ? 'The file content is text' : + 'The file content is binary'; + + print qq{"$_": $description\n}; +} + +################################################################################ diff --git a/challenge-028/athanasius/perl5/ch-2.pl b/challenge-028/athanasius/perl5/ch-2.pl new file mode 100644 index 0000000000..195730d8d0 --- /dev/null +++ b/challenge-028/athanasius/perl5/ch-2.pl @@ -0,0 +1,64 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 028 +========================= + +Task #2 +------- +Write a script to display *Digital Clock*. Feel free to be as creative as you +can when displaying digits. We expect bare minimum something like *"14:10:11"*. + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2019 PerlMonk Athanasius # +#--------------------------------------# + +use strict; +use warnings; +use Const::Fast; +use DateTime; + +const my $TIMEZONE => 'Australia/Brisbane'; + +BEGIN +{ + $| = 1; + print "\n"; +} + +#=============================================================================== +MAIN: +#=============================================================================== +{ + my $dt = DateTime->now(time_zone => $TIMEZONE); + my $hour = $dt->hour; # 0-23 + my $min = $dt->minute; # 0-59 + my $sec = $dt->second; # 0-61 (leap seconds!) + + printf "%02d:%02d:%02d\r", $hour, $min, $sec; + + while (1) + { + sleep 1; + + if (++$sec >= 60) + { + $sec = 0; + + if (++$min == 60) + { + $min = 0; + $hour = 0 if ++$hour == 24; + } + } + + printf "%02d:%02d:%02d\r", $hour, $min, $sec; + } +} + +################################################################################ diff --git a/challenge-028/athanasius/perl6/ch-1.p6 b/challenge-028/athanasius/perl6/ch-1.p6 new file mode 100644 index 0000000000..8089a8a6eb --- /dev/null +++ b/challenge-028/athanasius/perl6/ch-1.p6 @@ -0,0 +1,56 @@ +use v6; + +################################################################################ +=begin comment + +Perl Weekly Challenge 028 +========================= + +Task #1 +------- +Write a script to check the file content without actually reading the content. +It should accept file name with path as command line argument and print *"The +file content is binary."* or else *"The file content is ascii."* accordingly. + +=end comment +################################################################################ + +#--------------------------------------# +# Copyright © 2019 PerlMonk Athanasius # +#--------------------------------------# + +use File::Util:from<Perl5> <file_type>; + +BEGIN say ''; + +#=============================================================================== +sub MAIN(Str:D $path) +#=============================================================================== +{ + my Str $description = ! .e ?? 'This does not exist' !! + .d ?? 'This is a directory' !! + ! .f ?? 'This is not a plain file' !! + .z ?? 'The file is empty' !! 'OK' + given $path.IO; + + if $description eq 'OK' + { + my Str @types = file_type($path); + + if @types.elems == 2 && @types[0] eq 'PLAIN' + { + my Str $t1 = @types[1]; + $description = $t1 eq 'TEXT' ?? 'The file content is text' !! + $t1 eq 'BINARY' ?? 'The file content is binary' !! + 'ERROR: The file content is neither text nor binary'; + } + else + { + $description = 'ERROR: Unexpected file types: ' ~ @types.join(', '); + } + } + + qq{"$path": $description}.say; +} + +################################################################################ diff --git a/challenge-028/athanasius/perl6/ch-2.p6 b/challenge-028/athanasius/perl6/ch-2.p6 new file mode 100644 index 0000000000..81ad6ea713 --- /dev/null +++ b/challenge-028/athanasius/perl6/ch-2.p6 @@ -0,0 +1,57 @@ +use v6; + +################################################################################ +=begin comment + +Perl Weekly Challenge 028 +========================= + +Task #2 +------- +Write a script to display *Digital Clock*. Feel free to be as creative as you +can when displaying digits. We expect bare minimum something like *"14:10:11"*. + +=end comment +################################################################################ + +#--------------------------------------# +# Copyright © 2019 PerlMonk Athanasius # +#--------------------------------------# + +my UInt constant $TIMEZONE = +10; # Difference in hours from UTC/GMT +my UInt constant $SECS-PER-HOUR = 60 * 60; +my UInt constant $TZ-OFFSET = $TIMEZONE * $SECS-PER-HOUR; + +BEGIN say ''; + +#=============================================================================== +sub MAIN() +#=============================================================================== +{ + my DateTime $dt = DateTime.now(timezone => $TZ-OFFSET); + my UInt $hour = $dt.hour; + my UInt $min = $dt.minute; + my UInt $sec = $dt.whole-second; + + "%02d:%02d:%02d\r".printf($hour, $min, $sec); + + while 1 + { + sleep 1; + + if ++$sec >= 60 + { + $sec = 0; + + if ++$min == 60 + { + $min = 0; + $hour = 0 if ++$hour == 24; + } + } + + "%02d:%02d:%02d\r".printf($hour, $min, $sec); + } +} + +################################################################################ |
