It's quite common when outputting tabular data (be it CSV, or an HTML table) to ensure that column headers are printed once by using something along the lines of:
sub arrayref_to_csvline { ... }
my $headers;
while (my $row = $iter->())
{
unless ($headers)
{
print arrayref_to_csvline($row->fields);
$headers = 1;
}
print arrayref_to_csvline($row->values);
}
I recently discovered a pattern that takes advantage of the return value of print (it returns 1 on success)...
sub arrayref_to_csvline { ... }
my $headers;
while (my $row = $iter->())
{
$headers ||= print arrayref_to_csvline($row->fields);
print arrayref_to_csvline($row->values);
}
... just thought I'd share.
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'