Shell Wrapped in Perl

Last entry: 27 Nov 2004

It often amuses and pains me to see some of the Perl scripts that people write that are essentially shell scripts wrapped in Perl.

Perl is a great general purpose language, but there are times when a shell script is more appropriate for the particular task.


The following collection is sourced mostly from comp.lang.perl.misc. Names and references have been removed to protect the innocent.

use strict;
my @files = `ls -lart /app/EV4/data/db/u* | awk '{print \$NF}'`;
foreach my $file (@files) {
chomp ($file);
(my $date) = $file =~ m/.*\.(.*)/;
my @result = `grep anicol $file`;

I'm assuming the above code is a snippet from a larger script as $date is not used, so the below shell script conversion keeps it. I'm also assuming that the date order that the files are processed is important, so that has been kept. Note that there is no need for the -1l flags.


for file in `ls -rt /app/EV4/data/db/u*`
  date=`echo $file | awk -F. '{print $NF}'`
  result=`grep anicol $file`

Also see the Perl SAQ

Email Peter Sundstrom if you have some more examples.
Please include URL or message ID.