Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

File Pipe Question

by Anonymous Monk
on May 21, 2009 at 22:50 UTC ( [id://765567]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://765567]
Approved by AnomalousMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-19 09:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found