Or in newer perl's (5.8.0 or better I think) you could just open a handle to a scalar ref and write to it. Quick (untested) example:
#!/usr/bin/perl
format FOO =
This is @|||||||||||||
$foo
.
open(FOO,">", \$str) or die;
for $foo (qw(foo bar baz)) {
write FOO;
}
close FOO;
print $str;