Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

A step on the path to enlightenment

by coolmichael (Deacon)
on Mar 16, 2003 at 23:58 UTC ( [id://243550]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://243550]
Approved by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-12-14 16:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (70 votes). Check out past polls.