Have a look at the formline function and the accumulator $^A.
Here is a short, seasonal, example that centres the elements of an array on separate lines. The format is built dynamically.
#!/usr/bin/perl -w
use strict;
my @a = qw(* *** ***** ******* ********* *);
formline(('@'.('|'x72)."\n")x@a, @a);
print $^A;
--
John.
| [reply] [Watch: Dir/Any] [d/l] |
My second edition camel book shows how to do it. The critical points are: 1) put the output of the format statement into a variable, 2) eval that variable. Here is a piece of demonstration code. I have tried to make it readable, it is surely not the most compact version possible.
$data = qw( aaa bbb ccc );
$fields = scalar @data;
$data_fields = "";
map $data_fields .= '"' . $_ . '"', @data;
$format = "format STDOUT = \n"
. "@<<< " x $fields . "\n"
. $data_fields . "\n";
. ".\n";
eval $format;
write;
HTH, --traveler
| [reply] [Watch: Dir/Any] [d/l] |
Use eval.
See the camel p 126 or perlform. | [reply] [Watch: Dir/Any] |
Here two functions I use myself in one my module:
# formated output
sub sformat {
my $format = shift;
local $^A = '';
formline($format, @_);
return $^A;
}
# print line using some format specification
sub fprint {
my $format = shift;
$self->print(sformat($format, @_));
}
--
Ilya Martynov
(http://martynov.org/)
| [reply] [Watch: Dir/Any] [d/l] |
| [reply] [Watch: Dir/Any] |