Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Simple CLI calculator based on Perl's eval()

by kcott (Archbishop)
on Mar 04, 2021 at 04:27 UTC ( [id://11129107] : note . print w/replies, xml ) Need Help??

in reply to Simple CLI calculator based on Perl's eval()

G'day reisinge,

"Sometimes I need to do a quick calculation. I'm too lazy to open a GUI calculator ..."

Me, too. I have a file, .bash_functions, that I source from .bashrc, which contains this function:

function perls () { eval "echo \`perl -T -Mstrict -Mwarnings -Mautodie=:all -E ' my \$ev = eval \"$@\"; say defined \$ev ? \"\$ev\" : \"$@\"; '\`" }

Here's some examples. The first two show normal operation; the third shows error handling.

$ perls 6*7 42 $ perls 196**.5 14 $ perls 196***.5 Number found where operator expected at (eval 6) line 1, near "*.5" (Missing operator before 5?) 196***.5

Obviously, that has less functionality than ++yours; however, it has served me well for quick calculations on the command line.

In anticipation of the question, "What does the 's' stand for?"; the answer is, "I don't remember". I've been using that function for probably a decade or more; the meaning of 's' has been lost in the mists of time. :-)

— Ken

Replies are listed 'Best First'.
Re^2: Simple CLI calculator based on Perl's eval()
by LanX (Saint) on Jun 02, 2021 at 10:38 UTC

      G'day Rolf,

      I have a whole series of functions and aliases of the form perlX, where X is a lowercase letter. Here's a couple of examples.

      This one I use almost every day. It's been in my toolbox since the mid '90s. It probably started as a simple contraction of perl -e; then -e became -E; strict and warnings were added; and so on. The -MCarp::Always is a fairly recent addition.

      $ alias perle alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -MCarp::Always -E +'

      This one's for Unicode work.

      $ alias perlu alias perlu='perl -Mstrict -Mwarnings -Mautodie=:all -Mutf8 -C -E'

      These are all for my own personal use. As I've added more over the years, some obvious letters were already taken so alternative (less obvious) ones were used. Only having to remember a single letter is easy for me and not confusing at all; in fact, it's mostly muscle memory. Remembering the historical reasoning behind the choice of a letter is far less important and, in some cases, gets forgotten.

      Addendum: It looks like you've hard-coded the "less confusing" link to Following that link puts me in a logged-out (Anonymous Monk) state. Please fix by changing that to [id://11133350|less confusing].

      — Ken

        > Please fix by changing that to [id://11133350|less confusing]

        Ah thanks for pointing this out, it's a bug in my wikisyntax.

        I'll add it to the to-do list :)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery