Catmandu::Util - A collection of utility functions
use Catmandu::Util qw(:string); $str = trim($str);
use Catmandu::Util qw(:io);
Takes a file path, glob, glob reference, scalar reference or IO::Handle object and returns an opened IO::Handle object.
my $fh = io '/path/to/file'; my $fh = io *STDIN; my $fh = io \*STDOUT, mode => 'w', binmode => ':crlf'; my $scalar = ""; my $fh = io \$scalar, mode => 'w'; $fh->print("some text");
Options are:
Reads the file at $path
into a string.
my $str = read_file('/path/to/file.txt');
Writes the string $str
to a file at $path
.
write_file('/path/to/file.txt', "contents");
my $cfg = read_yaml('config.yaml');
my $cfg = read_json('config.json');
join_path('/path/..', './to', 'file.txt'); # => "/to/file.txt"
normalize_path('/path/../to/./file.txt'); # => "/to/file.txt"
my $id = "FB41144C-F0ED-11E1-A9DE-61C894A0A6B4"; segmented_path($id, segment_size => 4); # => "FB41/144C/F0ED/11E1/A9DE/61C8/94A0/A6B4" segmented_path($id, segment_size => 2, base_path => "/files"); # => "/files/FB/41/14/4C/F0/ED/11/E1/A9/DE/61/C8/94/A0/A6/B4"
use Catmandu::Util qw(:array);
A collection of functions that operate on array references.
Returns 1
if $index
is in the bounds of $array
array_exists(["a", "b"], 2); # => 0 array_exists(["a", "b"], 1); # => 1
my $list = [{color => 'black', id => 1}, {color => 'white', id => 2}, {id => 3}, {color => 'black', id => 4}]; array_group_by($list, 'color'); # => {black => [{color => 'black', id => 1}, {color => 'black', id => 4}], # white => [{color => 'white', id => 2}]}
my $list = [{id => 1}, {}, {id => 3}]; array_pluck($list, 'id'); # => [1, undef, 3]
array_to_sentence([1,2,3]); # => "1, 2 and 3" array_to_sentence([1,2,3], ","); # => "1,2 and 3" array_to_sentence([1,2,3], ",", " & "); # => "1,2 & 3"
array_sum([1,2,3]); # => 6
Returns 1 if $array
includes a value that is deeply equal to $val
, 0 otherwise. Comparison is done with is_same()
.
array_includes([{color => 'black'}], {color => 'white'}); # => 0 array_includes([{color => 'black'}], {color => 'black'}); # => 1
array_any(["green", "blue"], sub { my $color = $_[0]; $color eq "blue" }); # => 1
Returns a copy of $array
without the head.
array_rest([1,2,3,4]); # => [2,3,4] array_rest([1]); # => []
Returns a copy of $array
with all duplicates removed. Comparison is done with is_same()
.
use Catmandu::Util qw(:string);
Returns a copy of $str
flagged as UTF-8.
Returns a copy of $str
with leading and trailing whitespace removed.
Equivalent to ucfirst lc as_utf8 $str
.
use Catmandu::Util qw(:is); is_number(42) ? "it's numeric" : "it's not numeric"; is_maybe_hash_ref({}); # => 1 is_maybe_hash_ref(undef); # => 1 is_maybe_hash_ref([]); # => 0
A collection of predicate functions that test the type or value of argument $val
. Each function (except is_same()
and is_different
) also has a maybe variant that also tests true if $val
is undefined. Returns 1
or 0
.
Tests if $val
is callable (is an existing package or blessed object).
Tests if $val
is callable and has all methods in @method_names
.
Tests if $val
is a reference. Equivalent to ref $val ? 1 : 0
.
Tests if $val
is a scalar reference.
Tests if $val
is an array reference.
Tests if $val
is a hash reference.
Tests if $val
is a subroutine reference.
Tests if $val
is a regular expression reference generated by the qr//
operator.
Tests if $val
is a glob reference.
Tests if $val
is a real value (defined, not a reference and not a glob.
Tests if $val
is a non-empty string. Equivalent to is_value($val) && length($val) > 0
.
Tests if $val
is a number.
Tests if $val
is an integer.
Tests if $val
is a non-negative integer. Equivalent to is_integer($val) && $val >= 0
.
Tests if $val
is a positive integer. Equivalent to is_integer($val) && $val >= 1
.
Tests if $val
is deeply equal to $other_val
.
The opposite of is_same()
.
use Catmandu::Util qw(:check); check_hash_ref({color => 'red'}); # => {color => 'red'} check_hash_ref([]); # dies
A group of assert functions similar to the :is
group, but instead of returning true or false they return their argument or die.
use Catmandu::Util qw(:human);
Insert a comma a 3-digit intervals to make $num
more readable. Only works with integers for now.
human_number(64354); # => "64,354"
human_byte_size(64); # => "64 bytes" human_byte_size(10005000); # => "10.01 MB"
human_content_type('application/x-dos_ms_excel'); # => "Excel" human_content_type('application/zip'); # => "ZIP archive" human_content_type('foo/x-unknown'); # => "foo/x-unknown" human_content_type('foo/x-unknown', 'Unknown'); # => "Unknown"
use Catmandu::Util qw(:xml);
Returns qq(<?xml version="1.0" encoding="UTF-8"?>\n)
.
Returns an XML escaped copy of $str
.
Load package $pkg
at runtime with require
and return it's full name.
my $pkg = require_package('File::Spec'); my $dir = $pkg->tmpdir(); require_package('Util', 'Catmandu'); # => "Catmandu::Util" require_package('Catmandu::Util', 'Catmandu'); # => "Catmandu::Util"
Add directories to @INC
at runtime.