A spreadsheet of *n* fields is to be generated. For debugging purpose,
confidentiality and so on reasons, sometimes some of the fields are not to
be generated.
Using a series of "if" or "?" can solve the problem, but it makes the codes
very clumsy and not generic. Is perl capable of handling this situation?
Absolutely!
The easiest way is to use printf. Just printf each line.
If you want a plain text spreadsheet, use a format. See perlform for
more details.
If you want a more dynamic spreadsheet, print your values out as a
comma delimited file and (on Windows) save it with a .csv extension.
This will open natively in Excel and Access. Example:
print SSFILE "$var1,$var2,$var3,$var4,$var5,$var2\n";
If you want the ultimate in flexible spreadsheets, print each line as
XML, interpolating variables as necessary. This way, you can transform
your output into many different formats by using XSLT, or can write
your own parser if your skills allow. See example below.
CC
#!/usr/bin/perl -w
# Name: test_heredoc_xml_print.plx
# Purpose: to test printing a heredoc to an outfile
open OUTFILE, ">heredoc.xml";
print OUTFILE <<END;
<?xml version="1.0" ?>
<people>
<person>
<first>John</first>
<middle>S</middle>
<last>McCain</last>
<party>Republican</party>
</person>
<person>
<first>Mike</first>
<middle></middle>
<last>Huckabee</last>
<party>Republican</party>
</person>
<person>
<first>Barack</first>
<middle>Hussein</middle>
<last>Obama</last>
<party>Democrat</party>
</person>
<person>
<first>Hillary</first>
<middle>Rodham</middle>
<last>Clinton</last>
<party>Democrat</party>
</person>
<person>
<first>Ralph</first>
<middle></middle>
<last>Nader</last>
<party>Independant</party>
</person>
</people>
END
close OUTFILE;
exit();