in reply to Re: Simple command line calculator
in thread Simple command line calculator

Heathen!

Why not Perl's interactive mode? perl -debug. {grin}

-- Randal L. Schwartz, Perl hacker

  • Comment on •Re: Re: Simple command line calculator

Replies are listed 'Best First'.
Re: •Re: Re: Simple command line calculator
by Juerd (Abbot) on Mar 14, 2002 at 20:22 UTC

    perl -ple'$_=eval'
    is a bit easier to use :)

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

      How would you pass parameters to that expression?
      Rich36
      There's more than one way to screw it up...

        How would you pass parameters to that expression?

        2;130 juerd@ouranos:~$ perl -ple'$_=eval' 1+1 2 3*100 300

        U28geW91IGNhbiBhbGwgcm90MTMgY
        W5kIHBhY2soKS4gQnV0IGRvIHlvdS
        ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
        geW91IHNlZSBpdD8gIC0tIEp1ZXJk
        

      You can make this take a command argument very easily. In fact you can replace the expr function with this:
      function calc { perl -ple '$_=eval' } function expr { echo "$1" | calc }
      Toku

        How about a Perl solution?

        perl -le'print eval shift'
        Or, to slurp all arguments:
        perl -le'print eval "@ARGV"'

        U28geW91IGNhbiBhbGwgcm90MTMgY
        W5kIHBhY2soKS4gQnV0IGRvIHlvdS
        ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
        geW91IHNlZSBpdD8gIC0tIEp1ZXJk
        

Re: •Re: Re: Simple command line calculator
by premchai21 (Curate) on Mar 15, 2002 at 20:17 UTC

    The Perl debugger interface, to me, is too cluttered for this purpose, especially considering the -- what are they, ANSI control codes? -- formatting. Python's interactive mode is somewhat better, but Python doesn't do math quite as well as Perl, I think. Interactive Ruby (irb), out of the three, is IMHO the best; as two examples, Ruby automatically switches to bignums when necessary, and is not as whitespace-dependent as Python (important in interactive mode)... the major disadvantage is that it is much less common than Perl and Python, or so I've seen.

      The Perl debugger interface, to me, is too cluttered for this purpose, especially considering the -- what are they, ANSI control codes? -- formatting.

      ANSI codes, yes, because it's using Term::ReadLine::Gnu and Term::ReadLine (on my system at least) - i think they look cool, myself. (:

      of course, if you want an interactive calculator, the Term::Readline manual has a nice example.

      --
      strfry()