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.
In reply to Backward compatible lexical warnings by xdg
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |