SHARYANTO::Array::Util3pm
Array-related utilities
Usage
use SHARYANTO::Array::Util qw(match_array_or_regex split_array);match_array_or_regex('bar', ['foo', 'bar', qr/[xyz]/]); # true, matches stringmatch_array_or_regex('baz', ['foo', 'bar', qr/[xyz]/]); # true, matches regexmatch_array_or_regex('oops', ['foo', 'bar', qr/[xyz]/]); # falsemy @res = split_array('--', [qw/--opt1 --opt2 -- foo bar -- --val/]);# -> ([qw/--opt1 --opt2/], [qw/foo bar/], [qw/--val/])my @res = split_array(qr/--/, [qw/--opt1 --opt2 -- foo bar -- --val/], 2);# -> ([qw/--opt1 --opt2/], [qw/foo bar -- --val/])my @res = split_array(qr/(--)/, [qw/--opt1 --opt2 -- foo bar -- --val/], 2);# -> ([qw/--opt1 --opt2/], [qw/--/], [qw/foo bar -- --val/])my @res = split_array(qr/(-)(-)/, [qw/--opt1 --opt2 -- foo bar -- --val/], 2);# -> ([qw/--opt1 --opt2/], [qw/- -/], [qw/foo bar -- --val/])
Version
This document describes version 0.77 of SHARYANTO::Array::Util (from Perl distribution SHARYANTO-Utils), released on 2015-09-04.
Overview
Functions
match_array_or_regex($needle, $haystack) -> any
Check whether an item matches (list of) values/regexes.
Examples:
match_array_or_regex("abc", ["abc", "abd"]); # -> 1match_array_or_regex("abc", qr/ab./); # -> 1match_array_or_regex("abc", [qr/ab./, "abd"]); # -> 1This routine can be used to match an item against a regex or a list ofstrings/regexes, e.g. when matching against an ACL.
Since the smartmatch ("~~") operator can already match against a list of strings or regexes, this function is currently basically equivalent to:
if (ref($haystack) eq 'ARRAY') {return $needle ~~ @$haystack;} else {return $needle =~ /$haystack/;}
Arguments ('*' denotes required arguments):
•
|
|
|---|---|
•
|
|
Return value: (any)
match_regex_or_array($needle, $haystack) -> any
Alias for match_array_or_regex.
Examples:
match_regex_or_array("abc", ["abc", "abd"]); # -> 1match_regex_or_array("abc", qr/ab./); # -> 1match_regex_or_array("abc", [qr/ab./, "abd"]); # -> 1This routine can be used to match an item against a regex or a list ofstrings/regexes, e.g. when matching against an ACL.
Since the smartmatch ("~~") operator can already match against a list of strings or regexes, this function is currently basically equivalent to:
if (ref($haystack) eq 'ARRAY') {return $needle ~~ @$haystack;} else {return $needle =~ /$haystack/;}
Arguments ('*' denotes required arguments):
•
|
|
|---|---|
•
|
|
Return value: (any)
split_array($str_or_re, \@array[, $limit]) => LIST
Like the "split()" builtin Perl function, but applies on an array instead of a scalar. It loosely follows the "split()" semantic, with some exceptions.
replace_array_content($aryref, @elems) => $aryref
Replace elements in <$aryref> with @elems. Return $aryref. Do not create a new arrayref object (i.e. it is different from: "$aryref = ["new", "content"]").
Do not use this function. In Perl you can just use: "splice(@$aryref, 0, length(@$aryref), @elems)" or even easier: "@$aryref = @elems". I put the function here for reminder.
Homepage
Please visit the project's homepage at <https://metacpan.org/release/SHARYANTO-Utils>.
Source
Source repository is at <https://github.com/perlancar/perl-SHARYANTO-Utils>.
Copyright and license
This software is copyright (c) 2015 by [email protected].
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Releases
6 releases since Debian 8 (Jessie); the text changed 2 times.