Sign in to PrePAN

Util::Underscore Common helper functions without having to import them



use Util::Underscore;

_::croak "$foo must do Some::Role" if not _::does($foo, 'Some::Role');


This module contains various utility functions, and makes them accessible through the "_" package. This allows the use of these utilities (a) without much per-usage overhead and (b) without namespace pollution.

It contains functions from the following modules:

  • Scalar::Util
  • List::Util
  • List::MoreUtils
  • Carp
  • Safe::Isa, which contains convenience functions for UNIVERSAL
  • Try::Tiny

Not all functions from those are available, and some have been renamed.

The module has shipped, and can be installed like cpanm Util::Underscore. Feedback is of course still welcome.


There is potential confusion with both $_ and use of underscore for private functions, and the line "Not all functions from those are available, and some have been renamed" suggests further complications in use.

Not sure if possible, but something that could take exports from named modules (rather than an author defined selection) and add to a universal namespace would potentially be useful.

use PerlX::Underscore 'List::Util' => 'first';
use PerlX::Underscore 'List::MoreUtils';



There is zero possible confusion with `$_`, but I do feel uncomfortable grabbing the `_` namespace. Functions have been renamed in order to make the naming more homogeneous, e.g. `maxstr` → `max_str`, `weaken` → `ref_weaken`.

The problem with user-defined imports is that `_::` is *globally* visible – what gets imported there is visible everywhere. Curating the available functions therefore avoids conflicts and ensures back-compat. Lexical packages would be awesome, but Perl does not have them :-(

There is another way: one could have the `_` package dispatch calls via `AUTOLOAD` and implement importing/exporting as a pragma. This means that a caller appears to be lexically importing or exporting modules into the `_` namespace without affecting other modules. There are two problems with this: Firstly, `AUTOLOAD` is inefficient and happens at run-time (whereas subroutines can often be resolved during compilation). Secondly, this would mean we cannot use signatures – something especially the `List::Util` functions rely on.
Util::_ would have been an appropriate package name.

Please sign up to post a review.