I frequently find myself leaving out 'use warnings' when developing CPAN modules to ensure backward compatibility for Perls prior to 5.6. However, that means that I'm also missing an important safety net that helps save me from myself as I develop.
Here are two ideas I had to work around this -- turning lexical warnings on for Perl 5.6 or better (i.e. where I develop) but not causing problems for older versions of Perl:
Use the if module to make warnings conditional:
use if (not $] < 5.006) => 'warnings';
Use a BEGIN block (essentially the same thing but without the dependency on if):
BEGIN { if (not $] < 5.006) { require warnings; warnings->import } }
Some quick tests seems to indicate that these both work -- at least for my Perl > 5.006.
Does anyone see any potential problems with this approach? If not, I'm probably going to add the latter to my module boilerplate.
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Backward compatible lexical warnings
by tilly (Archbishop) on Aug 30, 2006 at 22:16 UTC | |
|
Re: Backward compatible lexical warnings (don't)
by tye (Sage) on Aug 31, 2006 at 06:24 UTC | |
|
Re: Backward compatible lexical warnings
by ambrus (Abbot) on Aug 30, 2006 at 20:12 UTC | |
|
Re: Backward compatible lexical warnings
by Tanktalus (Canon) on Aug 30, 2006 at 20:04 UTC | |
by xdg (Monsignor) on Aug 30, 2006 at 23:04 UTC | |
by Anonymous Monk on Aug 30, 2006 at 21:31 UTC | |
by chromatic (Archbishop) on Aug 30, 2006 at 23:01 UTC | |
by Anonymous Monk on Sep 01, 2006 at 18:07 UTC |