Last active
June 8, 2026 22:12
-
-
Save s1037989/1575bec465f7c3a62fb4be960cc1145b to your computer and use it in GitHub Desktop.
Mojo::Log singleton in a Util package
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # $ MOJO_LOG_LEVEL=info perl -Ilib -Mojo -MUtil=sq -E 'BEGIN { $Util::log = app->log; } a("/:n" => sub { $_->render(text => sq) })->start' get /5 | |
| # 36 | |
| # $ MOJO_LOG_LEVEL=trace perl -Ilib -Mojo -MUtil=sq -E 'BEGIN { $Util::log = app->log; } a("/:n" => sub { $_->render(text => sq) })->start' get /5 | |
| # [2026-06-08 17:10:33.96373] [87592] [trace] 6^2 = 36 | |
| # 36 | |
| package Util; | |
| use Mojo::Base -strict, -signatures; | |
| use Exporter qw(import); | |
| use Mojo::Log; | |
| our @EXPORT_OK = qw(sq); | |
| our $log = Mojo::Log->new; | |
| sub sq ($n=6) { | |
| my $n2 = $n * $n; | |
| $log->trace(sprintf '%d^2 = %d', $n, $n2); | |
| return $n2; | |
| } | |
| 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment