| Catmandu::Util(3pm) | User Contributed Perl Documentation | Catmandu::Util(3pm) |
Catmandu::Util - A collection of utility functions
use Catmandu::Util qw(:string);
$str = trim($str);
use Catmandu::Util qw(:io);
my $fh = io '/path/to/file';
my $fh = io *STDIN;
my $fh = io \*STDOUT, mode => 'w', binmode => ':crlf';
my $write_cb = sub { my $str = $_[0]; ... };
my $fh = io $write_cb, mode => 'w';
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');
Throws a Catmandu::Error on failure.
my $str = read_file($fh);
Writes the string $str to a file at $path.
write_file('/path/to/file.txt', "contents");
Throws a Catmandu::Error on failure.
my $cfg = read_yaml($path);
Dies on failure reading the file or parsing the YAML.
my $cfg = read_json($path);
Dies on failure reading the file or parsing the 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"
content_type("book.pdf");
# => "application/pdf"
use Catmandu::Util qw(:hash);
A collection of functions that operate on hash references.
hash_merge({a => 1}, {b => 2}, {a => 3});
# => { a => 3 , b => 2}
use Catmandu::Util qw(:array);
A collection of functions that operate on array references.
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
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
array_rest([1,2,3,4]);
# => [2,3,4]
array_rest([1]);
# => []
use Catmandu::Util qw(:string);
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.
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);
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);
my $pkg = require_package('File::Spec');
my $dir = $pkg->tmpdir();
require_package('Util', 'Catmandu');
# => "Catmandu::Util"
require_package('Catmandu::Util', 'Catmandu');
# => "Catmandu::Util"
Throws a Catmandu::Error on failure.
Throws a Catmandu::Error on failure.
now('%Y/%m/%d');
now('iso_date_time_millis');
The default format is "iso_date_time";
| 2023-03-03 | perl v5.36.0 |