[Melbourne-pm] programming tutorials: Perl

Jacinta Richardson jarich at perltraining.com.au
Tue Nov 16 04:25:55 PST 2010


john thornton wrote:

> a bad tutorial. I don't give up on any language easily. Except maybe
> bash with its 0 and 1 the wrong way around for truth tables.

Even this does actually make sense if someone explains why.  Consider the 
following problem:

	You are running a process.  Two broad categories of things can happen:

	1.  runs to completion and finishes successfully

	2.  it fails to run to completion or finish successfully for some reason.


In many ways, 1. is boring.  Either it succeeds or it doesn't.  True or false. 
However 2. is really interesting.  Did it get to completion?  If not, why not? 
  Why didn't it succeed?

If we flag success with 0, we can then leave open *all* of the positive integers 
as answers to what went wrong to cause the second situation.

For example; grep returns 0 to say it ran to completion and found the thing you 
were looking for.  It returns 1 to say it ran to completion but did not find the 
thing you were looking for, and 2 for all other situations (errors).  Thus if 
you're running grep and you only care whether the string exists in the file, you 
check it against 0.

On the other hand return values for mount are much more interesting:

        0      success

        1      incorrect invocation or permissions

        2      system error (out of memory, cannot fork, no more loop devices)

        4      internal mount bug or missing nfs support in mount

        8      user interrupt

        16     problems writing or locking /etc/mtab

        32     mount failure

        64     some mount succeeded

So, as you can see, success = 0 is logical if you want to make it possible to 
signal further information with your exit values.  Once you have this extra 
information you can write more robust scripts which can handle some possible 
errors rather than just giving up on failure because the reason for failure was 
unknown.

All the best,

	J


More information about the Melbourne-pm mailing list