{"id":6857,"date":"2022-12-20T19:33:44","date_gmt":"2022-12-20T22:33:44","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/mdkcommonfunc-man3\/"},"modified":"2022-12-20T19:33:44","modified_gmt":"2022-12-20T22:33:44","slug":"mdkcommonfunc-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/mdkcommonfunc-man3\/","title":{"rendered":"MDK::Common::Func (man3)"},"content":{"rendered":"<h1 align=\"center\">MDK::Common::Func<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#EXPORTS\">EXPORTS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">MDK::Common::Func \u2212 miscellaneous functions<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">use MDK::Common::Func qw(:all);<\/p>\n<h2>EXPORTS <a name=\"EXPORTS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">may_apply( <small>CODE REF, SCALAR<\/small> )<\/p>\n<p style=\"margin-left:17%;\">&#8220;may_apply($f, $v)&#8221; is &#8220;$f ? $f\u2212>($v) : $v&#8221;<\/p>\n<p style=\"margin-left:11%;\">may_apply( <small>CODE REF, SCALAR, SCALAR<\/small> )<\/p>\n<p style=\"margin-left:17%;\">&#8220;may_apply($f, $v, $otherwise)&#8221; is &#8220;$f ? $f\u2212>($v) : $otherwise&#8221;<\/p>\n<p style=\"margin-left:11%;\">if_( <small>BOOL, LIST<\/small> )<\/p>\n<p style=\"margin-left:17%;\">special constructs to workaround a missing perl feature: &#8220;if_($b, &#8220;a&#8221;, &#8220;b&#8221;)&#8221; is &#8220;$b ? (&#8220;a&#8221;, &#8220;b&#8221;) : ()&#8221;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">example of use: &#8220;f(&#8220;a&#8221;, if_(arch() =~ \/i.86\/, &#8220;b&#8221;), &#8220;c&#8221;)&#8221; which is not the same as &#8220;f(&#8220;a&#8221;, arch()=~ \/i.86\/ &#038;&#038; &#8220;b&#8221;, &#8220;c&#8221;)&#8221;<\/p>\n<p style=\"margin-left:11%;\">if__( <small>SCALAR, LIST<\/small> )<\/p>\n<p style=\"margin-left:17%;\">if_ alike. Test if the value is defined<\/p>\n<p style=\"margin-left:11%;\">fold_left { <small>CODE<\/small> } <small>LIST<\/small><\/p>\n<p style=\"margin-left:17%;\">if you don\u2019t know fold_left (aka foldl), don\u2019t use it ;p<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">fold_left { $::a + $::b } 1, 3, 6<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">gives 10 (aka 1+3+6)<\/p>\n<p style=\"margin-left:11%;\">mapn { <small>CODE<\/small> } <small>ARRAY REF, ARRAY REF, &#8230;<\/small><\/p>\n<p style=\"margin-left:17%;\">map lists in parallel:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">mapn { $_[0] + $_[1] } [1, 2], [2, 4] # gives 3, 6 <br \/> mapn { $_[0] + $_[1] + $_[2] } [1, 2], [2, 4], [3, 6] gives 6, 12<\/p>\n<p style=\"margin-left:11%;\">mapn_ { <small>CODE<\/small> } <small>ARRAY REF, ARRAY REF, &#8230;<\/small><\/p>\n<p style=\"margin-left:17%;\">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<\/p>\n<p style=\"margin-left:11%;\">find { <small>CODE<\/small> } <small>LIST<\/small><\/p>\n<p style=\"margin-left:17%;\">returns the first element where <small>CODE<\/small> returns true (or returns undef)<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">find { \/foo\/ } &#8220;fo&#8221;, &#8220;fob&#8221;, &#8220;foobar&#8221;, &#8220;foobir&#8221;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">gives &#8220;foobar&#8221;<\/p>\n<p style=\"margin-left:11%;\">any { <small>CODE<\/small> } <small>LIST<\/small><\/p>\n<p style=\"margin-left:17%;\">returns 1 if <small>CODE<\/small> returns true for an element in <small>LIST<\/small> (otherwise returns 0)<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">any { \/foo\/ } &#8220;fo&#8221;, &#8220;fob&#8221;, &#8220;foobar&#8221;, &#8220;foobir&#8221;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">gives 1<\/p>\n<p style=\"margin-left:11%;\">every { <small>CODE<\/small> } <small>LIST<\/small><\/p>\n<p style=\"margin-left:17%;\">returns 1 if <small>CODE<\/small> returns true for <b>every<\/b> element in <small>LIST<\/small> (otherwise returns 0)<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">every { \/foo\/ } &#8220;fo&#8221;, &#8220;fob&#8221;, &#8220;foobar&#8221;, &#8220;foobir&#8221;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">gives 0<\/p>\n<p style=\"margin-left:11%;\">map_index { <small>CODE<\/small> } <small>LIST<\/small><\/p>\n<p style=\"margin-left:17%;\">just like &#8220;map&#8221;, but set $::i to the current index in the list:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">map_index { &#8220;$::i $_&#8221; } &#8220;a&#8221;, &#8220;b&#8221;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">gives &#8220;0 a&#8221;, &#8220;1 b&#8221;<\/p>\n<p style=\"margin-left:11%;\">each_index { <small>CODE<\/small> } <small>LIST<\/small><\/p>\n<p style=\"margin-left:17%;\">just like &#8220;map_index&#8221;, but doesn\u2019t return anything<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">each_index { print &#8220;$::i $_n&#8221; } &#8220;a&#8221;, &#8220;b&#8221;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">prints &#8220;0 a&#8221;, &#8220;1 b&#8221;<\/p>\n<p style=\"margin-left:11%;\">grep_index { <small>CODE<\/small> } <small>LIST<\/small><\/p>\n<p style=\"margin-left:17%;\">just like &#8220;grep&#8221;, but set $::i to the current index in the list:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">grep_index { $::i == $_ } 0, 2, 2, 3<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">gives (0, 2, 3)<\/p>\n<p style=\"margin-left:11%;\">find_index { <small>CODE<\/small> } <small>LIST<\/small><\/p>\n<p style=\"margin-left:17%;\">returns the index of the first element where <small>CODE<\/small> returns true (or throws an exception)<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">find_index { \/foo\/ } &#8220;fo&#8221;, &#8220;fob&#8221;, &#8220;foobar&#8221;, &#8220;foobir&#8221;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">gives 2<\/p>\n<p style=\"margin-left:11%;\">map_each { <small>CODE<\/small> } <small>HASH<\/small><\/p>\n<p style=\"margin-left:17%;\">returns the list of results of <small>CODE<\/small> applied with $::a (key) and $::b (value)<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">map_each { &#8220;$::a is $::b&#8221; } 1=>2, 3=>4<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">gives &#8220;1 is 2&#8221;, &#8220;3 is 4&#8221;<\/p>\n<p style=\"margin-left:11%;\">grep_each { <small>CODE<\/small> } <small>HASH<\/small><\/p>\n<p style=\"margin-left:17%;\">returns the hash key\/value for which <small>CODE<\/small> applied with $::a (key) and $::b (value) is true:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">grep_each { $::b == 2 } 1=>2, 3=>4, 4=>2<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">gives 1=>2, 4=>2<\/p>\n<p style=\"margin-left:11%;\">partition { <small>CODE<\/small> } <small>LIST<\/small><\/p>\n<p style=\"margin-left:17%;\">alike &#8220;grep&#8221;, but returns both the list of matching elements and non matching elements<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">my ($greater, $lower) = partition { $_ > 3 } 4, 2, 8, 0, 1<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">gives $greater = [ 4, 8 ] and $lower = [ 2, 0, 1 ]<\/p>\n<p style=\"margin-left:11%;\">before_leaving { <small>CODE<\/small> }<\/p>\n<p style=\"margin-left:17%;\">the code will be executed when the current block is finished<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"># create $tmp_file <br \/> my $b = before_leaving { unlink $tmp_file }; <br \/> # some code that may throw an exception, the &#8220;before_leaving&#8221; ensures the <br \/> # $tmp_file will be removed<\/p>\n<p style=\"margin-left:11%;\">cdie( <small>SCALAR<\/small> )<\/p>\n<p style=\"margin-left:17%;\">aka <i>conditional die<\/i>. If a &#8220;cdie&#8221; is catched, the execution continues <b>after<\/b> the cdie, not where it was catched (as happens with die &#038; eval)<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">If a &#8220;cdie&#8221; is not catched, it mutates in real exception that can be catched with &#8220;eval&#8221;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">cdie is useful when you want to warn about something weird, but when you can go on. In that case, you cdie &#8220;something weird happened&#8221;, and the caller decide wether to go on or not. Especially nice for libraries.<\/p>\n<p style=\"margin-left:11%;\">catch_cdie { <small>CODE1<\/small> } sub { <small>CODE2<\/small> }<\/p>\n<p style=\"margin-left:17%;\">If a &#8220;cdie&#8221; occurs while executing <small>CODE1, CODE2<\/small> is executed. If <small>CODE2<\/small> returns true, the &#8220;cdie&#8221; is catched.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">MDK::Common<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  MDK::Common::Func \u2212 miscellaneous functions <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3007,3222],"class_list":["post-6857","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-man3","tag-mdkcommonfunc"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6857","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=6857"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6857\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=6857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=6857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=6857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}