Right. Now, where was I?
Ah, yes. The meaning of Perl. We defined the word.
We named the author. We mentioned that it's free.
And that it's everywhere. We pointed out some
similarities to C, as well as some differences. We
touched briefly on the "regular expression" concept.
We talked about electric typewriters and text editors.
I also mentioned "implied variables" and finished up
with:
> What was that about "implied variables" up there?
> We'll talk about that as we go on.
So what does that mean?
Well ... let's talk about the way information is stored.
Let's say that we have some data. Names, addresses,
and the like. Maybe some word processing documents.
Possibly some programs or scripts (you could call them
"batch" files). How about some program configuration
settings? Or lists of network host addresses.
Some ways to store this information are 1) a proprietary
file format with no documentation telling you how to
create or process such a file -- although you can obtain
it -- for a price, 2) a space-efficient binary format -- with
integers stored as 16- (or 32-) bit objects, floating point
stored in native IEEE format -- often relying on hardware
dependent byte ordering, 3) plain old ASCII, with numbers
represented as strings of digits, and fields separated by
commas, tabs, spaces, or whatever.
... and the "implied variables" ... ? Patience, I'm
coming to that.
Remember the typewriters, teletypes, and other printing
terminals? Remember not having the glass screen? No
wysiwyg? The short story is that this leads to storing
nearly everything as text. Even the formatting commands
for a word processing document. There's a family of text
formatting tools whose names come from a shortened form
of "runoff" (roff) and "new runoff" (nroff) and "typesetting
runoff" (troff) and so on.
They format documents for given printers by interpreting
"dot" commands and "escapes" embedded in the text.
An example of a document fragment with nroff commands
(obtained from a page at Berkely):
--------------------
.ND
.nr LL 5.5i
.ds CH Hard Times - Chapter I
.ds CF - % -
.TL
Chapter I:
The One Thing Needful
.LP
Now, what I want is, Facts.
Teach these boys and girls nothing but Facts.
.B
Facts alone are wanted in life.
.R
Plant nothing else, and root out everything else.
You can only form the minds of reasoning
animals upon Facts: \Inothing else\R will ever
be of any service to them.
--------------------
The "dot" commands are fairly obvious: the
line begins with a period (dot) and one or more
letters forming the command. The "escapes"
are done with backslashes (\) and a single letter.
Don't get all hung up in what all the commands
mean. What's important is that, since they
are all written as ASCII strings, creating some
program or script to process such documents
becomes practical for mere mortals.
By the way, did you notice how, because it's
just all ASCII, you can edit this document with
ANY editor. There is nothing that forces you
to use an editor whose keyboard or display
interface makes you nuts.
In the typical Unix environment, most information
is stored as a series of ASCII strings, making
text processing a very useful thing. It is hardly
surprising, therefore, that a number of the standard
Unix tools (e.g. head, tail, more, cat, sort, paste,
man, grep, sed, awk, *roff ... and now Perl) make
their living doing just that.
Alright, so it's cool to be good at doing text.
What, please, has that got to do with this -- what
did you call it -- "implied variables" thing?
Ahh, well, yes. So we're writing this script, see,
and we're going to process a text file of, oh, let's
say 50,000 lines, and we have a number of things
that may have to be done to each (or any) line.
Using, say, Basic, we might have to do something
like this (creative syntax and keywords):
open "foo.txt" as 1
while not eof(1)
input textline$ from #1
A$ = somefunc(textline$)
B$ = otherfunc(A$)
A$ = yetanother(B$)
textline$ = okimdone(A$)
wend
... Notice that every operation has an assignment
associated with it. Quite naturally, the result of
any string manipulation has to go somewhere.
However Perl provides a default variable for "the
current line we're working on" whose name is
$_ (yes, just a '$' and a '_' together -- very terse).
What's more, unless you're doing something that
requires a named variable, you can, in many cases,
just state the string operation, and Perl undertands
that the operation takes place on the "current line"
or string. For example:
open (INFILE, "foo.txt"); # INFILE is our handle
while <INFILE> { # reads a line from foo.txt
s/london/London/g; # capitalize, leave result in $_
tr/a-z/A-Z/ if /^\./; # capitalize dot command lines
tr/{/(; tr/}/)/; # convert {...} to (...) in $_
}
... as you can see, there are no "explicit" assignments
in the above example. Perl just knows that when such an
assignment isn't used, that it places the results of that
command in the "current line" variable ($_).
Next, some samples. But first, a word from our Sandman.
Zzzzzzzzzzzz.
Always,
Garry
To unsubscribe from SURVPC send a message to [EMAIL PROTECTED] with
unsubscribe SURVPC in the body of the message.
Also, trim this footer from any quoted replies.
More info can be found at;
http://www.softcon.com/archives/SURVPC.html