in reply to Change Carp's output to Mojo::Log

If you look at the source of Mail::IMAPClient, you'll see this comment: "#local $SIG{__WARN__} = \&Carp::cluck; #DEBUG" which hints that the author uses $SIG{__WARN__} for this purpose (and Tk::Carp, pointed out by Corion, appears to work the same way). The following seems to work for me, though it's always worth noting that changes to $SIG{__WARN__} are global, so it's possible you might step on another module's toes or that one might step on yours. This is true even with local, unless you know exactly what the call stack looks like. You can of course limit the effects by using local $SIG{__WARN__} only in those blocks where you actually make calls to Mail::IMAPClient, that should help limit your exposure to such potential problems.

use warnings; use strict; package Foo { use Carp; sub bar { carp "Foo Bar"; } } use Mojo::Log; my $log = Mojo::Log->new; local $SIG{__WARN__} = sub { $log->warn(@_) }; Foo::bar();

Replies are listed 'Best First'.
Re^2: Change Carp's output to Mojo::Log
by bliako (Abbot) on Jun 09, 2022 at 20:27 UTC

    i have seen that but I did not dare to use it (especially with Mojolicious). Thanks++ for clarifying.