In newer Perls there is "say" command that behaves like println:
$ perl -e 'use v5.11; say "qqq"'
qqq
but it's a bit cumbersome to use in oneliners, as one needs to declare version...
$ perl -e 'say "qqq"'
String found where operator expected at -e line 1, near "say "qqq""
$ perl -e 'print "qqq\n"'
qqq # but \n is easy for forget and "print" is longer...
Is there a way to enable say
without adding slashes (there can already by plenty of in the line) or moving cursor left to type use v5.11
in command line?
If calling perl from the command line, you can use the -E
flag
-E program
: like -e
, but enables all optional featuresAs demonstrated:
$ perl -E 'say "qqq"'
qqq
As an option to -E
, I use -l
, which makes print
work like say
(add a newline). I use this most of the time myself, and I find that it completely replaces say
.
$ perl -lwe'print "foo"'
foo
What it really does is set $\
to the current value of $/
, which causes the oddity that the command line option -0
affects -l
, which is something to look out for. The order of the switches matter, so that
$ perl -l -00 -e'print "hi"'
works as expected, but
$ perl -00 -l -e'print "hi"'
Does not (it sets $\
to "\n\n"
, for paragraph mode).
This latter case is practical when using paragraph mode, for re-printing the paragraphs easily. All in all, there are many benefits to using -l
.
Technically, print
is longer than say
, but my fingers already type print
automatically, and print
is in the actual case shorter than print^H^H^H^H^Hsay
.. (backspace that is)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With