February 2012
Intermediate to advanced
1184 pages
37h 17m
English
For low-level access to the internal formatting mechanism, you may use
the built-in formline operator and
access $^A (the $ACCUMULATOR variable) directly. (Formats
essentially compile into a sequence of calls to formline.) For example:
$str = formline <<'END', 1,2,3; @<<< @||| @>>> END say "Wow, I just stored '$^A' in the accumulator!";
Or to create an swrite
subroutine that is to write as
sprintf is to printf, do this:
use Carp;
sub swrite {
croak "usage: swrite PICTURE ARGS" unless @_;
my $format = shift;
$^A = "";
formline($format, @_);
return $^A;
}
$string = swrite(<<'END', 1, 2, 3);
Check me out
@<<< @||| @>>>
END
print $string;If you were using the IO::Handle module, you could use formline as follows to wrap a block of text at
column 72:
use IO::Handle;
STDOUT–>formline("^" . ("<" x 72) . "~~\n", $long_text);Now brace yourself for a big chapter…
Read now
Unlock full access