IMPORTANT NOTE: Please also read the README.whats-new to read about newly incorporated features. "calc" is a simple command-line perl calculator. It reads a line of input from the user, evaluates it into the variable $a, pushes the results onto a stack, and displays the stack, until the user enters "quit" instead of an expression. Here is an example session (the user prompt is "> ", and the stack elements are labelled "a"): > 1 + 2 a00 = '3' = 1 + 2 > 3 - 1 a00 = '3' = 1 + 2 a01 = '2' = 3 - 1 > a0 + a1 a00 = '3' = 1 + 2 a01 = '2' = 3 - 1 a02 = '5' = a0 + a1 > $a + 6 a00 = '3' = 1 + 2 a01 = '2' = 3 - 1 a02 = '5' = a0 + a1 a03 = '11' = $a + 6 > quit Alternatively, calc may be called with a command line argument, which is evaluated and printed. Calc then exits, eg: ttk@typhon:~> calc '1 + 2' 3 ttk@typhon:~> calc 'units("foot","inch")' 12 Okay, that's the simple side of it, but calc also has a variety of sophisticated features: Perl Evaluation What calc does to evaluate the user's input is munge it slightly, prefix it with "$a = ", and then pass it to perl's "eval()" function, which interprets its argument as perl code. This gives calc the ability to act as an interactive perl interpreter. If the user input begins with a simple expression which combines harmlessly with "$a =" (I like to use "1;") and then continues with arbitrary perl code, the perl code will be evaluated and executed. I normally keep an xterm running at the bottom of my screen with calc running, and I often find it handy for testing perl expressions while I'm writing "real code", just to make sure I have the logic or syntax right. EG: > "http://www.ciar.org/ttk" =~ /^[a-z]+:\/\/(.+?)\/?.*$/ a00 = '1' > 2; for ( $i = 0, $j = 0; $i < 5; $i++ ) { $j += $i; print ("$i: $j\n"); } 0: 0 1: 1 2: 3 3: 6 4: 10 a00 = '1' a01 = '2' > For your convenience, all of calc's internal variables' names are prefixed with "CALC_", so that any variables you use in your fragments of code will not interfere with calc's operation -- with the sole exception of "$a", for reasons of convenience. The Stack The stack is one of calc's handier features. The results of every calculation it makes gets pushed onto the stack, so that the user may refer back to previously-calculated figures. The stack can grow indefinitely, but only the first 18 values will be displayed (though this can be changed by loading a different value into $CALC_max_k at the command prompt). Calc provides a streamlined method for accessing stack elements. Before the user's input is evaluated, any instance of "a<#>" (ie, matched by regex /(a[0-9]+)/ and not immediately adjacent to any alphabetic character) gets converted into a stack reference. EG: > 1 + 2 a00 = '3' = 1 + 2 > 2 + 3 a00 = '3' = 1 + 2 a01 = '5' = 2 + 3 > a0 + 4 a00 = '3' = 1 + 2 a01 = '5' = 2 + 3 a02 = '7' = a0 + 4 > a1 + a2 a00 = '3' = 1 + 2 a01 = '5' = 2 + 3 a02 = '7' = a0 + 4 a03 = '8' = a1 + a2 > Calc also allows the user to delete ranges of the stack: a00 = '3' = 1 + 2 a01 = '5' = 2 + 3 a02 = '9' = a0 + a1 + 1 a03 = '8' = a1 + a2 > d1-2 a00 = '3' = 1 + 2 a01 = '8' = a1 + a2 > d0 a00 = '8' = a1 + a2 > And move selected values to the bottom of the stack: a00 = '3' = 1 + 2 a01 = '4' = 2 + 2 a02 = '5' = 2 + 3 > m2 a00 = '5' = 2 + 3 a01 = '3' = 1 + 2 a02 = '4' = 2 + 2 > m1 a00 = '3' = 1 + 2 a01 = '5' = 2 + 3 a02 = '4' = 2 + 2 > Finally, calc allows users to save and restore the stack, via the "s<#>" and "r<#>" commands ("save" to disk file "~/.calc.state.<#>" and "restore" from disk file "~/.calc.state.<#>"): a00 = '4' = 2 + 2 a01 = '3' = 1 + 2 a02 = '5' = 2 + 3 > s1 a00 = '4' = 2 + 2 a01 = '3' = 1 + 2 a02 = '5' = 2 + 3 > quit ttk@typhon:~> calc > r1 a00 = '4' = 2 + 2 a01 = '3' = 1 + 2 a02 = '5' = 2 + 3 > "the quick brown fox jumped over the lazy dogs" a00 = '4' = 2 + 2 a01 = '3' = 1 + 2 a02 = '5' = 2 + 3 a03 = 'the quick brown fox jumped over the lazy dogs' > Command History Calc keeps a complete log of user input, saves history across sessions (to the disk file "~/.calc.history"), and allows the user to review and repeat past calculations via shell-like operators: a00 = '3' = 1 + 2 a01 = '5' = 2 + 3 a02 = '8' = a0 + a1 a03 = 'the quick brown fox' = "the quick brown fox" a04 = '8' = a0 + a1 > history 0: 1 + 2 1: 2 + 3 2: a0 + a1 3: "the quick brown fox" 4: a0 + a1 > history 2 3: "the quick brown fox" 4: a0 + a1 > a0 + a4 a00 = '3' = 1 + 2 a01 = '5' = 2 + 3 a02 = '8' = a0 + a1 a03 = 'the quick brown fox' = "the quick brown fox" a04 = '8' = a0 + a1 a05 = '11' = a0 + a4 > !! a00 = '3' = 1 + 2 a01 = '5' = 2 + 3 a02 = '8' = a0 + a1 a03 = 'the quick brown fox' = "the quick brown fox" a04 = '8' = a0 + a1 a05 = '11' = a0 + a4 a06 = '11' = a0 + a4 > history 4 3: "the quick brown fox" 4: a0 + a1 5: a0 + a4 6: a0 + a4 > !3 "the quick brown fox" a00 = '3' = 1 + 2 a01 = '5' = 2 + 3 a02 = '8' = a0 + a1 a03 = 'the quick brown fox' = "the quick brown fox" a04 = '8' = a0 + a1 a05 = '11' = a0 + a4 a06 = '11' = a0 + a4 a07 = 'the quick brown fox' = "the quick brown fox" > Function Stock Sometimes when using (abusing?) calc to run slightly more complex perl statements, it is handy to have a set of functions available for the user's use. Such functions can be placed into the file "~/.calc.code". I've prepopulated this file with a few simple functions. Example of use: > padi(1+2,4) a00 = '0003' > pads("dog", 4) a00 = '0003' a01 = ' dog' > TickerTape I often like to keep a record of all of the calculations I ask calc to perform, so that I can include it in another document to show how I derived at a figure, or review it later if I think I have made a mistake. So calc keeps a running log of inputs and results, like a virtual ticker-tape, in "~/.calc.tickertape", eg: ttk@typhon:~> tail -4 .calc.tickertape > padi(1+2,4) 0003 > pads("dog", 4) dog A note: it is handy to insert occasional comments into the ticker-tape to remind yourself of what it is that you're doing, so you're not left scratching your head over it later. User input that begins with a "#" will be interpreted as a comment, and not evaluated, but will appear in the ticker-tape with a timestamp of the event, eg: ttk@typhon:~> calc > # Calculating the mass of plate 3 > quit ttk@typhon:~> tail -4 ~/.calc.tickertape > pads("dog", 4) dog # COMMENT Mon Jul 22 16:18:25 2002 / 1027379905 # Calculating the mass of plate 3 IMPORTANT NOTE: Please also read the README.whats-new to read about newly incorporated features. ANOTHER IMPORTANT NOTE: Don't look at calc's source code -- it is guaranteed to make your eyes bleed. It was one of the first applications I ever wrote in perl, before I really knew perl at all well, and it evolved very haphazardly. Future Features Calc is very much a work in progress, which is only to be expected. I use it a lot, and as I need more features I code them up. Things I will get around to eventually include: * Proper handling of expressions which evaluate to a list. * Incorporation of an SQL client. * Recognition of non-rval perl expressions, so that the user doesn't have to prefix perl statements with a "1;". * More tcsh-like (and less-quirky) history operators. * A way to tell calc not to load more than N history lines on startup. * Enabling calc to insert new functions to .calc.code automatically when one is entered on the command line (and evaluates correctly). * Will probably split off a "push" Perl Users' Shell project sometime, which will see calc reworked to behave like a command shell (a la /bin/sh, /bin/tcsh, et al). UPDATE: This was started as "calc3". If you have questions/comments/suggestions, please let me know. I can be reached at .