home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!darwin.sura.net!gatech!destroyer!cs.ubc.ca!uw-beaver!fluke!inc
- From: inc@tc.fluke.COM (Gary Benson)
- Subject: Re: PERL
- Message-ID: <1993Jan8.175041.5709@tc.fluke.COM>
- Keywords: perl
- Organization: John Fluke Mfg. Co., Inc., Everett, WA
- References: <1993Jan6.212656.23814@lynx.dac.northeastern.edu>
- Date: Fri, 8 Jan 1993 17:50:41 GMT
- Lines: 119
-
- In article <1993Jan6.212656.23814@lynx.dac.northeastern.edu> efroyman@lynx.dac.northeastern.edu (Ella Froyman) writes:
- >Hi netters;
- >
- >I am new to perl and can you clarify some things for me:
- >
- >(1) What does a $variable| mean?
- >(2) What does $source++ do?
- > !(system ("mt -f $source fsf 1") >> 8) || &tape_fail();
- >
- >Thanks I know this are basic questions but i am new to PERL and
- >languages.
- >
- >
- >Send all replies to my email
- >Ella
-
-
- I did send an answer via email, but then a local expert I had asked for help
- in figuring out that last line suggested I should post a reply, too. Here is
- pretty close to what I said in my email to Ella:
-
- First I asked for assurance that this was not a *HOMEWORK* assignment :-)
-
- 1. $variable| probably means that the value of $variable is going to be a
- command whose output will be piped back into the calling program. I
- explained that the | symbol is also used for the bitwise OR operator, to
- indicate alternatives in regular expressions, and as a centering
- specifier on picture lines in formats, but that in none of those cases do
- you typically see a variable. I told her that for a precise answer, she
- should supply at least the entire line that the chunk appeared on, and
- preferably a few surrounding lines, too. To illustrate the use, I showed
- her the following two lines from R. Schwartz' zap program:
-
- $pscmd = $BSD ? "ps -auxww" : "ps -ef";
- open(PS, "$pscmd|") || die "can't run $pscmd: $!";
-
- I explained that the first line sets the ps command variable ($pscmd) to be
- "ps -auxww" if the system is a BSD one (or at least if the variable BSD is
- defined, which presumably the program assures only happens in a BSD system),
- but anyway. If the system is not a BSD one, the ps command will be "ps -ef".
-
- In the second line, a filehandle called PS is opened to catch the output of
- the ps command, accomplished by ending the filename associated with the
- filehandle with the pipe symbol. Or die.
-
- I should have also mentioned that this "filename" really is a filename
- usually, but that a pipe symbol on one side or the other tells the 'open'
- function to interpret it as a command whose output will be piped in to the
- program (as it does in this instance), or to which the program will pipe
- output, if the pipe comes first. In addition, real filenames can have other
- such symbols associated with them by 'open': > to open the file for output,
- >> for opening in append mode, and < to open it for input.
-
- (I did mention that both the if-then-else shorthand in the first line and
- the open-filehandle-piped command idiom on the second line are valuable
- tools to remember, and that she would probably find herself using them
- frequently. I also told her that if this is indeed a *homework* assignment,
- she should demand extra credit for uncovering these :-)
-
- 2. $source++ just means to increment the value of the variable by one, and
- is similar to ++$source, except with regards to when it performs the
- increment. I forgot to mention that this autoincrement operator has a
- little built in magic... if the value of the variable is a string, rather
- than a number, "A" increments to "B", and "zz" to "aaa", and so on.
-
- 3. I was not sure why Ella had included the line
-
- !(system ("mt -f $source fsf 1") >> 8) || &tape_fail();
-
- in her posting, but I assumed she wanted to know what it did. So I
- called up a local Perl guru, and after I thought I understood it, I
- wrote something like the following translation:
-
- Ignore the ! negation at the beginning for a moment. The "system" is to
- execute the program "mt", which I discovered is a magnetic tape drive
- control program. My man page did not indicate the meaning of the -f
- option, but I presumed it to mean "force" as it frequently does in unix
- programs; in other words, finish running even if errors occur. This mt
- program will run on the device specified in the variable $source, and
- fast forward until it reaches the first end-of-file marker (fsf 1). It
- looks like this will cause the first file encountered to be skipped.
- (Perhaps a preamble?) The output of the system command will be shifted
- right 8 bits by the bitwise >> operator; therefore the exit status will
- be the result of evaluating this entire expression (system ... 8).
-
- The resulting number is 0 if the mt command succeeded, and non-zero
- otherwise. Recalling that this entire expression is to be negated, a
- non-zero value then becomes true, so the OR part is not evaluated
- (|| &tape_fail() means "or run the subroutine tape_fail). However, if
- the value of the shifted right status is non-zero, this means that the
- mt command failed. When non-zero is negated, it becomes false, so the
- program should evaluate the OR conditional and do that subroutine.
-
- In retrospect, I could have probably figured it out myself if I had kept
- the words of L. Wall in mind, "In general, when confronted by a complex
- expression, analyze it from the inside out to see what order things
- happen."
-
- I thanked her for her posting because it taught me something, and because it
- gave me an opportunity to give back some of the HUGE amounts of help I
- myself have received in studying Perl. I may not have explained all this in
- perfect technical terms, but it makes sense to me, and I hope it will also
- make sense to Ella. Please correct any errors I have made here, and expand
- on anything that may be unclear.
-
- As I rewrote this, it occurred to me that I should also mention one other
- piece of information that all new Perl learners should be aware of:
-
- ISBN 0-937175-64-1
-
- This is the number of the book called "Programming Perl" by Larry Wall and
- Randal Schwartz, a couple of guys who seem to know quite a bit about the
- subject. It has a camel on the front. Get two copies.
-
- --
- Gary Benson -_-_-_-_-_-_-_-_-_-inc@sisu.fluke.com_-_-_-_-_-_-_-_-_-_-_-_-_-_-
-
- There are things that are so serious that you can only joke about them.
- -Heisenberg
-