My eyes always miss this in the documentation, but starting in Perl 8.8 or thereabouts you can make a string into a file handle and then select it as the default file handle for write:
#make string into file handle
my $sBuf='';
open(my $fhOut, '>', \$sBuf); #\$sBuf is a reference to $sBuf
# make it your output stream for format/write
select $fhOut;
my ($sName, $iAge);
format SomeFormat =
Test: @<<<<<<<<<<<< @>>>>>
$sName, int($iAge)
.
$sName = "MickeyMouse";
$iAge = 82; #born Nov, 1928
$~ = 'SomeFormat';
write;
print "buffer is: <$sBuf>\n";
If you have perl 5.8.8 installed the documentation only shows up in open and not in perlopentut. In the Perl 5.12.2 documentation, there is a FAQ question devoted to this.
Alternatively, you can also try to follow the advice here to roll your own swrite() function.
Update: added sample code using string buffer file handle with format and write
Update: added the 5's, as per DrHyde below. | [reply] [Watch: Dir/Any] [d/l] [select] |
| [reply] [Watch: Dir/Any] |
sprintf and printf are your huckleberries...
The first argument is the format specification, which is then followed by a list of variables that directly correspond to the "placeholders" specified in the format.
For example,
printf "%05d", 32; # prints an integer 5 digits wide, with leading ze
+roes, ie. 00032
printf "%32s", 'foo'; # prints the word 'foo' into an area 32 characte
+rs wide
This doesn't even begin to scratch the surface of what sprint and printf do, but it is a place to start.
What can be asserted without proof can be dismissed without proof. - Christopher Hitchens
| [reply] [Watch: Dir/Any] [d/l] |
Show us the code you have tried.
True laziness is hard work
| [reply] [Watch: Dir/Any] |
format SIMPLE_TOP=
______________________________________________________________________
+_
@|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"TITLE"
______________________________________________________________________
+_
@|||||||||||||||||... |@||||||||... @||||||||... |@||||||||||||||||||.
+..
"Variable1","Variable2","Variable3","Variable3"
______________________________________________________________________
+_
.
format SIMPLE=
@<<<<<<<<<<<<<<<<<... |@||||||||... @||||||||... |@||||||||||||||||||.
+..
$varible, $varible, $varible, $varible
______________________________________________________________________
+_
.
and I am using write; to print it in the output. I found in documentation :
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;
but I don't have idea how it schould work, could you explain me,
what mean << operator in that function, and how can I put more complicated formats, with TOP, and , FOOTER etc.
ELISHEVA and DrHyde , I will try your tips, and check which will work for me better.
thanks a lot for respond. | [reply] [Watch: Dir/Any] [d/l] [select] |
Eep! Formats! I haven't seen those in YEARS! In fact I've not seen them used for nearly two *decades*! You should consider them to be obsolete, and find another way of formatting your output. sprintf() is probably the weapon of choice.
| [reply] [Watch: Dir/Any] [d/l] |
I just looking for way, to format somehow my output for easily reading. And I find those formats, I will try sprintf(),
| [reply] [Watch: Dir/Any] |