Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Replace characters of input stream on the fly

by wytetygur (Initiate)
on Apr 08, 2011 at 04:29 UTC ( #898254=perlquestion: print w/replies, xml ) Need Help??
wytetygur has asked for the wisdom of the Perl Monks concerning the following question:


I'm fairly new to perl and am trying to figure out how I can open a file, start reading it and replace the Macintosh style CR line terminators with the Unix style LF line terminator and then send that output directly to the Text::CSV module.

I'd like to do this without having to write the modified input to a file, then open that file to be process by Text::CSV.

Is there a way in perl to parse the input stream and then feed the output directly to another function? Kind of like the pipe on the command line.

I've look through all the perldoc documentation but am not getting any clues to my answers.

Sorry, no code sample because I don't know how to start it yet

I'd really appreciate your help.

Thanks, Allen

  • Comment on Replace characters of input stream on the fly

Replies are listed 'Best First'.
Re: Replace characters of input stream on the fly
by atcroft (Abbot) on Apr 08, 2011 at 05:02 UTC

    Could you not use something similar to the following?

    my $csv = Text::CSV->new ({ eol => qq{\r}, }) or die "" . Text::CSV->error_diag ();

    Hope that helps.

Re: Replace characters of input stream on the fly
by apl (Monsignor) on Apr 08, 2011 at 10:37 UTC
    Write a program that opens a file, or dies if it can't be opened.

    Modify the program to read one line, and display that line.

    Modify that program to read each line in the file.

    Modify that program to open a second, CSV file for output (dying if you can't).

    Modify that program to output each read line to the CSV file.

    Modify that program to change the record terminator.

    Let us know at what point you have a problem. Show us your code and the error produced.

    Otherwise, tell your teacher / boss you're too lazy to do the job yourself...

Re: Replace characters of input stream on the fly
by SimonClinch (Deacon) on Apr 08, 2011 at 12:05 UTC
    The problem is not clearly stated and seems to be blaming the wrong "Pirate of Silicon Valley", but if you want the CR to LF conversion to be transparent to Perl and feed each line to a method from a module, you could open a pipe from the output of a dos2unix conversion process instead of the file itself and use map as an analogy to pipe:
    my $csv = Text::CSV->new; my $pid = open my $ph, 'dos2unix < inputfilename.txt |'; map $csv -> method($_), <$ph>; ... close $ph; waitpid $pid, 0; ...
    However I can't see a method in Text::CSV you'd want to do that for. Seems more like you need to create your own routine that goes through several different CSV method calls to process each line of input.

    One world, one people

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://898254]
Approved by NetWallah
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2018-04-20 05:43 GMT
Find Nodes?
    Voting Booth?