2

The following command works as expected:

perl -lne 'print $1 if /install\(TARGETS (.*) RUNTIME DESTINATION bin\)/' CMakeLists.txt

This one works as well:

perl -nle 'print $1 if /install\(TARGETS (.*) RUNTIME DESTINATION bin\)/' CMakeLists.txt

However, this one errors out:

perl -nel 'print $1 if /install\(TARGETS (.*) RUNTIME DESTINATION bin\)/' CMakeLists.txt

The error message is:

Can't open print $1 if /install(TARGETS (.*) RUNTIME DESTINATION bin)/: No such file or directory.

What is the reason for this?

6

The -e option takes an argument, a string that will be evaluated as Perl code. When you use perl -lne '...' and perl -nle '...' this works as intended as the ordering of the options is not important (in this case).

However, when you use perl -nel '...', you say "evaluate the string l as Perl code, and use '...' as the name of a file to be read by that code", which makes little sense (this is why you get that strange looking "No such file or directory" error).

So, no, the order of the options is not important (here), but if you move the -e option around, you have to move the option-argument, the Perl code, with it, so that the Perl code always comes directly after that option.

So, if you want to put the -e option first, for example, use

perl -e '...' -ln

The order of the options to perl is important if you mix options like -p and -n on the same command line. A -p option will be overridden by a later -n option and vice versa, so perl -np is the same as perl -p while perl -pn is the same as perl -n. This is documented in the perlrun manual (see man perlrun or perldoc perlrun).

  • +1. it's worth noting that this is true for all commands with options that take arguments - the option's arg must immediately follow the option. The only almost-exception I can think of is when running tar with the "traditional" style options (e.g. tar cfz filename.tar.gz is allowed, but tar -c -f -z filename.tar.gz or tar -cfz are not because -f requires a filename arg....and that's an extreme case for backwards compatibility going back several decades). – cas Sep 17 at 3:08
3

From the perlrun manpage:

perl ... [ [-e|-E] 'command' ] ...

Whatever follows the -e option is treated by perl as a command. With -nel, l is the command for -e. print $1 if /install\(TARGETS (.*) RUNTIME DESTINATION bin\)/ is the first non-option argument, and it becomes the input file, which perl tries to open and fails.

Compare with, say:

~ echo  foo | perl -neprint
foo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.