For simple output formatting I tend to use printf where I used write/format in the days of Perl 4. However sometimes it seems the simplest solution for variable numbers of output lines per data record. For example
#!/usr/bin/perl
use strict;
use warnings;
my ($lorem, $aprille);
format =
@# ^<<<<<<<<<<<<<<<<<<<<<<<< | ^<<<<<<<<<<<<<<<<<<
$.,$aprille , $lorem
^<<<<<<<<<<<<<<<<<<<<<<<< | ^<<<<<<<<<<<<<<<<<< ~~
$aprille , $lorem
|
.
while(<DATA>) {
($aprille, $lorem) = split(/\|/, $_, 2);
write;
}
__DATA__
WHAN that Aprille with his shoures soote |Lorem ipsum dolor sit amet,
The droghte of Marche hath perced to the roote,|consectetur adipisicing elit,
And bathed every veyne in swich licour, |sed do eiusmod tempor
Of which vertu engendred is the flour; |incididunt ut labore et dolore
Whan Zephirus eek with his swete breeth |magna aliqua. Ut enim ad minim
Inspired hath in every holt and heeth |veniam, quis nostrud
The tendre croppes, and the yonge sonne |exercitation exercitation
Hath in the Ram his halfe cours y-ronne, |ullamco laboris nisi ut ali-
And smale fowles maken melodye, |quip ex ea commodo conse-
That slepen al the night with open ye, |quat. Duis aute irure dolor
So priketh hem nature in hir corages: |in reprehenderit in volup-
Than longen folk to goon on pilgrimages, |tate velit esse cillium dol-
And palmers for to seken straunge strondes, |ore eu fugiat nulla pariatur.
To ferne halwes, couthe in sondry londes; |Lorem ipsum dolor sit amet,
And specially, from every shires ende |consectetur adipisicing elit,
Of Engelond, to Caunterbury they wende, |sed do eiusmod tempor
The holy blisful martir for to seke, |incididunt ut labore et dolore
That hem hath holpen, whan that they were seke.|magna aliqua. Ut enim ad minim
And now for something completely different. Nice plumage.|Norwegian blue.
Produces
1 WHAN that Aprille with | Lorem ipsum dolor
his shoures soote | sit amet,
|
2 The droghte of Marche | consectetur
hath perced to the roote, | adipisicing elit,
|
3 And bathed every veyne in | sed do eiusmod
swich licour, | tempor
...
19 And now for something | Norwegian blue.
completely different. |
Nice plumage. |
Note that record 19 occupies three lines.
What is an equivalent concise perl5ish way to do the above without using write and format?
The main problem with formats is the reliance on global variables. For other problems with formats, see pages 449 – 454 of Perl Best Practices.
The modern solution would be
Perl6::Form. This is a backport of what they are planning for Perl 6.What follows is a rough translation of your format code to
Perl6::Form. I do not knowPerl6::Formvery well, so there may be ways to make it better or truer to your original example: