package MDK::Common; =head1 NAME MDK::Common - miscellaneous functions =head1 SYNOPSIS use MDK::Common; # exports all functions, equivalent to use MDK::Common::DataStructure qw(:all); use MDK::Common::File qw(:all); use MDK::Common::Func qw(:all); use MDK::Common::Math qw(:all); use MDK::Common::String qw(:all); use MDK::Common::System qw(:all); use MDK::Common::Various qw(:all); =head1 DESCRIPTION C is a collection of packages containing various simple functions: L, L, L, L, L, L, L, L. =head1 EXPORTS from MDK::Common::DataStructure.pm =over =item sort_numbers(LIST) numerical sort (small numbers at beginning) =item ikeys(HASH) aka I, as simple as C=E $b } keys> =item add2hash(HASH REF, HASH REF) adds to the first hash the second hash if the key/value is not already there =item add2hash_ adds to the first hash the second hash if the key is not already there =item put_in_hash adds to the first hash the second hash, crushing existing key/values =item member(SCALAR, LIST) is the value in the list? =item invbool(SCALAR REF) toggles the boolean value =item listlength(LIST) returns the length of the list. Useful in list (opposed to array) context: sub f { "a", "b" } my $l = listlength f(); whereas C would return "b" =item deref(REF) de-reference =item deref_array(REF) de-reference arrays: deref_array [ "a", "b" ] #=> ("a", "b") deref_array "a" #=> "a" =item is_empty_array_ref(SCALAR) is the scalar undefined or is the array empty =item is_empty_hash_ref(SCALAR) is the scalar undefined or is the hash empty =item uniq(LIST) returns the list with no duplicates (keeping the first elements) =item difference2(ARRAY REF, ARRAY REF) returns the first list without the element of the second list =item intersection(ARRAY REF, ARRAY REF, ...) returns the elements which are in all lists =item next_val_in_array(SCALAR, ARRAY REF) finds the value that follow the scalar in the list (circular): C gives C<1> (do not use a list with duplicates) =item group_by2(LIST) interprets the list as an ordered hash, returns a list of [key,value]: C 2, 3 => 4, 5 => 6)> gives C<[1,2], [3,4], [5,6]> =item list2kv(LIST) interprets the list as an ordered hash, returns the keys and the values: C 2, 3 => 4, 5 => 6)> gives C<[1,3,5], [2,4,6]> =back =head1 EXPORTS from MDK::Common::File.pm =over =item dirname(FILENAME) =item basename(FILENAME) returns the dirname/basename of the file name =item cat_(FILENAME) returns the file content: in scalar context it returns a single string, in array context it returns the lines. If the file doesn't exist, it returns undef =item cat__(FILEHANDLE REF) returns the file content: in scalar context it returns a single string, in array context it returns the lines =item output(FILENAME, LIST) creates a file and outputs the list (if the file exists, it is clobbered) =item append_to_file(FILENAME, LIST) add the LIST at the end of the file =item output_p(FILENAME, LIST) just like C but creates directories if needed =item mkdir_p(DIRNAME) creates the directory (make parent directories as needed) =item rm_rf(FILES) remove the files (including sub-directories) =item cp_af(FILES, DEST) just like "cp -af" =item linkf(SOURCE, DESTINATION) =item symlinkf(SOURCE, DESTINATION) =item renamef(SOURCE, DESTINATION) same as link/symlink/rename but removes the destination file first =item touch(FILENAME) ensure the file exists, set the modification time to current time =item all(DIRNAME) returns all the file in directory (except "." and "..") =item glob_(STRING) simple version of C: doesn't handle wildcards in directory (eg: */foo.c), nor special constructs (eg: [0-9] or {a,b}) =item substInFile { CODE } FILENAME executes the code for each line of the file. You can know the end of the file is reached using C =item expand_symlinks(FILENAME) expand the symlinks in the absolute filename: C gives "/usr/X11R6/bin/XFree86" =item openFileMaybeCompressed(FILENAME) opens the file and returns the file handle. If the file is not found, tries to gunzip the file + .gz =item catMaybeCompressed(FILENAME) cat_ alike. If the file is not found, tries to gunzip the file + .gz =back =head1 EXPORTS from MDK::Common::Func.pm =over =item may_apply(CODE REF, SCALAR) C is C<$f ? $f-E($v) : $v> =item may_apply(CODE REF, SCALAR, SCALAR) C is C<$f ? $f-E($v) : $otherwise> =item if_(BOOL, LIST) special constructs to workaround a missing perl feature: C is C<$b ? ("a", "b") : ()> example of use: C which is not the same as C =item if__(SCALAR, LIST) if_ alike. Test if the value is defined =item 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) =item 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 =item 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 =item map_index { CODE } LIST just like C, but set C<$::i> to the current index in the list: map_index { "$::i $_" } "a", "b" gives "0 a", "1 b" =item each_index { CODE } LIST just like C, but doesn't return anything each_index { print "$::i $_\n" } "a", "b" prints "0 a", "1 b" =item grep_index { CODE } LIST just like C, but set C<$::i> to the current index in the list: grep_index { $::i == $_ } 0, 2, 2, 3 gives (0, 2, 3) =item find_index { CODE } LIST returns the index of the first element where CODE returns true find_index { /foo/ } "fo", "fob", "foobar", "foobir" gives 2 =item 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" =item 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 =item partition { CODE } LIST alike C, 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 ] =item 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 =item cdie(SCALAR) aka I. If a C is catched, the execution continues B the cdie, not where it was catched (as happens with die & eval) If a C is not catched, it mutates in real exception that can be catched with C 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. =item catch_cdie { CODE1 } sub { CODE2 } If a C occurs while executing CODE1, CODE2 is executed. If CODE2 returns true, the C is catched. =back =head1 EXPORTS from MDK::Common::Math.pm =over =item $PI the well-known constant =item even(INT) =item odd(INT) is the number even or odd? =item sqr(FLOAT) C gives C<9> =item sign(FLOAT) returns a value in { -1, 0, 1 } =item round(FLOAT) C gives C<1>, C gives C<2> =item round_up(FLOAT, INT) returns the number rounded up to the modulo: C gives C<20> =item round_down(FLOAT, INT) returns the number rounded down to the modulo: C gives C<10> =item divide(INT, INT) integer division (which is lacking in perl). In array context, also returns the remainder: C<($a, $b) = divide(10,3)> gives C<$a is 3> and C<$b is 1> =item min(LIST) =item max(LIST) returns the minimum/maximum number in the list =item or_(LIST) is there a true value in the list? =item and_(LIST) are all values true in the list? =item sum(LIST) =item product(LIST) returns the sum/product of all the element in the list =item factorial(INT) C gives C<24> (4*3*2) =back =head1 OTHER in MDK::Common::Math.pm the following functions are provided, but not exported: =over =item factorize(INT) C gives C<([2,3], [5,1])> as S<40 = 2^3 + 5^1> =item decimal2fraction(FLOAT) C gives C<(4, 3)> ($PRECISION is used to decide which precision to use) =item poly2(a,b,c) Solves the a*x2+b*x+c=0 polynomial: C gives C<(1, -1)> =item permutations(n,p) A(n,p) =item combinaisons(n,p) C(n,p) =back =head1 EXPORTS from MDK::Common::String.pm =over =item bestMatchSentence(STRING, LIST) finds in the list the best corresponding string =item formatList(INT, LIST) if the list size is bigger than INT, replace the remaining elements with "...". formatList(3, qw(a b c d e)) # => "a, b, c, ..." =item formatError(STRING) the string is something like "error at foo.pl line 2" that you get when catching an exception. formatError will remove the "at ..." so that you can nicely display the returned string to the user =item formatTimeRaw(TIME) the TIME is an epoch as returned by C