No::Worries::Warn(3)

πŸ“¦
version 1.7 Β· 4 versions
, in epel 9

πŸ’– No::Worries::Warn(3) β€” warning handling without worries πŸ’–

okay so bestie, this is a page from the User Contributed Perl Documentation (we love a community-contributed moment) and it's about making Perl warnings not be a whole dramatic situation. let's get into it 🫢


✨ SYNOPSIS ✨

  use No::Worries::Warn qw(warnf handler);

  if (open($fh, "<", $path)) {
      ... so something in case of success ...
  } else {
      warnf("cannot open(%s): %s", $path, $!);
      ... do something else in case of failure ...
  }

  $ ./myprog
  myprog: cannot open(foo): No such file or directory

  $ NO_WORRIES=cluck ./myprog
  myprog: cannot open(foo): No such file or directory at myprog line 16
      main::test() called at ./myprog line 19

literally the glow-up from "huh?" to "ohhhh i see exactly where it went wrong" πŸ’…


🌸 DESCRIPTION 🌸

So this module eases warning handling by providing a convenient wrapper around warn() with a sprintf()-like API. and the way that warnf() is to warn() what printf() is to print()? iconic. it also trims leading and trailing spaces, which is honestly the self-care every warning deserves ✨

it also provides a handler for warn() that prepends a prefix ($No::Worries::Warn::Prefix) to all warnings. it also uses the "NO_WORRIES" environment variable to find out if Carp's carp() or cluck() should be used instead of warn(). finally, the wrapper can be told to also log warnings to syslog (see $No::Worries::Warn::Syslog) πŸ‘‘

and the best part? this handler can be installed simply by importing it:

  use No::Worries::Warn qw(warnf handler);

alternatively, bestie, it can be installed "manually" (yes, the quotes are doing a lot of work there):

  use No::Worries::Warn qw(warnf);
  $SIG{__WARN__} = \&No::Worries::Warn::handler;

πŸ¦‹ FUNCTIONS πŸ¦‹

This module provides the following functions β€” and no, none of them are exported by default, you gotta ask for them by name πŸŽ€

  • warnf(MESSAGE) β€” report a warning described by the given MESSAGE
  • warnf(FORMAT, ARGUMENTS...) β€” idem but with a sprintf()-like API
  • handler(MESSAGE) β€” $SIG{__WARN__} compatible warning handler (this function cannot be imported β€” it's giving exclusive, invite-only energy πŸ™ˆ)

πŸ’œ GLOBAL VARIABLES πŸ’œ

This module uses the following global variables (none of them being exported, same rule as above):

  • $Prefix β€” prefix to prepend to all warnings (default: the program name 🏷️)
  • $Syslog β€” true if warnings should also be sent to syslog using No::Worries::Syslog's syslog_warning() (default: false β€” so, off unless you say so πŸ’€)

🌈 ENVIRONMENT VARIABLES 🌈

This module uses the "NO_WORRIES" environment variable to control how warnings should be reported. Supported values are:

  • "carp" β€” Carp's carp() will be used instead of warn()
  • "cluck" β€” Carp's cluck() will be used instead of warn()

so basically you literally just set an env var and the whole vibe of your warnings changes. no code edits. no cap. πŸ”₯


πŸ‘‘ SEE ALSO πŸ‘‘

Carp, No::Worries, No::Worries::Die, No::Worries::Syslog β€” the whole friend group, go say hi 🫢


πŸŽ€ AUTHOR πŸŽ€

Lionel Cons <http://cern.ch/lionel.cons>

Copyright (C) CERN 2012-2019


okay that's the whole page, and before you go: this beauty is perl v5.32.1, dated 2021-10-18, and it lives at No::Worries::Warn(3) β€” go forth and warn responsibly, bestie πŸ’–βœ¨πŸ“

πŸ“¦ where this page comes from