aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKian-Meng, Ang <kianmeng@cpan.org>2019-06-17 00:45:47 +0800
committerKian-Meng, Ang <kianmeng@cpan.org>2019-06-17 00:46:28 +0800
commit8589097ccef4315d8e21e740a68dcf9b69fffd8c (patch)
treee81895dde52e8a2a5a4425aaa2f22b8b74d28110
parent89a99ee87e462293366f76f9778391f34d751446 (diff)
downloadperlweeklychallenge-club-8589097ccef4315d8e21e740a68dcf9b69fffd8c.tar.gz
perlweeklychallenge-club-8589097ccef4315d8e21e740a68dcf9b69fffd8c.tar.bz2
perlweeklychallenge-club-8589097ccef4315d8e21e740a68dcf9b69fffd8c.zip
Add C2 & C3 answers for challenge #012
-rw-r--r--challenge-012/kian-meng-ang/perl5/ch-2.pl35
-rw-r--r--challenge-012/kian-meng-ang/perl5/ch-3.pl75
2 files changed, 110 insertions, 0 deletions
diff --git a/challenge-012/kian-meng-ang/perl5/ch-2.pl b/challenge-012/kian-meng-ang/perl5/ch-2.pl
new file mode 100644
index 0000000000..d152ad4435
--- /dev/null
+++ b/challenge-012/kian-meng-ang/perl5/ch-2.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+# vi:et:sw=4 ts=4 ft=perl
+
+use strict;
+use warnings;
+use utf8;
+use feature qw(say);
+
+use Array::Utils qw(intersect);
+
+my ($separator, @paths) = (q|/|, ());
+
+# No need $_ to prohibit useless topic
+push @paths, chomp && [split /$separator/] while (<DATA>);
+
+# Start with a base path. Compare and find common array elements against
+# subsequenty paths.
+my $common_path = shift @paths;
+@{$common_path} = intersect(@{$common_path}, @{$_}) foreach @paths;
+
+# Display the common prefix paths.
+# $ perl ch-2.pl
+# /a/b
+say join $separator, @{$common_path};
+
+# Note to self:
+# __END__ and __DATA__ token cannot mixed within the same file.
+
+1;
+
+__DATA__
+/a/b/c/d
+/a/b/cd
+/a/b/cc
+/a/b/c/d/e
diff --git a/challenge-012/kian-meng-ang/perl5/ch-3.pl b/challenge-012/kian-meng-ang/perl5/ch-3.pl
new file mode 100644
index 0000000000..a4fedacba6
--- /dev/null
+++ b/challenge-012/kian-meng-ang/perl5/ch-3.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/env perl
+# vi:et:sw=4 ts=4 ft=perl
+
+use strict;
+use warnings;
+use utf8;
+use feature qw(say);
+
+use Carp;
+use Config::Tiny;
+use Const::Fast;
+use HTTP::Tiny;
+use JSON::Tiny qw(decode_json);
+use XML::Hash::XS;
+
+const my $BASE_URL => 'https://www.abbreviations.com/services/v2/syno.php';
+const my $SERVICE => 'synonyms';
+
+MAIN: {
+ my $word = $ARGV[0] || croak q|Missing word!|;
+ my $word_info = _find_word_info(_config(), $word);
+
+ say $word_info->{synonyms};
+}
+
+sub _config {
+ my $rc_file = qq|$ENV{HOME}/.stands4rc|;
+ my $config = Config::Tiny->read($rc_file) || croak qq|Missing $rc_file!|;
+
+ $config->{$SERVICE}->{uid} || croak q|Missing API uid!|;
+ $config->{$SERVICE}->{token} || croak q|Missing API token!|;
+
+ return $config;
+}
+
+sub _find_word_info {
+ my ($config, $word) = @_;
+
+ my $params = {
+ uid => $config->{$SERVICE}->{uid},
+ token => $config->{$SERVICE}->{token},
+ word => $word,
+ format => 'json',
+ };
+
+ my $ua = HTTP::Tiny->new();
+ my $response = $ua->get($BASE_URL, $params);
+
+ if (!$response->{success}) {
+ croak qq|ERROR: $response->{code}, $response->{status}, $response->{reason}!|;
+ return;
+ }
+
+ # Error response is always a XML string regardless the `format` was
+ # explicitly set to `json`. See (2).
+ my $results = ($response->{content} =~ m/xml/)
+ ? xml2hash($response->{content})
+ : decode_json($response->{content})->{results};
+
+ croak qq|ERROR: $results->{error}| if (exists $results->{error});
+
+ return $results->{results}->{result};
+}
+
+1;
+
+__END__
+
+Note to self:
+(1) `HTTP::Tiny` does not have built-in way to deserialize JSON or XML response
+ data compare to `Mojo::UserAgent`.
+(2) Is there a better way to resolve inconsistency in returned HTTP body
+ content? How do we check for JSON or XML string?
+(3) Not all `XX::Tiny` CPAN modules were created or maintained equally.
+