You could use Perl's internal format defintions. Here's an example that should work for you.
#!/usr/bin/perl -w
use strict;
# Define various fields that we use in the format
my $name = "TMH";
my $title = "PerlMonk";
my $story = 'The Mad Hatter (TMH) was a crazy monk. It was thought' .
' to be due to the mercury used to get rid of the dust ' .
'on hats...';
# This is the key part. A format is a series of fieldlines and
# valuelines. Fieldlines define what the format looks like, valueline
+s
# specify what datagoes in the fields. Fields that start with @ are
# fixed character widthfields. The < specifies that it is
# left-justified. | (centered) and > (right-justified) are also
# valid. The number of those specifiers is the width of the field.
# The ^ is a filled field; it breaks text up (at word boundries) into
# conveniently sized lines. The double tilde (~) at the beginning of
# the third line specifies that the line should expand dynamically.
# This allows the filled field to take up as many lines of that size a
+s
# it needs to. We use the format name STDOUT so that the format
# affects that filehandle.
format STDOUT =
@<<<<<< @<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<
$name, $title, $story
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<
$story
.
# Print doesn't work with formats, so write must be used. write
# defaults to sending to STDOUT and looks for the format STDOUT.
write;
That prints
TMH PerlMonk The Mad Hatter (TMH) was a
crazy monk. It was thought
to be due to the mercury
used to get rid of the dust
on hats...
Added new code. Now with comments! ;-) |