What is 'expect'

Tom Christiansen tchrist at convex.COM
Sat Nov 24 08:22:07 AEST 1990


In article <5808 at stpstn.UUCP> lerman at stpstn.UUCP (Ken Lerman) writes:
>No, I am not disparaging perl, because I don't know enough about it.
>But from the little I know about perl, I don't believe it solves what
>I perceive to be the problem.  What we (or I) need is a simple,
>elegant, extensible tool that is easy to learn and use.  Do people
>seriously claim that perl is it?  If so, then perhaps it is time for
>me to spend some time with my perl manual.

Perl meets most of your criteria; the one it doesn't really satisfy
is that of elegance.  Quoting from the 3rd sentence of its man page:

    The language is intended to practical (easy-to-use, efficient,
    complete) rather than beautiful (tiny, elegant, minimal).

So if you value elegance beyond all other virtues, then you might be a
trifle scandalized by some of "features" lurking in dim corners of 
perl's baroque periphery.  

But if you want something that's easy to learn and use, as well as
extensible and quick, then I think you might be doing yourself a favor
by sitting down and looking at it.  It may be pathologically eclectic,
but it gets the job done, and well.

At its most basic core, perl is about text processing.  You can do 
a lot more with it than that, but this is the problem-area perl was 
developed to address.  

For example: if you look around at languages that recognize the symbols $1
through $9, you'll see these are used for the thing nearest to that
language's main purpose.  The shell sees $1 through $9 as command
arguments; troff has them as macro arguments; yacc uses them in its
parsing of productions; awk sees them as fields on each input line.
Perl, however, uses those for subexpressions in the last pattern match.

Here's a snippet of a perl program that shows you how the pattern
matching has been merged in the with control flow in a way that
I consider elegant, and which you should at least consider expedient:

    $now = `date`; # note backticks
    while ( <FILE> ) {  # read a line into pattern space
	if (! s/red/blue/g) {
	    print STDERR "i changed no red to blue on $now\n";
	    next;  # like "continue" in C
	} 
	if ( /^flag: (.*)/ ) {
	    print STDERR "saw flag: $1\n";
	    $flags{$1}++;
	    s/$&//;  # $& is all of last match
	} 
	if (/one: (.*) two: (.*) three: (.*)/) {
	    do some_function($2, $3, $1);
	    if ($flags{'silver'} || $flags{'gold'}) { 
		tr/a-z/A-Z/; # capitalize
		s/$/wow!/;   # append wow! to pattern space
	    }
	} 
	print;  # print current pattern space
    } 

This doesn't really do anything useful, but it shows how the control
flow and pattern operations can be used together.  It also shows you
how perl's quoting is like the shells, and introduces you the other of
the two most powerful aspects of perl: the arrays that you can
subscript with strings, as you can in awk.  You can often use one
reference to an array of this sort instead of a big, long looping
algorithm in other languages.

Was the code above too bizarre to even contemplate reading and
understanding, or did you basically understand what was going on?  I've
taught perl to hundreds of people with some prior experience in other
UNIX tools (sh, awk, sed, C) and they've all taken to it pretty quickly.

This is enough to get you going, but there is more that you'd want to
read up on eventually, because I didn't even mention how to do other
kinds of file handling and I/O, subroutine and local variables, niftier
regular expression stuff, or how your arrays can be bound to dbm files
for persistent data and processing certain system databases quickly.

Perl is by no means the right tool for every possible problem, but when
it comes to simple text processing and report generation, it works
quite well as a Practical Extraction and Report Language.  There's a
pretty active discussion and question/answer group next door in
comp.lang.perl; you might want to check it out.

--tom



More information about the Comp.unix.shell mailing list