Re: Interesting math
- From: msb@xxxxxxx (Mark Brader)
- Date: Sun, 15 Jul 2007 07:30:44 -0000
... I've learned a little more about Perl, and yeah, to someone
familiar with the old-time Unix toolkit (awk, sed, etc.), it's
clearly the right tool -- or *a* right tool -- for some jobs.
One thing I've learned is that it's often a superior language for
writing things you would otherwise write as shell scripts, if you
want them to be robust.
For example, «read x» in sh usually reads one line from stdin
unaltered into $x, but not if that line starts with whitespace.
«echo "$x"» usually writes the value of $x to stdout, but not if
$x is "-n," and/or not if it contains certain character sequences
with backslashes, depending on what shell you're using.
And if you want to do build up a command line, you can write
something like this:
while ...
do
case ...
...) opts="$opts -a";;
...) opts="$opts -b";;
...) opts="$opts -c";;
esac
done
$command $opts "$1" "$2"
but not if some the options you want to put in $opts might themselves
contain spaces. If you write «opts="$opts -C 20"» and invoke $command
the same way, it will get -C and 20 as two arguments, not one; and if you
write «"$opts"», it will get all the options as one argument. To make
this sort of thing work robustly in sh involves some serious gyrations.
In effect, a sh program has exactly one array available, which is "$@".
Another thing you might want to do in a shell script is
while ...
do
( ... ; ... ; ... ; p=`command` ) >file
done
echo command said $p
and that doesn't work either, because as soon as you use (...) to
redirect the output from several commands, you've created a subshell
and the $p in the subshell isn't propagated out.
Write the script in Perl instead, and all these problems just go away.
And you also have access to things like fork and stat and in general
anything you could do from a C program, or an awk program for that matter.
Oh, and one more thing -- a regexp engine with more features than any
other I have used. One of the simpler ones is that if you write a
substitution, which basically uses sed-like syntax, with the option /e,
the right-hand side is now an arbitrary Perl expression. So you can
do things like using a regexp to pick out significant numerical values
on a line, do arithmetic on those values, and substitute them back
into the line. For example, this:
s/([AB])(\d+)/$1 . ($2 + 1)/eg
will take all instances of A or B followed by some digits representing
a number, and increment the number.
--
Mark Brader | No programming language is Perfect. Perl comes very close.
msb@xxxxxxx | P! e! r! *l?* :-( Not quite "Perfect".
Toronto | -- Brian Ingerson
My text in this article is in the public domain.
.
- Follow-Ups:
- Re: Interesting math
- From: blmblm
- Re: Interesting math
- References:
- Interesting math
- From: Skitt
- Re: Interesting math
- From: blmblm
- Re: Interesting math
- From: Jitze Couperus
- Re: Interesting math
- From: blmblm
- Interesting math
- Prev by Date: Re: Dancer
- Next by Date: Re: Slightly OT - P.A.Systems, Mangoes, and High-Speed Trains
- Previous by thread: Re: Interesting math
- Next by thread: Re: Interesting math
- Index(es):
Relevant Pages
|