October 2019 Archives

Perl Weekly Challenge 031: Division by Zero & Dynamic Variable Name

Division by zero

Create a function to check divide by zero error without checking if the denominator is zero.

Perl already checks the denominator when dividing. All we need to do is to catch the exception it throws and check it instead of checking the value of the denominator.

The following division subroutine uses Try::Tiny to catch the exception. It’s a good practice not to use the low level eval, see for example Bug in eval in pre-5.14 for the reasons.

#!/usr/bin/perl
use warnings;
use strict;

use Try::Tiny;

sub division {
    my ($numerator, $denominator) = @_;
    try {
        { safe => 1, result => $numerator / $denominator }
    } catch {
        { safe => 0, error => $_ }
    }
}

use Test::More tests => 4;

ok division(1,2)->{safe};
ok !division(1,0)->{safe};

is division(1,2)->{result}, 1/2;
like division(1,0)->{error}, qr/Illegal division by zero/;

Perl Weekly Challenge 030: Sunday Christmas and Series with sum 12

Sunday Christmas

Write a script to list dates for Sunday Christmas between 2019 and 2100. For example, 25 Dec 2022 is Sunday.

I used the core module Time::Piece to check the dates.

It’s easy to create a string representing Christmas: just concatenate the year with '-12-25'. The module’s strptime method can be used to create an object if we provide a format of the input string, in this case it’s '%Y-%m-%d'. The object’s method day_of_week now tells us what day the object represents, 0 corresponds to Sunday, which is the day we’re interested in.

Perl Weekly Challenge 029: Brace expansion and calling a C function

Brace expansion

Write a script to demonstrate brace expansion. For example, the script would take the command line argument Perl {Daily,Weekly,Monthly,Yearly} Challenge and should expand it and print like below:
Perl Daily Challenge
Perl Weekly Challenge
Perl Monthly Challenge
Perl Yearly Challenge

You’ve probably heard about the glob function. It can expand wildcards like * or ?, so you e.g. can easily check what files correspond to p*.p?. But glob can do one more thing for us: brace expansion.

Perl Weekly Challenge 028: File Content and Digital Clock

File Content

Write a script to check the file content without explicitly 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.

Frankly, I had no idea how to solve this. I had to google the solution, and I was quite surprised Perl had the operators designed for exactly this purpose. During my approximately 20 years of Perl programming, I’ve never needed the -T and -B operators—probably because all my scripts and programs expect either a text or a binary file as the input, and it’s upon the user to provide it in the expected format.

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

say 'The file content is ', (-T shift) ? 'ascii' : 'binary', '.';

Skipping DateTime::Format::Mail in the Pull Request Club

I was assigned DateTime::Format::Mail for September in the Pull Request Club. There was one open issue in its GitHub repository, so I hoped solving it would be my pull request for the month.

The reporter complained the module fails to parse the following e-mail header:

Date: Wed, 10 Jul 2019 11:20:02 +0200 (CEST)

About E. Choroba

user-pic I blog about Perl.