I use this as a commandline calculator, but it can easily fit in any interactive program.
CAUTION: s///'ing does NOT make this safe. 123x123x123x123 will still let your box have a hard time.
#!/usr/local/bin/perl $a = "@ARGV"; $a =~ s/[^*.+\-0-9&|\)\(x\/\^]//g; # Unnecessary escapes are for my syntax hilighter. # Please don't nag about them :) $a =~ s/\*\*/^/g; $a =~ s/([*+.\/x-])\1*/$1/g; $a =~ s/\^/**/g; eval("print($a)"); print "\n";

Replies are listed 'Best First'.
(jeffa) Re: Simple calculator
by jeffa (Bishop) on Dec 17, 2001 at 21:05 UTC
    This little snippet reminds me of the old days when I was still in college. I was writting a Java applet that would graph a polynomial expression, and was having quite a hard time getting it finished. There were many conditions i had to consider ... one polynomial, two, or more ... (Java isn't very good at that sort of thing).

    Then, eduardo pops by my computer and says "Oh! You should be doing that with Perl and Tk!" And he promptly sat down at the computer beside me and started typing away (at about 75wpm, i might add).

    About 15 minutes later, he said "here, look at this" and i watched in amazement as his quick prototype whipped my Java applet's butt, in about 10 lines of code!

    After that, I was sold on Perl!

    I do have some suggestions for your code - first, please use strict. I know you don't need it, but for the sake of those new to Perl, it's just a good thing to demonstrate. Second, I would opt for my $a = join('',@ARGV); or even my ($a) = @ARGV; instead of my $a = "@ARGV";, simply because enclosing an array in quotes is confusing, again, to those new to Perl. Newbies see code like that and wonder why code like this doesn't do what they think it will:

    my $ref = { foo=>'bar' }; my $clone = "$ref";
    And last, this is a good candidate for the -l ( dash el, not dash one) flag:
    #!/usr/local/bin/perl -lw
    (yes, please use warning too) -l appends a new line at the end of your output, so this allows you to drop having to print a new line at the end. Laziness ;)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
      This was never intended to be used by anyone but me, but the snippets page dared me :)
      Actually, in a bot called cu2q, I've got this really nice !calc function, but I still need to get it out of there and into a separate script. It's hundreds of lines, and based on this lil' snippet.
      17:10 <Juerdje> !calc one thousand times the square root of threehundredandsixtytwo 17:10 <cu2q> 1000*sqrt 362 = 19026.2975904404 17:10 <Juerdje> !calc ~0 17:10 <cu2q> ~0 = 4294967295 17:10 <Juerdje> !calc -o:eng ~0 17:10 <cu2q> ~0 = fourmilliardtwohundredandninetyfourmillionninehundredandsixtyseventhou +sandtwohundredandninetyfive
      When I'm done, I'll try to have it strict and with warnings. And when that's done (somewhere next year), I'll put it on perlmonks :)

      2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

        wow, im searching for something similar for my bot. can i have it? pleease :)