Thursday, March 22, 2018

How Useful is Perl's -l? It Depends

You want to generate

1
2
3
4
5

If you didn't know about -l, you might do

$ perl -e 'foreach (1..5) {print; print "\n";}'

But, if you knew about -l, you could do :

$ perl -le 'foreach (1..5) {print;}'

To accomplish the same thing. Moral - ... make up your own moral. But, never stop learning!!

Now, what if you wanted to pipe this through a script to produce just

1

One way is

| perl -n -e 'print and exit if /\d/;'

Works, but doesn't

| perl -n -e 'print if ?\d?;'

Look prettier? What is the ?? version of the match operator? Only match once!

No comments: