Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching from `>` to `<` ruins my one-liner

Tags:

perl

I am filtering for lines with more than 3 items with

perl -ne  'print if split > 2' file.txt

But when I want to filter for lines with less than 3 items I can't use

perl -ne  'print if split < 2' file.txt
#Unterminated <> operator at -e line 1.

I can work around it with either of these

perl -ne 'print if not split > 2' file.txt
perl -ne 'print if 2 > split' file.txt

But I am wondering why the expression only fails for one of <,>.

like image 730
peer Avatar asked Jan 23 '19 13:01

peer


1 Answers

After a bit of digging, and looking at the comments on your post, I think I've found the answer. The parser is attempting to understand what you mean when it sees split <. Without parentheses on your call to split, it needs to guess how many arguments you are passing to split (It takes 0-3, see perldoc -f split for more information).

Because of this, it seems to assume by default that you are trying to call it with at least one argument, which looks like the start of the null filehandle <>. When it sees that it is incomplete, it prints the error and aborts.

This explains why the fixes commented on your post work. They all make it clear that split is to be called with no arguments, and the parser should not look for an expression, and the < character can then be treated as an operator.

like image 129
Marcus Avatar answered Oct 09 '22 12:10

Marcus