Main Navigation

Content

Text Generation in Perl

Learn Perl Now!
And get a job doing Perl.

Introduction

Perl is a good language for generating text, and even its name originally stood for "Practical Extraction and Report Language". This page aims to list some of the facilities provided by Perl and CPAN for generating text.

Text Generation Facilities

String Concatenation

Perl allows you to easily concatenate two strings using the . and .= operators. While you can quickly outgrow it, it is often enough. You can also interpolate string values inside some types of string constants. Therefore, you can say:

my $name = "Sophie";
my $city = "London";
my $large_string = "Hello $name, welcome to $city!";
# $large_string is now "Hello Sophie, welcome to London!"

You can also interpolate array elements and hash keys:

my @colors = ("Red", "Orange", "Yellow", "Green", "Indigo", "Blue", "Violet");
my %capitals = (
    "France" => "Paris",
    "Germany" => "Berlin",
    "Spain" => "Madrid",
    "Japan" => "Tokyo",
);

print "Japan has a flag with a $colors[0] circle, and a its capital is $capitals{Japan}\n";

Furthermore, one can interpolate more complex expressions using the turtle operator - @{[]}, although if you're resorting to doing it, you should probably use the more advanced techniques below:

#!/usr/bin/perl

use strict;
use warnings;

print "Please enter a number:\n";
my $n = <>;
chomp($n);

my $string = "$n is @{[($n % 2 == 0) ? 'even' : 'odd' ]}";

print "$string\n";

Perl also gives you a large selection of delimiters for strings as well as here-documents, which allow assigning multiple-line string constants with a user-specified terminating delimiter.

Nevertheless, please read on to see what other and often better options exist.

sprintf()

sprintf() is a built-in Perl function for formatting its arguments, similar to the sprintf function provided by C and other programming languages. Make sure you familiarize yourself with it.

Text-Sprintf-Named

Text-Sprintf-Named allows one to pass an associative array to a sprintf-like function with named conversions. So one can do:

my $formatter =
    Text::Sprintf::Named->new(
        {fmt => "Hello %(name)s! Today is %(day)s!"}
    );

$formatter->format({args => {'name' => "Ayeleth", 'day' => "Sunday"}});

Template Toolkit

Template Toolkit is a sophisticated and powerful templating system for Perl, with many extensions on the CPAN. Also see a presentation that Shlomi Fish gave about Template Toolkit, that explains why one should use a template system and especially Template Toolkit.

Data-Report

A Perl module for preparing reports as text and some other higher-level formats.

Perl6-Form

A module for formatting fixed-width text. You should use it instead of the built-in perlform facility assuming you want something like that.

Text-Table

A CPAN module for outputting text tables.

XML-Writer

A convenient CPAN module for outputting XML.

Generating Text Using Lists

An alternative option to generating text is to create arrays or more complex operations and serialise them into one large string (although this may be best done using Template Toolkit). Here are some pointers to get you started:

Share/Bookmark

Sidebar

Footer