At 11:57 AM 1/19/02 +0100, Rafael Garcia-Suarez wrote:
>On 2002.01.18 23:13 Jonathan E. Paton wrote:
>> 
>> For those who haven't met RPN, or are looking for a Perlish
>> impliementation here is a better (working) one than I
>> posted earlier:
>[...]
>
>Just because it's fun, here's a very basic and short command-line RPN
>evaluator :
>
>#!/usr/bin/perl -lw
>"@ARGV"=~m{^(p(?{print$s[-1]})|(\d+)(?{push@s,$2})|([+-/*])(?{eval
>"\$s[-2]$3=pop\@s"})|\s)*$}g||die"Syntax error\n";
>__END__
>
>It does only +, -, *, / and p(rint), but it may be extended quite
>easily (at the expense of its conciseness).

Seems that it can be extended to arbitrary operators without losing 
any conciseness:

#!/usr/bin/perl -l
for(@ARGV){push@s,$_ and next if/\d/;/^p/&&print$s[-1]and 
next;eval"\$s[-2]$_=pop\@s"||die"!"}

$ ./foo 5 9 + p 3 % 5 / p 9 '**' p
14
0.4
0.000262144

-- 
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com

Reply via email to