warnings-lock

 view release on metacpan or  search on metacpan

lib/warnings/lock.pm  view on Meta::CPAN

use strict;
use warnings;

package warnings::lock;
# ABSTRACT: Lock down the set of warnings active in a lexical scope

our $VERSION = '1';

use Variable::Magic 'wizard', 'cast';


my $hints_key = __PACKAGE__ . '/desired_warning_bits';

my $wiz = wizard set => sub {
    ${^WARNING_BITS} = $^H{$hints_key}
        if exists $^H{$hints_key};
};

sub import {
    $^H |= 0x20000;
    $^H{$hints_key} = ${^WARNING_BITS};
    cast ${^WARNING_BITS} => $wiz;
}

sub unimport {
    delete $^H{$hints_key};
}

1;

__END__

=pod

=head1 NAME

warnings::lock - Lock down the set of warnings active in a lexical scope

=head1 VERSION

version 1

=head1 SYNOPSIS

    # Set up warnings just the way we want them
    use warnings;
    use feature 'signatures';
    no warnings 'experimental::signatures';

    # Lock warnings into their current state
    use warnings::lock;

    # Import additional modules which might attempt to change our warnings
    # configuration
    use Moose;
    ...

    # Use of signatures feature will not warn
    sub greeting ($name) { print "Hello $name!" }

=head1 DESCRIPTION

This module allows you to lock down the set of warnings active within a lexical
scope. This is useful to protect yourself from other modules you C<use>
overwriting the warnings configuration you set up.

=head1 PROBLEM

Perl's L<warnings> are really useful. It's generally a good idea to turn them on
with C<use warnings>.

However, sometimes you might have a good reason to turn off specific warnings
which C<use warnings> enabled. One common example is to disable warnings for
experimental features: C<no warnings 'experimental::signatures'>.

For better or worse, it has become rather popular for certain kinds of CPAN
modules to try to ensure that their users have warnings enabled. While well
intentioned, this sometimes has unintended consequences.

If you import one of those modules, such as L<Moose>, after setting up your
warnings as described above, the warnings configuration will be reverted back to



( run in 0.887 second using v1.01-cache-2.11-cpan-ff9377addf4 )