http://www.perlmonks.org?node_id=243550

coolmichael has asked for the wisdom of the Perl Monks concerning the following question:

To quote an email I just wrote:

I'm quite fond of a command line interface directly to perl:
perl -e "print eval, \"\n\" while(<>);"
which would look nicer in a non-dos shell with better quoting possibilities. And it can be even shorter
perl -p -e "$_=eval"
but then you don't get new lines after each answer.
perl -p -e "s/(.*)/$1/ee"
also works very well, but is longer.

I've never understood s///e very well before, but this just occurred to me while I was writing it. And it worked. I was amazed.

The big question is, why does it work. I don't really know.

--
negativespace.net - all things inbetween.

Replies are listed 'Best First'.
Re: A step on the path to enlightenment
by Chmrr (Vicar) on Mar 17, 2003 at 00:18 UTC

    Each /e tacked onto the end is essentially doing another eval. The first eval evaluates $1, which gets it what was matched -- namely, the whole string. The second /e applies eval to what we just got -- the whole string. Thus, it evaluates whatever you typed.

    As a side note, you can have your cake and eat it two with your second option -- perl -ple "$_=eval" will give you your newline.

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: A step on the path to enlightenment
by grantm (Parson) on Mar 17, 2003 at 04:25 UTC
    perl -p -e "$_=eval"
    but then you don't get new lines after each answer

    You could use -l to tack on the newlines:

    perl -lpe "$_=eval"
Re: A step on the path to enlightenment
by Aristotle (Chancellor) on Mar 17, 2003 at 12:58 UTC
    And of course you can also use -l to shorten any other variants. -n might also be of interest.
    perl -le "print eval while <>" perl -nle "print eval" perl -ple "$_=eval"
    Personally, simply for the sake of clarity, I'd prefer the -n variant.

    Makeshifts last the longest.