The use warnings pragma can be extended, so you can control warnings (on/off/fatal) from a module.

That is really simple for a single category having the same name as the module, but you would need to generate additional catagory names for fine-tuning which things are warnings or not.

Another approach is to have a global $verbose flag in the module. This is easily extended to how ever many control variables are needed.

A hibrid approach is to use warnings for on/off, and a global module variable for fine tuning.

What's the best, proper way to do this?

Here's a deeper issue: shouldn't the warning state be scoped by caller? One module wants Win32::OLE to throw exceptions, another wants the same module to emit warnings but return. I don't think any common mechanism can handle this, can it?

As a design point, I'd say that what Win32::OLE does should be avoided: don't make major semantic changes to the function calls based on a mode; instead have multiple functions.

An exception to that would be versioning. One piece of code wants v1 and another wants v2. One way to handle that is to actually import different functions. But sometimes it would be simpler just to know what the caller wanted, via dynamic scoping.

I guess that's turned into more of a Meditation. But, has anyone else dealt with this?

—John


In reply to Warnings from reusable code by John M. Dlugosz

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.