diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-08-30 00:50:23 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-08-30 00:50:23 +0100 |
| commit | 81d499fd8952c0a0b3ca26c57d039a9ea1441a05 (patch) | |
| tree | ff6cfbd8ee4f87e7a5dad2815202e555a0d9c2d0 | |
| parent | 5a36dc73838d80b807db4f9d5d158431a30959df (diff) | |
| parent | df6f26067815074aec9f7a1cc7d4c7cb30cd27fd (diff) | |
| download | perlweeklychallenge-club-81d499fd8952c0a0b3ca26c57d039a9ea1441a05.tar.gz perlweeklychallenge-club-81d499fd8952c0a0b3ca26c57d039a9ea1441a05.tar.bz2 perlweeklychallenge-club-81d499fd8952c0a0b3ca26c57d039a9ea1441a05.zip | |
Merge pull request #571 from andrezgz/challenge-023-task3
challenge-023 andrezgz task3 solution
| -rw-r--r-- | challenge-023/andrezgz/perl5/ch-3.pl | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/challenge-023/andrezgz/perl5/ch-3.pl b/challenge-023/andrezgz/perl5/ch-3.pl new file mode 100644 index 0000000000..4950dc47ee --- /dev/null +++ b/challenge-023/andrezgz/perl5/ch-3.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-023/ +# Task #3 +# Write a script to use Random Poems API. This is the easiset API, I have come across so far. +# You don't need API key for this. They have only route to work with (GET). +# https://www.poemist.com/api/v1/randompoems +# The API task is optional but we would love to see your solution. + +use strict; +use warnings; + +use LWP::Simple qw (get); +use JSON qw( decode_json ); +binmode STDOUT, ':encoding(UTF-8)'; + + +my $json = get('https://www.poemist.com/api/v1/randompoems'); +die 'No poem for you right now :(' unless defined $json; +my $poems = decode_json( $json ); + +my $rnd_poem = $poems->[int rand scalar @$poems]; +my $heading = $rnd_poem->{title}.' by '.$rnd_poem->{poet}->{name}; +my $line = q{=} x length $heading; + +print sprintf "%s\n%s\n\n%s\n", $heading, $line, $rnd_poem->{content}; |
