Badger
view release on metacpan or search on metacpan
lib/Badger/Utils.pm view on Meta::CPAN
#========================================================================
#
# Badger::Utils
#
# DESCRIPTION
# Module implementing various useful utility functions.
#
# AUTHOR
# Andy Wardley <abw@wardley.org>
#
#========================================================================
package Badger::Utils;
use strict;
use warnings;
use base 'Badger::Exporter';
use File::Path;
use Scalar::Util qw( blessed );
use Badger::Constants 'HASH ARRAY PKG DELIMITER BLANK TRUE FALSE';
use Badger::Debug
import => ':dump',
default => 0;
use overload;
use constant {
UTILS => 'Badger::Utils',
CLASS => 0,
FILE => 1,
LOADED => 2,
};
our $VERSION = 0.02;
our $ERROR = '';
our $WARN = sub { warn @_ }; # for testing - see t/core/utils.t
our $MESSAGES = { };
our $HELPERS = { # keep this compact in case we don't need to use it
'Digest::MD5' => 'md5 md5_hex md5_base64',
'Scalar::Util' => 'blessed dualvar isweak readonly refaddr reftype
tainted weaken isvstring looks_like_number
set_prototype',
'List::Util' => 'first max maxstr min minstr reduce shuffle sum',
'List::MoreUtils' => 'any all none notall true false firstidx
first_index lastidx last_index insert_after
insert_after_string apply after after_incl before
before_incl indexes firstval first_value lastval
last_value each_array each_arrayref pairwise
natatime mesh zip uniq minmax',
'Hash::Util' => 'lock_keys unlock_keys lock_value unlock_value
lock_hash unlock_hash hash_seed',
'Badger::Date' => 'DATE Date Today',
'Badger::Timestamp' => 'TIMESTAMP TS Timestamp Now',
'Badger::Logic' => 'LOGIC Logic',
'Badger::Duration' => 'DURATION Duration',
'Badger::URL' => 'URL',
'Badger::Filter' => 'FILTER Filter',
'Badger::Filesystem' => 'FS File Dir Bin Cwd',
'Badger::Filesystem::Virtual'
=> 'VFS',
};
our $DELEGATES; # fill this from $HELPERS on demand
our $RANDOM_NAME_LENGTH = 32;
our $TEXT_WRAP_WIDTH = 78;
__PACKAGE__->export_any(qw(
UTILS blessed is_object numlike textlike truelike falselike
params self_params plural
odd_params xprintf dotid random_name camel_case CamelCase wrap
permute_fragments plurality inflect split_to_list extend merge merge_hash
list_each hash_each join_uri resolve_uri
));
__PACKAGE__->export_fail(\&_export_fail);
# looks_like_number() is such a mouthful. I prefer numlike() to go with textlike()
*numlike = \&Scalar::Util::looks_like_number;
# it would be too confusing not to have this alias
*CamelCase = \&camel_case;
sub _export_fail {
my ($class, $target, $symbol, $more_symbols) = @_;
$DELEGATES ||= _expand_helpers($HELPERS);
my $helper = $DELEGATES->{ $symbol } || return 0;
require $helper->[FILE] unless $helper->[LOADED];
$class->export_symbol($target, $symbol, \&{ $helper->[CLASS].PKG.$symbol });
return 1;
}
sub _expand_helpers {
# invert { x => 'a b c' } into { a => 'x', b => 'x', c => 'x' }
my $helpers = shift;
return {
map {
my $name = $_; # e.g. Scalar::Util
my $file = module_file($name); # e.g. Scalar/Util.pm
map { $_ => [$name, $file, 0] } # third item is loaded flag
split(DELIMITER, $helpers->{ $name })
( run in 2.185 seconds using v1.01-cache-2.11-cpan-0d23b851a93 )