The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

SHARYANTO::Array::Util - Array-related utilities

VERSION

version 0.70

SYNOPSIS

 use SHARYANTO::Array::Util qw(match_array_or_regex split_array);

 match_array_or_regex('bar',  ['foo', 'bar', qr/[xyz]/]); # true, matches string
 match_array_or_regex('baz',  ['foo', 'bar', qr/[xyz]/]); # true, matches regex
 match_array_or_regex('oops', ['foo', 'bar', qr/[xyz]/]); # false

 my @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/])

DESCRIPTION

FUNCTIONS

split_array($str_or_re, \@array[, $num]) => 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.

TODO

  • [REJECT] split_array: By default operate on @_?

    Like split's behavior of by default operating on $_.

match_array_or_regex(@args) -> any

Check whether an item matches (list of) values/regexes.

Examples:

 match_array_or_regex(haystack => ["abc", "abd"], needle => "abc"); # -> 1
 match_array_or_regex(haystack => qr/ab./, needle => "abc"); # -> 1
 match_array_or_regex(haystack => [qr/ab./, "abd"], needle => "abc"); # -> 1
This routine can be used to match an item against a regex or a list of
strings/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 (reg($haystack) eq 'ARRAY') {
        return $needle ~~ @$haystack;
    } else {
        return $needle =~ /$haystack/;
    }

Arguments ('*' denotes required arguments):

  • haystack* => any|array

  • needle* => str

Return value:

match_regex_or_array(@args) -> any

Alias for match_array_or_regex.

Examples:

 match_regex_or_array(haystack => ["abc", "abd"], needle => "abc"); # -> 1
 match_regex_or_array(haystack => qr/ab./, needle => "abc"); # -> 1
 match_regex_or_array(haystack => [qr/ab./, "abd"], needle => "abc"); # -> 1
This routine can be used to match an item against a regex or a list of
strings/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 (reg($haystack) eq 'ARRAY') {
        return $needle ~~ @$haystack;
    } else {
        return $needle =~ /$haystack/;
    }

Arguments ('*' denotes required arguments):

  • haystack* => any|array

  • needle* => str

Return value:

SEE ALSO

SHARYANTO

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/SHARYANTO-Utils.

SOURCE

Source repository is at https://github.com/sharyanto/perl-SHARYANTO-Utils.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=SHARYANTO-Utils

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

AUTHOR

Steven Haryanto <stevenharyanto@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Steven Haryanto.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.