Ancient
view release on metacpan or search on metacpan
lib/util.pm view on Meta::CPAN
my ($evens, $odds) = partition(sub { $_ % 2 == 0 }, \@numbers);
Splits an array into two arrayrefs based on a predicate. The first
contains elements for which the predicate returns true, the second
contains elements for which it returns false.
=head2 pick
my $subset = pick(\%hash, @keys);
Returns a new hashref containing only the specified keys from the
source hash. Missing keys are silently ignored.
my $user_info = pick(\%user, 'name', 'email');
=head2 omit
my $filtered = omit(\%hash, @keys);
Returns a new hashref with the specified keys removed.
Opposite of C<pick>.
my $safe = omit(\%user, 'password', 'secret_token');
=head2 pluck
my @ids = pluck(\@users, 'id');
Extracts a single field from an array of hashes. Returns a list
of values for that field from each hash.
my @names = pluck(\@employees, 'name');
=head2 defaults
my $merged = defaults(\%hash, \%defaults);
Returns a new hashref with values from C<%defaults> filled in for
any missing keys in C<%hash>. Does not modify the original hashes.
my $config = defaults(\%user_config, { timeout => 30, retries => 3 });
=head2 count
my $n = count(sub { $_ > 10 }, \@numbers);
Counts how many elements in the list satisfy the predicate.
More efficient than C<scalar grep { ... } @list> because it
doesn't build an intermediate list.
=head2 replace_all
my $result = replace_all($string, $search, $replace);
Replaces all occurrences of C<$search> in C<$string> with C<$replace>.
Faster than C<< $str =~ s/\Q$search\E/$replace/g >> for literal strings
because it avoids regex compilation.
=head2 negate
my $not_even = negate(sub { $_ % 2 == 0 });
Returns a new function that negates the result of the given predicate.
Useful for inverting filters.
my @odds = grep { negate(\&is_even)->($_) } @numbers;
=head2 once
my $init_once = once(\&initialize);
$init_once->(); # Runs initialize()
$init_once->(); # Returns cached result, doesn't run again
Wraps a function to ensure it only executes once. Subsequent calls
return the cached result of the first call.
=head1 TYPE PREDICATES
These functions use custom ops and are replaced at compile time with
direct SV flag checks. They have zero function call overhead.
=head2 is_ref
my $bool = is_ref($value);
Returns true if C<$value> is a reference (any type).
=head2 is_array
my $bool = is_array($value);
Returns true if C<$value> is an array reference.
=head2 is_hash
my $bool = is_hash($value);
Returns true if C<$value> is a hash reference.
=head2 is_code
my $bool = is_code($value);
Returns true if C<$value> is a code reference.
=head2 is_defined
my $bool = is_defined($value);
Returns true if C<$value> is defined (not C<undef>).
=head2 is_string
my $bool = is_string($value);
Returns true if C<$value> is a plain scalar (defined and not a reference).
This is useful when you want to check if a value is a simple string or number,
not undef and not a reference to something else.
is_string("hello"); # true
is_string(42); # true
( run in 0.611 second using v1.01-cache-2.11-cpan-df04353d9ac )