Thierry Vignaud
and 1 contributors

# NAME

MDK::Common::Func - miscellaneous functions

# SYNOPSIS

use MDK::Common::Func qw(:all);

# EXPORTS

may_apply(CODE REF, SCALAR)

may_apply(\$f, \$v) is \$f ? \$f->(\$v) : \$v

may_apply(CODE REF, SCALAR, SCALAR)

may_apply(\$f, \$v, \$otherwise) is \$f ? \$f->(\$v) : \$otherwise

if_(BOOL, LIST)

special constructs to workaround a missing perl feature: if_(\$b, "a", "b") is \$b ? ("a", "b") : ()

example of use: f("a", if_(arch() =~ /i.86/, "b"), "c") which is not the same as f("a", arch()=~ /i.86/ && "b", "c")

if__(SCALAR, LIST)

if_ alike. Test if the value is defined

fold_left { CODE } LIST

if you don't know fold_left (aka foldl), don't use it ;p

fold_left { \$::a + \$::b } 1, 3, 6

gives 10 (aka 1+3+6)

mapn { CODE } ARRAY REF, ARRAY REF, ...

map lists in parallel:

mapn { \$_[0] + \$_[1] } [1, 2], [2, 4] # gives 3, 6
mapn { \$_[0] + \$_[1] + \$_[2] } [1, 2], [2, 4], [3, 6] gives 6, 12
mapn_ { CODE } ARRAY REF, ARRAY REF, ...

mapn alike. The difference is what to do when the lists have not the same length: mapn takes the minimum common elements, mapn_ takes the maximum list length and extend the lists with undef values

find { CODE } LIST

returns the first element where CODE returns true (or returns undef)

find { /foo/ } "fo", "fob", "foobar", "foobir"

gives "foobar"

any { CODE } LIST

returns 1 if CODE returns true for an element in LIST (otherwise returns 0)

any { /foo/ } "fo", "fob", "foobar", "foobir"

gives 1

every { CODE } LIST

returns 1 if CODE returns true for every element in LIST (otherwise returns 0)

every { /foo/ } "fo", "fob", "foobar", "foobir"

gives 0

map_index { CODE } LIST

just like map, but set \$::i to the current index in the list:

map_index { "\$::i \$_" } "a", "b"

gives "0 a", "1 b"

each_index { CODE } LIST

just like map_index, but doesn't return anything

each_index { print "\$::i \$_\n" } "a", "b"

prints "0 a", "1 b"

grep_index { CODE } LIST

just like grep, but set \$::i to the current index in the list:

grep_index { \$::i == \$_ } 0, 2, 2, 3

gives (0, 2, 3)

find_index { CODE } LIST

returns the index of the first element where CODE returns true (or throws an exception)

find_index { /foo/ } "fo", "fob", "foobar", "foobir"

gives 2

map_each { CODE } HASH

returns the list of results of CODE applied with \$::a (key) and \$::b (value)

map_each { "\$::a is \$::b" } 1=>2, 3=>4

gives "1 is 2", "3 is 4"

grep_each { CODE } HASH

returns the hash key/value for which CODE applied with \$::a (key) and \$::b (value) is true:

grep_each { \$::b == 2 } 1=>2, 3=>4, 4=>2

gives 1=>2, 4=>2

partition { CODE } LIST

alike grep, but returns both the list of matching elements and non matching elements

my (\$greater, \$lower) = partition { \$_ > 3 } 4, 2, 8, 0, 1

gives \$greater = [ 4, 8 ] and \$lower = [ 2, 0, 1 ]

before_leaving { CODE }

the code will be executed when the current block is finished

# create \$tmp_file
my \$b = before_leaving { unlink \$tmp_file };
# some code that may throw an exception, the "before_leaving" ensures the
# \$tmp_file will be removed
cdie(SCALAR)

aka conditional die. If a cdie is catched, the execution continues after the cdie, not where it was catched (as happens with die & eval)

If a cdie is not catched, it mutates in real exception that can be catched with eval

cdie is useful when you want to warn about something weird, but when you can go on. In that case, you cdie "something weird happened", and the caller decide wether to go on or not. Especially nice for libraries.

catch_cdie { CODE1 } sub { CODE2 }

If a cdie occurs while executing CODE1, CODE2 is executed. If CODE2 returns true, the cdie is catched.

MDK::Common