pipe snippets

How to pipe input to a Perl script

Tagged pipe, perl  Languages perl

Let's say you want to pipe some input to a Perl script. First, you create this Perl script (pipe_me.pl):

while (<>) 
  print $_;

Then you call the script like this:

less access.log | perl pipe_me.pl

The script outputs the contents of access.log. To do some real work extend it with your own code--you might want to, for example, analyze an Apache access log.

You can also read the input line by line like this:

foreach $line (<>) 
  print $line;