Disable warnings from base module + load Moose/Test2::V0

89 Views Asked by At

In our app we have a common base module that enables strict, warnings and some other pragmas for every file that it used from. It also enables signatures and disables the warning about signatures being experimental:

sub import {
    warnings->unimport('experimental::signatures');
    feature->import('signatures');
}

When testing in CI with Perl 5.32 (which still considered signatures experimental) I still get a load of warnings about experimental because we loaded other modules like Moose or Test2::V0 after our base module.

use MyApp::Base;  # initial setup, enables signatures

use Carp;
use Moose;   # unconditionally enables experimental warnings again

Is there any way to permanently disable a specific warning regardless of other modules making use warnings in our scope?

I wanted to use our base class as first import in every code to allow all kinds of initial setup tasks. Do I really need to handcraft the order to move all Moose/Test2::V0/… imports above like this?

use Moose;    # these must come first
use Test2::V0;

use MyApp::Base;    # now we can run our common setup

use Carp;    # now we can import other stuff, too

How is this handled in other projects with a common base class?

1

There are 1 best solutions below

0
zdim On

Since you apparently do have to "unimport" after loading libraries that re-enable those warnings, why not have another module that does only such final configuration, in this case the needed un-importing? Then that one is loaded last.

A simplest example

package StopWarnings;
# Final configuration steps

use warnings;
use v5.32;

sub import {
    warnings->unimport('experimental::signatures');
    feature->import('signatures');
}

1;

A program that uses it

use warnings;
use v5.32;

use feature qw(signatures);  # instead of MyApp::Base

use Test2::V0;
use Moose;

use FindBin qw($RealBin);
use lib $RealBin;

use StopWarnings;  # when this is commented out there are warnings

sub greet($name) { say "Hello $name" }

greet('Prudence'); 

This is indeed yet another module to load but having a final configuration step makes some sense. With time, other last-thing configuration needs may find it useful.