While learning Perl I am also learning Linux (Ubuntu), so it is kinda fire-hose sipping time around here.
What is the difference between:
find . -type f | perl -nle '... #aka yada yada'
and
perl -nle '... # same yada yada' `find . -type f`
The first passes the file NAMES to Perl and the second passes the file CONTENTS it seems. Is this always true under Unix or a special property of Perl?
The first one generates the list of files and “pipes” it to perl. perl then reads the list by reading from standard input:
This is a common thing to do in unix shells, so you don’t have to use perl at all:
The second one generates the list of file names and turns them into command-line arguments, which then show up in your program in
@ARGV:This is a feature is not particular to Perl either. The shell provides the bits after the command in some sort of data structure that the program can access. Other languages have similar constructs even if they don’t look the same.
However, the diamond operator,
<>will automatically go through the filenames you specify on the command line, so thatwhileloop still works. This is a feature particular to Perl.The problem with the second approach tends to show up when you have a long list of arguments. Some shells limit the number of things that can show up on the command line. I don’t like the second version as much just for that reason.
However, instead of using find(1) (the shell version), you can turn it into a self-contained Perl program:
The output is a Perl program that doesn’t have to rely on any external commands.