Traps for module writers [was: Re: anonymous proxy server in 100 lines of Perl]

Gisle Aas (aas@bergen.sn.no)
25 Nov 1996 16:57:18 +0100


Randal Schwartz <merlyn@stonehenge.com> writes:
> A properly written library is going to have to have:
> 
> 	eval {
> 		local($SIG{__DIE__});
> 		...;
> 	}
> 
> and if you *don't* have that, *you* are asking for trouble, since the
> user is free to write whatever they want as a die handler.

Yes, it is probably easier to do it like this than to tell people not
to use $SIG{__DIE__} :-(

Perhaps we could have a "catch" keyword which worked like "eval" but
localized $SIG{__DIE__} automatically?

I'll add it to my list of (silly) things that a module writer must
care about:

   If you use something like "while (<>) {...}" in your library,
   remember to localize $_ and ensure that $/ is what you
   expect before the loop is entered. Something like this:
      local($/, $_) = ("\n");
   will probably do the trick.

   Never print anything before you set localized default values for:
   $", $\, $#, $,:
      local($", $\, $#, $,) = (" ", "", "%.15g", "");

   Always set $*=0 (or something you like) before you do any regexp
   stuff that use ^ and/or $ (or always use /m or /s).

   Never trust perl to convert a string to a number unless you have
   made sure that $# contains something sensible.
      local($#) = ("%.15g");

   Localize $SIG{__DIE__} if you are going to use
      eval {...die...}
   as an exception mechanism.

   Don't use "multi-dimensional array emulation" (since perl5 has better
   ways) which means we don't care about $; having a stupid value.

   There might be a problem with $^I too, but I am not sure.

   Fortunately Larry depreciated $[, so you don't have to care
   about it any more.

Perhaps a list like this should go into perlmod(1) or perltrap(1).

Regards,
Gisle