Perl

Posts

Dist::Zilla on Travis CI

With Dist::Zilla (dzil), testing Perl projects on Travis CI can be a bit tricky. Here's my approach.

Perl Docstrings: Put your POD into Heredocs

Python's docstrings are great, but have no real equivalent in Perl. However, we can make POD sections easily accessible to Perl code by putting the POD into heredocs:

my $some_function_docs = <<'=cut';

=head2 some_function

Write documentation here.

=cut

This article explains why that works, and how this might be used in your Perl modules.

by Lukas Atkinson Perl (5)

m//gc Style Lexing With Perl

or: Five Parsing Techniques You Can't Do With s/// Substitutions

You are writing a simple lexer or parser in Perl? You'll probably use regexes. Here's how to use the little-known pos() function to correctly apply regexes.

How to check for an array reference in Perl

So you've got a Perl $variable. Can we use it as an array or hash reference? If you do an online search for possible solutions, you'll find a number of suggestions, most of them wrong.

TL;DR: checking if ref $variable eq 'ARRAY' is almost always a bug. Depending on your use case, you want:

  • reftype $variable eq 'ARRAY' from Scalar::Util as a check for physical array references, or
  • _::is_array_ref $variable from my module Util::Underscore as a check for logical array references.
by Lukas Atkinson Perl (5)

Transforming Syntax

Or: how to write the easy part of a compiler

A Stack Overflow question asked how to translate a VB-like conditional into a C-like ternary. The other answers suggested regexes or treating it as Perl code *shudder*. But transpiling code to another language can be done correctly.

This post aims to cover:

  • parsing with Marpa::R2,
  • AST manipulation,
  • optimization passes,
  • compilation, and
  • Perl OO.

In the end, we'll be able to do all that in only 200 lines of code!

Since this post is already rather long, we will not discuss parsing theory. You are expected to be familiar with EBNF grammar notation.

Dump notes