Skip Menu |

This queue is for tickets about the Text-CSV CPAN distribution.

Report information
The Basics
Id: 83703
Status: resolved
Priority: 0/
Queue: Text-CSV

People
Owner: Nobody in particular
Requestors: astricker [...] futurelab.ch
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



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
Text::CSV supports print_hr() in latest version. However I think there is no need to support print_header because can write $csv->print( $io, $self->column_names() ). Thanks, On 2013-3月-01 金 06:11:47, astricker wrote: Show quoted text
> 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
From: astricker [...] futurelab.ch
Show quoted text
> Text::CSV supports print_hr() in latest version.
Great. Thanks. Show quoted text
> However I think there is no need to support print_header > because can write $csv->print( $io, $self->column_names() ).
Well, there was never a need for it, it's just a convenience method. As this header is usually printed only once, and the alternative $csv->print( $io, $csv->column_names() ) is simple enough too, I don't want to include print_header() anymore either. Thanks for resolving this issue. Regards, Andy