[comp.lang.perl] Perl as a pocket calculator

markb@agora.uucp (Mark Biggar) (07/16/90)

Needing a calculator program the other day and not having anything like
bc available, I wrote a simple 6 line perl script that evaled and printed
each of its input lines.  After playing with it for a while, I noticed
that it all could be done very simply from the command line like so:

perl -pe "s/^.*$/eval $&/e;"

After I mentioned this to Larry he suggested using:

perl -pe "s/^.*$/(eval $&).$@/e;"

so any error messages would be printed out.  This is very useful
not only as a pocket calculator replacement, but as a tool for trying
perl stuff out when writing perl scripts.

--
Perl's Maternal Uncle
Mark Biggar

don@zip.eecs.umich.edu (Don Winsor) (07/17/90)

When running the one liner perl calculator described in
this group, I sometimes get a weird "bad free()" error:

% perl -pe 's/^.*$/(eval $&).$@/e;'
sin(2)
0.90929742682568170942
sin(3.14159)
Bad free() ignored, <> line 2.
2.6535897933527261422e-0
^D
%

The system is a Sun Sparcstation-1 (4/60) running release 4.1
of Sun OS.  The Perl was release 3.0, patchlevel 18, compiled
with the standard Sun C compiler.  I can replicate the problem
on other Sun SPARC systems running 4.0.3 and 4.1 Sun OS,
but it does not occur on Sun-3 (68020 CPU) or VAX systems.

I suspect a bug in Sun OS or perl, but
I'm not sure where to look at this point.

Don Winsor
Department of Electrical Engineering and Computer Science
University of Michigan, Ann Arbor
don@eecs.umich.edu