I've just begun poking around the nms scripts, and am seeing something odd (to me) at the end of some of their files.

The files in question (guestbook.pl, for example) end with a long BEGIN block. I'm hoping someone here can explain a few details about what it's doing. Here's a sample (taken from guestbook.pl, edited for brevity):

(Edit: removed readmore tags)

#/usr/bin/perl -wT # ... {snip regular stuff (code + some POD)} ... # ... {and now, at the end of the file} BEGIN { eval 'local $SIG{__DIE__} ; require CGI::NMS::IPFilter'; $@ and $INC{'CGI/NMS/IPFilter.pm'} = 1; $@ and eval <<'END_CGI_NMS_IPFILTER' || die $@; ## BEGIN INLINED CGI::NMS::IPFilter package CGI::NMS::IPFilter; use strict; + require 5.00404; + use Socket; # ... {snip code interspersed with POD} ... 1; ## END INLINED CGI::NMS::IPFilter END_CGI_NMS_IPFILTER CGI::NMS::IPFilter->import(); }

Now, there seems to be a number of things going on here, a couple of which I might even understand :) :

  1. The author wants this BEGIN {...} code in the script to happen first, evidently in case the CGI::NMS::IPFilter module can't be found in the usual places.
  2. Why localize $SIG{__DIE__} in that first eval? So far, I've only seen %SIG used where you set a value to the name of a sub you want called in the event of getting the given signal. But we're not setting it to anything here...
  3. Why require instead of use here?
  4. If there's an error in running the short line of eval'd code, add a special value to $INC. Why set that $INC{...} value to 1 instead of a fully-qualified path name (say, the name of this current file)?
  5. Again, if something died during the first eval, eval this big single-quoted heredoc string (which is a module itself, taking up a couple of pages) -- and if *that* eval fails, just bail out.
    • Why doesn't the author simply distribute an external .pm file for this purpose? Is this a common practice?

What's the most common way of handling this situation, where you want either the installed module first, or else the one that you're including in your distribution? I've heard the term ``Orcish maneuver'', but I guess this is more of an ... hmm... maybe ``or just use this one here'' (the Ojutoh defense? ).


In reply to Odd convention? (fall-back module in BEGIN block) by j3

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.