borisz has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I could not find a option to turn the lineendings in Text::xSV from LF to CRLF.
Is there a better way?
My quick fix looks ugly:
package MyXSV; use base 'Text::xSV'; sub format_row { my $csv = shift; my $row = $csv->SUPER::format_row(@_); $row =~ s/\n$/\015\012/; $row; } 1; ... while ( $res->fetch ) { $csv->print_row( $city_id, $alias_id, $alias ); }
I tried a filter option to new filter => sub { local $_ = shift; s/\s+$/\015\012/; $_ } and local $\ = "\015\012";
Boris

Replies are listed 'Best First'.
Re: set lineendings in Text::xSV
by dragonchild (Archbishop) on Apr 07, 2006 at 12:00 UTC
    Text::xSV assumes that you'll be reading the file on the same platform you wrote it. Maybe, you should provide a patch to tilly changing the ."\n" at the end of format_row() to $/ or some named variable. That's what I would do.

    Update: Fixed author's name from tye to tilly. (Sorry!)


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: set lineendings in Text::xSV
by graff (Chancellor) on Apr 08, 2006 at 07:43 UTC
    Have you tried putting ":crlf" as a PerlIO layer on the output file handle? That should make the output meet your requirements without having to muck with Text::xSV at all (but I haven't tried -- I'm just guessing).
      I really wonder, but you are right! It works. Thanks graff
      Boris
Re: set lineendings in Text::xSV
by cdarke (Prior) on Apr 07, 2006 at 10:01 UTC
    At the perl level (I don't know Text::xSV) your RE could be prettier with:s/\n/\r\n/although I have to ask why you wish to add the \r?
      CRLF is a requirement for the csv.
      Boris
        If you are running this on Windows then it will insert the \r for you (unless the fh has binmode set). If running it on another platform then you can try setting $\ to "\r\n".