Tom Duff about parameter expansion (Re: [dev] switching to rc: filename expansion ?)

From: Marc Chantreux <eiro_AT_phear.org>
Date: Tue, 26 Nov 2019 10:19:37 +0100

> I forgot, then remembered this old trick from the Rc paper, by Tom Duff [1]:
> [1]: http://doc.cat-v.org/plan_9/4th_edition/papers/rc

Thanks to this doc, i know who/why i have to blame for my love/hate
relationship with rc: i read the code of werc something like a decade
ago and really love most of the syntax but i never used it because
of its lack of parameter expansion [1].

i though it was an extremist version of the plan9 simplicity:
rc run commands, sed and awk edit text. period! on a theorical side, i
really subscribe but i feel inconfortable with the consequences:
but as a result, lot of subshells are executed to do the job done.
so because of rc not having parameter expansion, we have to
replace

    target=${source%md}.html

by

    target=`{ echo $source | sed 's/md$/html/' }

rc started a subshell, then a sed that have to parse
and execcute its script for *one* line.

maybe i worry too much (or for no good reason) about
performances but this made me run away.

but i read from Tom Duff in the document:

    Most of the other differences between rc and the Bourne shell are
    not so serious. I eliminated Bourne’s peculiar forms of variable
    substitution, like

        echo ${a=b} ${c-d} ${e?error}

    because they are little used, redundant and easily expressed in less
    abstruse terms.

He made a point saying that they are little used (not enough for me)
but to me, a good practice is to use them with the combination of
set -u. it makes the errors much easier to spot.

the third assertion, however, isn't backed up by exemple and i still
don't know how to convert

        ${c-d} ${e?error}

in "easily expressed in less abstruse terms"

the best i have is

    if (~ $c () ) c=d
    if (~ $e () ) { >[2=1] echo error ; exit 1 }

who's abstruse now ? i hope i missed something again.

also: he didn't mention the very useful other parameter expansions
so maybe those things

        ${file%.md}
        ${file##*/}

didn't exist when he wrote rc so maybe the reason rc didn't have
parameter expansions is historical.

regards
marc

1: As i already mention: zsh took a lot from rc but rc also inspired a
shell named `es`: the only one i know with the ability to deal with
anonymous fonctions.
Received on Tue Nov 26 2019 - 10:19:37 CET

This archive was generated by hypermail 2.3.0 : Tue Nov 26 2019 - 10:24:09 CET