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

Hello!

I'd like to make a piece of code work a little more efficiently. I have a script which takes in a file as a parameter. However, the file needs to be a one liner because of the nature of the script.

What I need to accomplish ideally wouldn't change the module. Instead what I wanted to know, is it possible to pipe the file into the perl script one line at a time? Just to be clear I don't mean while(defined($line = <infile>))...ect.

thanks.

Replies are listed 'Best First'.
Re: File Pipe Question
by graff (Chancellor) on May 22, 2009 at 01:06 UTC
    The "-p" option (also described in perlrun) might be useful as well. The choice between "-p" and "-n" is whether you want to print every line of data after your perl script is applied to it, or whether you want to leave some (or all) lines of text unprinted (e.g. to do something else instead after all the input has been read and processed).

    In the latter case, you'll also want to learn about using the END{...} block -- e.g.:

    # one-liner to report histogram of byte values from data: cat some.data.* | perl -ne '$h{$_}++for(split//);END{printf"%x %d\n",o +rd(),$h{$_}for(sort keys %h)}'
Re: File Pipe Question
by akho (Hermit) on May 21, 2009 at 23:56 UTC
    Your meaning is not very clear, but see option -n in perlrun.