If you're really feeling "paraniod", you have to be very careful not to fall in the "defensive programming" mode which can be very damaging. With defensive programming, your function which should do something, but receives bad input, does nothing. You just created a ticking time-bomb, because sooner or later some other function is going to set it off, and you'll have no idea where the problem came from.

Consider:
sub OpenSomething { my ($param) = @_; # What kind of an idiot would forget the foo parameter! return unless (defined $param->{foo}); my $thing = new Object ($param->{foo}, $param->{zoo}); $thing->open($param->{foozoo}); return $thing; } sub CloseSomething { my ($thing) = @_; $thing->close(); }
Now, in your program you will call OpenSomething(), which might actually return nothing. You forget to check if you got anything back, so later on when you call CloseSomething() you get an error. The error, it would seem, originates in CloseSomething().

Of course, you could just get more defensive:
sub CloseSomething { my ($thing) = @_; # Maybe they forgot to pass the parameter? return unless (defined $thing); $thing->close(); }
Now what you have done, effectively, is swept all these errors under the carpet. They don't manifest, and they don't cause "errors" in the visible sense, but there may be problems with the way your program runs that doesn't make any sense.

It is probably better to die and get it over with:
sub OpenSomething { my ($param) = @_; # What kind of an idiot would forget the foo parameter! die "No 'foo' param passed to OpenSomething()" unless (defined $ +param->{foo}); my $thing = new Object ($param->{foo}, $param->{zoo}); $thing->open($param->{foozoo}); return $thing; } sub CloseSomething { my ($thing) = @_; die "Cannot CloseSomething() with undefined parameter" unless (d +efined $thing); $thing->close(); }
Using die in this capacity is similar to the ASSERT() macro used in C programming which will completely halt the program if something is seriously out of line.

If the train comes off the tracks, you'd best stop it.

In reply to Re^3: Newbies TCP Networking by tadman
in thread Newbies TCP Networking by otijim

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.