DEV Community

SunnahSource
SunnahSource

Posted on

Build a Hijri Months CLI in Perl 5 with Only Core Modules (HTTP::Tiny + JSON::PP, 24h Cache, 9 Languages)

Perl still ships on almost every Linux and macOS box, and its core library is enough to call a JSON API, cache the result and print Unicode properly. In this post we build a tiny command-line tool that lists the Islamic (Hijri) months with their Arabic names, number of days and a short note in your language, using the Hijri Calendar API from QuraniAPI.

No CPAN installs beyond what your distro already has for HTTPS.

The endpoint

GET https://quranicapi.online/api/serve.php?slug=hijri-calendar&lang=all
Enter fullscreen mode Exit fullscreen mode

The public sample response (no key needed) looks like this (trimmed):

{
  "title": "Hijri Calendar",
  "current_hijri": "1447 AH",
  "months": [
    { "number": 1, "name_ar": "مُحَرَّم", "name_en": "Muharram", "days": 30,
      "translations": { "en": "Sacred month — Islamic New Year, Ashura", "ur": "...", "de": "Heiliger Monat" } }
  ],
  "success": true
}
Enter fullscreen mode Exit fullscreen mode

The sample includes 5 of the 12 months (Muharram, Safar, Rabi al-Awwal, Ramadan, Dhul Hijjah). The full 12 months and Islamic events are a premium API; see pricing. Translations come in English, Urdu, Malay, French, German, Russian, Turkish, Chinese and Japanese.

HTTPS for HTTP::Tiny

HTTP::Tiny is core, but HTTPS needs IO::Socket::SSL and Net::SSLeay. Check with:

perl -MHTTP::Tiny -e 'print HTTP::Tiny->can_ssl ? "ok\n" : "missing\n"'
# Debian/Ubuntu if missing:
sudo apt-get install libio-socket-ssl-perl libnet-ssleay-perl
Enter fullscreen mode Exit fullscreen mode

The script (hijri.pl)

#!/usr/bin/env perl
# Hijri months CLI - Perl 5 core modules only (HTTP::Tiny + JSON::PP)
use strict; use warnings; use utf8;
use HTTP::Tiny; use JSON::PP; use File::Spec;
binmode STDOUT, ':encoding(UTF-8)';

my $lang  = shift // 'en';
my $url   = 'https://quranicapi.online/api/serve.php?slug=hijri-calendar&lang=all';
my $cache = File::Spec->catfile(File::Spec->tmpdir, 'quranicapi-hijri-calendar.json');
my $ttl   = 24 * 3600;

sub load {
    if (-e $cache && time - (stat $cache)[9] < $ttl) {
        open my $fh, '<:raw', $cache or die $!; local $/; return <$fh>;
    }
    my $res = HTTP::Tiny->new(timeout => 15, agent => 'hijri-cli/1.0')->get($url);
    if (!$res->{success}) {
        die "HTTP $res->{status} and no cache\n" unless -e $cache;
        warn "Network failed ($res->{status}), using stale cache\n";
        open my $fh, '<:raw', $cache or die $!; local $/; return <$fh>;
    }
    open my $fh, '>:raw', $cache or die $!; print $fh $res->{content}; close $fh;
    return $res->{content};
}

my $data = JSON::PP->new->utf8->decode(load());
die "API returned success=false\n" unless $data->{success};

printf "%s (%s)\n\n", $data->{title}, $data->{current_hijri};
for my $m (sort { $a->{number} <=> $b->{number} } @{ $data->{months} }) {
    my $note = $m->{translations}{$lang} // $m->{translations}{en} // '';
    printf "%2d. %-14s %s  %d days  - %s\n",
        $m->{number}, $m->{name_en}, $m->{name_ar}, $m->{days}, $note;
}
Enter fullscreen mode Exit fullscreen mode

What it does:

  • HTTP::Tiny fetches the JSON; status 599 means a connection or TLS error, so we fall back to the last cached copy.
  • The raw response is cached in your temp dir for 24 hours, so repeated runs are instant and work offline.
  • JSON::PP->new->utf8 decodes bytes to Perl characters, and binmode STDOUT, ':encoding(UTF-8)' prints Arabic and Urdu correctly.
  • If the chosen language key is missing, it falls back to English.

Run it

perl hijri.pl en
Enter fullscreen mode Exit fullscreen mode
Hijri Calendar (1447 AH)

 1. Muharram       مُحَرَّم  30 days  - Sacred month — Islamic New Year, Ashura
 2. Safar          صَفَر  29 days  - Second month of Islamic calendar
 3. Rabi al-Awwal  رَبِيع الأَوَّل  30 days  - Month of Prophet's birth (SAW)
 9. Ramadan        رَمَضَان  30 days  - Month of fasting — Quran revealed
12. Dhul Hijjah    ذُو الحِجَّة  29 days  - Month of Hajj — Eid al-Adha
Enter fullscreen mode Exit fullscreen mode
perl hijri.pl ur   # 1. Muharram  مُحَرَّم  30 days  - حرام مہینہ — اسلامی نیا سال، عاشورہ
perl hijri.pl de   # 1. Muharram  مُحَرَّم  30 days  - Heiliger Monat
Enter fullscreen mode Exit fullscreen mode

Ideas to extend

All 17 APIs are listed at quranicapi.online/apis.html, docs at quranicapi.online/docs.html, and the open sample repo is on GitHub.

Tested with Perl 5 core HTTP::Tiny and JSON::PP on Linux.

Top comments (0)