Subject: | Feature request: print_hr(), print_header() |
I noticed it would be very useful to have a print_hr () method to do the reverse
of getline_hr () with a hash ref.
$csv->column_names("a", "b");
$csv->print_hr($fh, { a => 1, b => 2 });
# will print a line "1","2"
When I working with (sane) CSV I usually have to put a header line containing
field names. As we set the column names already a method to print the header
would be useful too:
$csv->print_header($fh);
# will print a line "a","b"
Attached is a patch providing support for both functions along with documentation.
Regards, Andy
Subject: | print-hr.patch |
--- a/CSV.pm 2013-03-01 11:44:09.000000000 +0100
+++ b/CSV.pm 2013-03-01 12:02:31.000000000 +0100
@@ -141,6 +141,19 @@
}
+sub print_hr {
+ my ($self, $fh, $ref) = @_;
+ my @r = map { $ref->{$_} } $self->column_names();
+ $self->print($fh, \@r);
+}
+
+
+sub print_header {
+ my ($self, $fh) = @_;
+ my @h = $self->column_names();
+ $self->print($fh, \@h);
+}
+
sub _load_xs {
my $opt = shift;
@@ -808,6 +821,29 @@
C<column_names ()> croaks on invalid arguments.
+=head2 print_hr
+
+Create a row from a hash reference containing the fields previously
+named by C<column_names ()>.
+
+C<print_hr ()> expects a file handle as its first argument following
+a hash reference.
+
+ $csv->columns_names (qw(a b c));
+ $csv->print_hr ($io, {
+ a => 1,
+ c => 3,
+ });
+
+=head2 print_header
+
+Print a header containing all names as set by C<columns_names ()>
+previously.
+
+C<print_header ()> expects a file handle as its first argument.
+
+ $csv->print_header ($io);
+
=head2 bind_columns
Takes a list of references to scalars to store the fields fetched