[comp.lang.perl] gag me with an inappropriate tool

vixie@decwrl.dec.com (Paul A Vixie) (06/15/90)

Which is more readable?

	BF3> perl
	$a=(((((6+3)*2)-7)/2)*1)+9;
	print $a,$/;
	14.5

or

	[volition:mips] bc
	(((((6+3)*2)-7)/2)*1)+9
	14
	scale=1
	(((((6+3)*2)-7)/2)*1)+9
	14.5
	[volition:mips]

Perl is the best thing going for the things it's suitable for.  I can't
think of a string or file-as-database application I wouldn't prefer it
for.  But for a calculator, I think it's unwieldy.

Someone is probably working on an Emacs in Perl as I type this... :-)
--
Paul Vixie
DEC Western Research Lab	<vixie@wrl.dec.com>
Palo Alto, California		...!decwrl!vixie

merlyn@iwarp.intel.com (Randal Schwartz) (06/15/90)

In article <VIXIE.90Jun15002733@volition.pa.dec.com>, vixie@decwrl (Paul A Vixie) writes:
| Which is more readable?
| 
| 	BF3> perl
| 	$a=(((((6+3)*2)-7)/2)*1)+9;
| 	print $a,$/;
| 	14.5
| 
| or
| 
| 	[volition:mips] bc
| 	(((((6+3)*2)-7)/2)*1)+9
| 	14
| 	scale=1
| 	(((((6+3)*2)-7)/2)*1)+9
| 	14.5
| 	[volition:mips]
| 
| Perl is the best thing going for the things it's suitable for.  I can't
| think of a string or file-as-database application I wouldn't prefer it
| for.  But for a calculator, I think it's unwieldy.

You just didn't attack it the right way...

$_ = "6+3*2-7/2*1+9";
1 while s#^\d+(\.\d+)?[-+*/]\d+(\.\d+)?#eval $&#e;
print;

Think of numbers as strings.  It helps.  :-)

$_ = <<'-- '; s/../printf "%c",hex($&)/ge;
4a75737420616e6f74686572205065726c206861636b65722c
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

schaefer@ogicse.ogc.edu (Barton E. Schaefer) (06/16/90)

In article <1990Jun15.161531.1171@iwarp.intel.com> merlyn@iwarp.intel.com (Randal Schwartz) writes:
} In article <VIXIE.90Jun15002733@volition.pa.dec.com>, vixie@decwrl (Paul A Vixie) writes:
} | 
} | Perl is the best thing going for the things it's suitable for.  I can't
} | think of a string or file-as-database application I wouldn't prefer it
} | for.  But for a calculator, I think it's unwieldy.
} 
} You just didn't attack it the right way...
} 
} $_ = "6+3*2-7/2*1+9";
} 1 while s#^\d+(\.\d+)?[-+*/]\d+(\.\d+)?#eval $&#e;
} print;
} 
} Think of numbers as strings.  It helps.  :-)

Anybody want to drastically improve the following 10-minute effort?

#! /usr/bin/perl
# calc -- provide simplified calculator operations
# 02/25/84
# mhfoster
#
# 04/23/84 Add pipemode ability
#
# 04/06/90 Convert from csh/awk to perl
# Bart Schaefer
#
# Three methods of use:
#	calc 'exp'	-- returns a single evaluated expression
#	calc -		-- assumes 'pipe' mode, accepts <nl> delimited
#			   expressions on stdin, puts results on stdout
#	calc		-- enters interactive mode, allows entry
#			   of an expression at prompt ":". A null
#			   expression (<CR>) terminates.
#
# It is recommended that if the first form is used, the expression
# be enclosed in single quotes (') to prevent shell expansion 
# (a particular problem for multiplication).
#
# Examples:
#	calc '100.5 + 25*3'
#
#	calc
#	: 10/5+23.3
#	:
#
# The perl version has been hacked to provide support for subroutine defs.
# If the input expression read begins with the word "sub" it is evaluated
# directly, rather than attempting to assign its value to $_ for printing.
#

if ($#ARGV >= $[) {
    if ("$ARGV[$[]" eq "-") {
	$pipemode = 'on';
    }
}
if ($#ARGV >= $[ && (! $pipemode)) {
    eval "\$_ = @ARGV"; die $@ if $@;
    print "$_\n";
}
else {
    if (! $pipemode) {
	$| = 1;
	print "CALC [2.0]\n  Enter expressions to evaluate, <CR> to end\n";
	print ": ";
    }
    $eqn = <STDIN>;
    while ("$eqn" ne "\n") {
	if ($eqn =~ /^\s*sub/) {
	    eval $eqn;
	}
	else {
	    eval "\$_ = $eqn";
	    print "$_\n" unless $@;
	}
    }
    continue {
	warn $@ if $@;
	print ": " if (! $pipemode);
	$eqn = <STDIN>;
    }
}
-- 
Bart Schaefer						schaefer@cse.ogi.edu