[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Is this fun? What's going on?



Quoting Randal L. Schwartz (merlyn@stonehenge.com):
> Using $? when you haven't had a recent child exit of your awareness
> is likely to give you confusing die messages.  Not a good practice.
> 
> Neither is using $! when you haven't really had a system-call related
> failure (although that doesn't apply here, since you did).

Yeah, I once spent about 20 minutes trying to figure out what caused the
error message:

Host lookup failed (No such file or directory)

I was tearing my hair out, screaming "but /etc/hosts is there!", frantically
strace-ing my script, etc. Then light dawned and I solved the problem by
removing the ($!) from my die() message.[1]

> Don't add too much to your error messages.  The virtue of Laziness
> does not apply to thinking about what's in the message.

I like to think laziness applies to typing, and future effort. So you don't
write repetitive code (because it would be less typing to use a
function/loop/hash), but you do write comments (because it'll save you
effort next year). Other definitions of "lazy" tend to make it difficult to
explain the existence of Perl in the first place.


[1] In case it isn't immediately obvious, the erroneous code was something
like

$address=gethostbyname($host) || die "Host lookup failed ($!)\n";

Since gethostbyname() (on Unix at least) is a library function instead of a
system call, it doesn't set $!. I just found out that it does set $? for
errors though (is that feature new? I don't remember seeing it before).

-- 
Adam Rice -- wysiwyg@glympton.airtime.co.uk -- Blackburn, Lancashire, England

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe