Offer Kaye wrote:
> Hi,
Hello,
> Can anyone tell me if it is possible to define 2 different formats for
> the same filehandle?
>
> The reason I am asking is that I want to print 2 different tables to
> the same text file and I don't want to use printf statements. For me
> at least, code that uses printf to print something as complex as a
> text table is hard to both write and read, hard to understand and hard
> to debug. It's also ugly, to boot :)
>
> My current workaround is to close the file, reopen in append mode with a
> different filehandle name and define the second format with this name.
> Ugly, but works. Any other/better solutions?
After reading perlform (perldoc perlform) I came up with this:
#!/usr/bin/perl
use warnings;
use strict;
sub _print_format1 {
# output table 1, with 3 columns
$^A = '';
formline <<'FORMAT', @_;
@<<<<<<<<<<<<<< @>>>>>>>>>>> @<<<<<<<<<<<<<<<<<<<<<<<<<
FORMAT
$^A;
}
sub _print_format2 {
# output table 2, with 6 columns
$^A = '';
formline <<'FORMAT', @_;
@<<<<<<<<<<<< @>>>>>>>>>>>>> @>>>>>>>>> @>>>>>>>>>>>>> @>>>>>>>>>>>>>
@>>>>>>>>>>>>>>>
FORMAT
$^A;
}
my $outfile = 'file_with_tables.txt';
open OUT, '>', $outfile or die "Couldn't open $outfile for writing: $!\n";
print OUT "Table 1:\n",
_print_format1( 1, 15, 'foo' ),
_print_format1( 2, 8, 'bar' ),
"\nTable 2:\n",
_print_format2( 'clk1', 0.04, 12000, 0.51, 0.39, 0.25 ),
_print_format2( 'clk2', 0.13, 27000, 0.8, 0.5, 0.1 );
close OUT or die "Couldn't close $outfile after writing: $!\n";
__END__
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>