<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>MDK::Common - miscellaneous functions</title> <link rev="made" href="mailto:root@localhost" /> </head> <body style="background-color: white"> <p><a name="__index__"></a></p> <!-- INDEX BEGIN --> <ul> <li><a href="#name">NAME</a></li> <li><a href="#synopsis">SYNOPSIS</a></li> <li><a href="#description">DESCRIPTION</a></li> <li><a href="#exports_from_mdk::common::datastructure.pm">EXPORTS from MDK::Common::DataStructure.pm</a></li> <li><a href="#exports_from_mdk::common::file.pm">EXPORTS from MDK::Common::File.pm</a></li> <li><a href="#exports_from_mdk::common::func.pm">EXPORTS from MDK::Common::Func.pm</a></li> <li><a href="#exports_from_mdk::common::math.pm">EXPORTS from MDK::Common::Math.pm</a></li> <li><a href="#other_in_mdk::common::math.pm">OTHER in MDK::Common::Math.pm</a></li> <li><a href="#exports_from_mdk::common::string.pm">EXPORTS from MDK::Common::String.pm</a></li> <li><a href="#exports_from_mdk::common::system.pm">EXPORTS from MDK::Common::System.pm</a></li> <li><a href="#other_in_mdk::common::system.pm">OTHER in MDK::Common::System.pm</a></li> <li><a href="#exports_from_mdk::common::various.pm">EXPORTS from MDK::Common::Various.pm</a></li> <li><a href="#copyright">COPYRIGHT</a></li> </ul> <!-- INDEX END --> <hr /> <p> </p> <h1><a name="name">NAME</a></h1> <p>MDK::Common - miscellaneous functions</p> <p> </p> <hr /> <h1><a name="synopsis">SYNOPSIS</a></h1> <pre> use MDK::Common; # exports all functions, equivalent to</pre> <pre> 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);</pre> <p> </p> <hr /> <h1><a name="description">DESCRIPTION</a></h1> <p><code>MDK::Common</code> is a collection of packages containing various simple functions: <a href="/MDK/Common/DataStructure.html">the MDK::Common::DataStructure manpage</a>, <a href="/MDK/Common/File.html">the MDK::Common::File manpage</a>, <a href="/MDK/Common/Func.html">the MDK::Common::Func manpage</a>, <a href="/MDK/Common/Globals.html">the MDK::Common::Globals manpage</a>, <a href="/MDK/Common/Math.html">the MDK::Common::Math manpage</a>, <a href="/MDK/Common/String.html">the MDK::Common::String manpage</a>, <a href="/MDK/Common/System.html">the MDK::Common::System manpage</a>, <a href="/MDK/Common/Various.html">the MDK::Common::Various manpage</a>.</p> <p> </p> <hr /> <h1><a name="exports_from_mdk::common::datastructure.pm">EXPORTS from MDK::Common::DataStructure.pm</a></h1> <dl> <dt><strong><a name="item_sort_numbers"><code>sort_numbers(LIST)</code></a></strong><br /> </dt> <dd> numerical sort (small numbers at beginning) </dd> <p></p> <dt><strong><a name="item_ikeys"><code>ikeys(HASH)</code></a></strong><br /> </dt> <dd> aka <em>sorted integer keys</em>, as simple as <code>sort { $a <=> $b } keys</code> </dd> <p></p> <dt><strong><a name="item_add2hash">add2hash(HASH REF, HASH REF)</a></strong><br /> </dt> <dd> adds to the first hash the second hash if the key/value is not already there </dd> <p></p> <dt><strong><a name="item_add2hash_">add2hash_</a></strong><br /> </dt> <dd> adds to the first hash the second hash if the key is not already there </dd> <p></p> <dt><strong><a name="item_put_in_hash">put_in_hash</a></strong><br /> </dt> <dd> adds to the first hash the second hash, crushing existing key/values </dd> <p></p> <dt><strong><a name="item_member">member(SCALAR, LIST)</a></strong><br /> </dt> <dd> is the value in the list? </dd> <p></p> <dt><strong><a name="item_invbool">invbool(SCALAR REF)</a></strong><br /> </dt> <dd> toggles the boolean value </dd> <p></p> <dt><strong><a name="item_listlength"><code>listlength(LIST)</code></a></strong><br /> </dt> <dd> returns the length of the list. Useful in list (opposed to array) context: </dd> <dd> <pre> sub f { "a", "b" } my $l = listlength f();</pre> </dd> <dd> <p>whereas <code>scalar f()</code> would return ``b''</p> </dd> <p></p> <dt><strong><a name="item_deref"><code>deref(REF)</code></a></strong><br /> </dt> <dd> de-reference </dd> <p></p> <dt><strong><a name="item_deref_array"><code>deref_array(REF)</code></a></strong><br /> </dt> <dd> de-reference arrays: </dd> <dd> <pre> deref_array [ "a", "b" ] #=> ("a", "b") deref_array "a" #=> "a"</pre> </dd> <p></p> <dt><strong><a name="item_is_empty_array_ref"><code>is_empty_array_ref(SCALAR)</code></a></strong><br /> </dt> <dd> is the scalar undefined or is the array empty </dd> <p></p> <dt><strong><a name="item_is_empty_hash_ref"><code>is_empty_hash_ref(SCALAR)</code></a></strong><br /> </dt> <dd> is the scalar undefined or is the hash empty </dd> <p></p> <dt><strong><a name="item_uniq"><code>uniq(LIST)</code></a></strong><br /> </dt> <dd> returns the list with no duplicates (keeping the first elements) </dd> <p></p> <dt><strong><a name="item_difference2">difference2(ARRAY REF, ARRAY REF)</a></strong><br /> </dt> <dd> returns the first list without the element of the second list </dd> <p></p> <dt><strong><a name="item_intersection">intersection(ARRAY REF, ARRAY REF, ...)</a></strong><br /> </dt> <dd> returns the elements which are in all lists </dd> <p></p> <dt><strong><a name="item_next_val_in_array">next_val_in_array(SCALAR, ARRAY REF)</a></strong><br /> </dt> <dd> finds the value that follow the scalar in the list (circular): <a href="#item_next_val_in_array"><code>next_val_in_array(3, [1, 2, 3])</code></a> gives <code>1</code> (do not use a list with duplicates) </dd> <p></p> <dt><strong><a name="item_group_by2"><code>group_by2(LIST)</code></a></strong><br /> </dt> <dd> interprets the list as an ordered hash, returns a list of [key,value]: <a href="#item_group_by2"><code>group_by2(1 =</code></a> 2, 3 => 4, 5 => 6)> gives <code>[1,2], [3,4], [5,6]</code> </dd> <p></p> <dt><strong><a name="item_list2kv"><code>list2kv(LIST)</code></a></strong><br /> </dt> <dd> interprets the list as an ordered hash, returns the keys and the values: <a href="#item_list2kv"><code>list2kv(1 =</code></a> 2, 3 => 4, 5 => 6)> gives <code>[1,3,5], [2,4,6]</code> </dd> <p></p></dl> <p> </p> <hr /> <h1><a name="exports_from_mdk::common::file.pm">EXPORTS from MDK::Common::File.pm</a></h1> <dl> <dt><strong><a name="item_dirname"><code>dirname(FILENAME)</code></a></strong><br /> </dt> <dt><strong><a name="item_basename"><code>basename(FILENAME)</code></a></strong><br /> </dt> <dd> returns the dirname/basename of the file name </dd> <p></p> <dt><strong><a name="item_cat_"><code>cat_(FILENAME)</code></a></strong><br /> </dt> <dd> returns the file content: in scalar context it returns a single string, in array context it returns the lines. </dd> <dd> <p>If the file doesn't exist, it returns undef</p> </dd> <p></p> <dt><strong><a name="item_cat_or_die"><code>cat_or_die(FILENAME)</code></a></strong><br /> </dt> <dd> same as <a href="#item_cat_"><code>cat_</code></a> but dies when something goes wrong </dd> <p></p> <dt><strong><a name="item_cat__">cat__(FILEHANDLE REF)</a></strong><br /> </dt> <dd> returns the file content: in scalar context it returns a single string, in array context it returns the lines </dd> <p></p> <dt><strong><a name="item_output">output(FILENAME, LIST)</a></strong><br /> </dt> <dd> creates a file and outputs the list (if the file exists, it is clobbered) </dd> <p></p> <dt><strong><a name="item_append_to_file">append_to_file(FILENAME, LIST)</a></strong><br /> </dt> <dd> add the LIST at the end of the file </dd> <p></p> <dt><strong><a name="item_output_p">output_p(FILENAME, LIST)</a></strong><br /> </dt> <dd> just like <a href="#item_output"><code>output</code></a> but creates directories if needed </dd> <p></p> <dt><strong><a name="item_output_with_perm">output_with_perm(FILENAME, PERMISSION, LIST)</a></strong><br /> </dt> <dd> same as <a href="#item_output_p"><code>output_p</code></a> but sets FILENAME permission to PERMISSION (using chmod) </dd> <p></p> <dt><strong><a name="item_mkdir_p"><code>mkdir_p(DIRNAME)</code></a></strong><br /> </dt> <dd> creates the directory (make parent directories as needed) </dd> <p></p> <dt><strong><a name="item_rm_rf"><code>rm_rf(FILES)</code></a></strong><br /> </dt> <dd> remove the files (including sub-directories) </dd> <p></p> <dt><strong><a name="item_cp_af">cp_af(FILES, DEST)</a></strong><br /> </dt> <dd> just like ``cp -af'' </dd> <p></p> <dt><strong><a name="item_linkf">linkf(SOURCE, DESTINATION)</a></strong><br /> </dt> <dt><strong><a name="item_symlinkf">symlinkf(SOURCE, DESTINATION)</a></strong><br /> </dt> <dt><strong><a name="item_renamef">renamef(SOURCE, DESTINATION)</a></strong><br /> </dt> <dd> same as link/symlink/rename but removes the destination file first </dd> <p></p> <dt><strong><a name="item_touch"><code>touch(FILENAME)</code></a></strong><br /> </dt> <dd> ensure the file exists, set the modification time to current time </dd> <p></p> <dt><strong><a name="item_all"><code>all(DIRNAME)</code></a></strong><br /> </dt> <dd> returns all the file in directory (except ``.'' and ``..'') </dd> <p></p> <dt><strong><a name="item_glob_"><code>glob_(STRING)</code></a></strong><br /> </dt> <dd> simple version of <code>glob</code>: doesn't handle wildcards in directory (eg: */foo.c), nor special constructs (eg: [0-9] or {a,b}) </dd> <p></p> <dt><strong><a name="item_substinfile_%7b_code_%7d_filename">substInFile { CODE } FILENAME</a></strong><br /> </dt> <dd> executes the code for each line of the file. You can know the end of the file is reached using <code>eof</code> </dd> <p></p> <dt><strong><a name="item_expand_symlinks"><code>expand_symlinks(FILENAME)</code></a></strong><br /> </dt> <dd> expand the symlinks in the absolute filename: <a href="#item_expand_symlinks"><code>expand_symlinks("/etc/X11/X")</code></a> gives ``/usr/X11R6/bin/XFree86'' </dd> <p></p> <dt><strong><a name="item_openfilemaybecompressed"><code>openFileMaybeCompressed(FILENAME)</code></a></strong><br /> </dt> <dd> opens the file and returns the file handle. If the file is not found, tries to gunzip the file + .gz </dd> <p></p> <dt><strong><a name="item_catmaybecompressed"><code>catMaybeCompressed(FILENAME)</code></a></strong><br /> </dt> <dd> cat_ alike. If the file is not found, tries to gunzip the file + .gz </dd> <p></p></dl> <p> </p> <hr /> <h1><a name="exports_from_mdk::common::func.pm">EXPORTS from MDK::Common::Func.pm</a></h1> <dl> <dt><strong><a name="item_may_apply">may_apply(CODE REF, SCALAR)</a></strong><br /> </dt> <dd> <a href="#item_may_apply"><code>may_apply($f, $v)</code></a> is <code>$f ? $f->($v) : $v</code> </dd> <p></p> <dt><strong>may_apply(CODE REF, SCALAR, SCALAR)</strong><br /> </dt> <dd> <a href="#item_may_apply"><code>may_apply($f, $v, $otherwise)</code></a> is <code>$f ? $f->($v) : $otherwise</code> </dd> <p></p> <dt><strong><a name="item_if_">if_(BOOL, LIST)</a></strong><br /> </dt> <dd> special constructs to workaround a missing perl feature: <a href="#item_if_"><code>if_($b, "a", "b")</code></a> is <code>$b ? ("a", "b") : ()</code> </dd> <dd> <p>example of use: <code>f("a", if_(arch() =~ /i.86/, "b"), "c")</code> which is not the same as <code>f("a", arch()=~ /i.86/ && "b", "c")</code></p> </dd> <p></p> <dt><strong><a name="item_if__">if__(SCALAR, LIST)</a></strong><br /> </dt> <dd> if_ alike. Test if the value is defined </dd> <p></p> <dt><strong><a name="item_fold_left_%7b_code_%7d_list">fold_left { CODE } LIST</a></strong><br /> </dt> <dd> if you don't know fold_left (aka foldl), don't use it ;p </dd> <dd> <pre> fold_left { $::a + $::b } 1, 3, 6</pre> </dd> <dd> <p>gives 10 (aka 1+3+6)</p> </dd> <p></p> <dt><strong><a name="item_mapn_%7b_code_%7d_array_ref%2c_array_ref%2c_%2e%2e">mapn { CODE } ARRAY REF, ARRAY REF, ...</a></strong><br /> </dt> <dd> map lists in parallel: </dd> <dd> <pre> mapn { $_[0] + $_[1] } [1, 2], [2, 4] # gives 3, 6 mapn { $_[0] + $_[1] + $_[2] } [1, 2], [2, 4], [3, 6] gives 6, 12</pre> </dd> <p></p> <dt><strong><a name="item_mapn__%7b_code_%7d_array_ref%2c_array_ref%2c_%2e%2">mapn_ { CODE } ARRAY REF, ARRAY REF, ...</a></strong><br /> </dt> <dd> 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 </dd> <p></p> <dt><strong><a name="item_find_%7b_code_%7d_list">find { CODE } LIST</a></strong><br /> </dt> <dd> returns the first element where CODE returns true (or returns undef) </dd> <dd> <pre> find { /foo/ } "fo", "fob", "foobar", "foobir"</pre> </dd> <dd> <p>gives ``foobar''</p> </dd> <p></p> <dt><strong><a name="item_any_%7b_code_%7d_list">any { CODE } LIST</a></strong><br /> </dt> <dd> returns 1 if CODE returns true for an element in LIST (otherwise returns 0) </dd> <dd> <pre> any { /foo/ } "fo", "fob", "foobar", "foobir"</pre> </dd> <dd> <p>gives 1</p> </dd> <p></p> <dt><strong><a name="item_every_%7b_code_%7d_list">every { CODE } LIST</a></strong><br /> </dt> <dd> returns 1 if CODE returns true for <strong>every</strong> element in LIST (otherwise returns 0) </dd> <dd> <pre> every { /foo/ } "fo", "fob", "foobar", "foobir"</pre> </dd> <dd> <p>gives 0</p> </dd> <p></p> <dt><strong><a name="item_map_index_%7b_code_%7d_list">map_index { CODE } LIST</a></strong><br /> </dt> <dd> just like <code>map</code>, but set <code>$::i</code> to the current index in the list: </dd> <dd> <pre> map_index { "$::i $_" } "a", "b"</pre> </dd> <dd> <p>gives ``0 a'', ``1 b''</p> </dd> <p></p> <dt><strong><a name="item_each_index_%7b_code_%7d_list">each_index { CODE } LIST</a></strong><br /> </dt> <dd> just like <code>map_index</code>, but doesn't return anything </dd> <dd> <pre> each_index { print "$::i $_\n" } "a", "b"</pre> </dd> <dd> <p>prints ``0 a'', ``1 b''</p> </dd> <p></p> <dt><strong><a name="item_grep_index_%7b_code_%7d_list">grep_index { CODE } LIST</a></strong><br /> </dt> <dd> just like <code>grep</code>, but set <code>$::i</code> to the current index in the list: </dd> <dd> <pre> grep_index { $::i == $_ } 0, 2, 2, 3</pre> </dd> <dd> <p>gives (0, 2, 3)</p> </dd> <p></p> <dt><strong><a name="item_find_index_%7b_code_%7d_list">find_index { CODE } LIST</a></strong><br /> </dt> <dd> returns the index of the first element where CODE returns true (or throws an exception) </dd> <dd> <pre> find_index { /foo/ } "fo", "fob", "foobar", "foobir"</pre> </dd> <dd> <p>gives 2</p> </dd> <p></p> <dt><strong><a name="item_map_each_%7b_code_%7d_hash">map_each { CODE } HASH</a></strong><br /> </dt> <dd> returns the list of results of CODE applied with $::a (key) and $::b (value) </dd> <dd> <pre> map_each { "$::a is $::b" } 1=>2, 3=>4</pre> </dd> <dd> <p>gives ``1 is 2'', ``3 is 4''</p> </dd> <p></p> <dt><strong><a name="item_grep_each_%7b_code_%7d_hash">grep_each { CODE } HASH</a></strong><br /> </dt> <dd> returns the hash key/value for which CODE applied with $::a (key) and $::b (value) is true: </dd> <dd> <pre> grep_each { $::b == 2 } 1=>2, 3=>4, 4=>2</pre> </dd> <dd> <p>gives 1=>2, 4=>2</p> </dd> <p></p> <dt><strong><a name="item_partition_%7b_code_%7d_list">partition { CODE } LIST</a></strong><br /> </dt> <dd> alike <code>grep</code>, but returns both the list of matching elements and non matching elements </dd> <dd> <pre> my ($greater, $lower) = partition { $_ > 3 } 4, 2, 8, 0, 1</pre> </dd> <dd> <p>gives $greater = [ 4, 8 ] and $lower = [ 2, 0, 1 ]</p> </dd> <p></p> <dt><strong><a name="item_before_leaving">before_leaving { CODE }</a></strong><br /> </dt> <dd> the code will be executed when the current block is finished </dd> <dd> <pre> # 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</pre> </dd> <p></p> <dt><strong><a name="item_cdie"><code>cdie(SCALAR)</code></a></strong><br /> </dt> <dd> aka <em>conditional die</em>. If a <a href="#item_cdie"><code>cdie</code></a> is catched, the execution continues <strong>after</strong> the cdie, not where it was catched (as happens with die & eval) </dd> <dd> <p>If a <a href="#item_cdie"><code>cdie</code></a> is not catched, it mutates in real exception that can be catched with <code>eval</code></p> </dd> <dd> <p>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.</p> </dd> <p></p> <dt><strong><a name="item_catch_cdie">catch_cdie { CODE1 } sub { CODE2 }</a></strong><br /> </dt> <dd> If a <a href="#item_cdie"><code>cdie</code></a> occurs while executing CODE1, CODE2 is executed. If CODE2 returns true, the <a href="#item_cdie"><code>cdie</code></a> is catched. </dd> <p></p></dl> <p> </p> <hr /> <h1><a name="exports_from_mdk::common::math.pm">EXPORTS from MDK::Common::Math.pm</a></h1> <dl> <dt><strong><a name="item_%24pi">$PI</a></strong><br /> </dt> <dd> the well-known constant </dd> <p></p> <dt><strong><a name="item_even"><code>even(INT)</code></a></strong><br /> </dt> <dt><strong><a name="item_odd"><code>odd(INT)</code></a></strong><br /> </dt> <dd> is the number even or odd? </dd> <p></p> <dt><strong><a name="item_sqr"><code>sqr(FLOAT)</code></a></strong><br /> </dt> <dd> <a href="#item_sqr"><code>sqr(3)</code></a> gives <code>9</code> </dd> <p></p> <dt><strong><a name="item_sign"><code>sign(FLOAT)</code></a></strong><br /> </dt> <dd> returns a value in { -1, 0, 1 } </dd> <p></p> <dt><strong><a name="item_round"><code>round(FLOAT)</code></a></strong><br /> </dt> <dd> <a href="#item_round"><code>round(1.2)</code></a> gives <code>1</code>, <a href="#item_round"><code>round(1.6)</code></a> gives <code>2</code> </dd> <p></p> <dt><strong><a name="item_round_up">round_up(FLOAT, INT)</a></strong><br /> </dt> <dd> returns the number rounded up to the modulo: <a href="#item_round_up"><code>round_up(11,10)</code></a> gives <code>20</code> </dd> <p></p> <dt><strong><a name="item_round_down">round_down(FLOAT, INT)</a></strong><br /> </dt> <dd> returns the number rounded down to the modulo: <a href="#item_round_down"><code>round_down(11,10)</code></a> gives <code>10</code> </dd> <p></p> <dt><strong><a name="item_divide">divide(INT, INT)</a></strong><br /> </dt> <dd> integer division (which is lacking in perl). In array context, also returns the remainder: <a href="#item_divide"><code>($a, $b) = divide(10,3)</code></a> gives <code>$a is 3</code> and <code>$b is 1</code> </dd> <p></p> <dt><strong><a name="item_min"><code>min(LIST)</code></a></strong><br /> </dt> <dt><strong><a name="item_max"><code>max(LIST)</code></a></strong><br /> </dt> <dd> <pre> returns the minimum/maximum number in the list</pre> </dd> <dt><strong><a name="item_or_"><code>or_(LIST)</code></a></strong><br /> </dt> <dd> is there a true value in the list? </dd> <p></p> <dt><strong><a name="item_and_"><code>and_(LIST)</code></a></strong><br /> </dt> <dd> are all values true in the list? </dd> <p></p> <dt><strong><a name="item_sum"><code>sum(LIST)</code></a></strong><br /> </dt> <dt><strong><a name="item_product"><code>product(LIST)</code></a></strong><br /> </dt> <dd> returns the sum/product of all the element in the list </dd> <p></p> <dt><strong><a name="item_factorial"><code>factorial(INT)</code></a></strong><br /> </dt> <dd> <a href="#item_factorial"><code>factorial(4)</code></a> gives <code>24</code> (4*3*2) </dd> <p></p></dl> <p> </p> <hr /> <h1><a name="other_in_mdk::common::math.pm">OTHER in MDK::Common::Math.pm</a></h1> <p>the following functions are provided, but not exported:</p> <dl> <dt><strong><a name="item_factorize"><code>factorize(INT)</code></a></strong><br /> </dt> <dd> <a href="#item_factorize"><code>factorize(40)</code></a> gives <code>([2,3], [5,1])</code> as 40 = 2^3 + 5^1 </dd> <p></p> <dt><strong><a name="item_decimal2fraction"><code>decimal2fraction(FLOAT)</code></a></strong><br /> </dt> <dd> <a href="#item_decimal2fraction"><code>decimal2fraction(1.3333333333)</code></a> gives <code>(4, 3)</code> ($PRECISION is used to decide which precision to use) </dd> <p></p> <dt><strong><a name="item_poly2"><code>poly2(a,b,c)</code></a></strong><br /> </dt> <dd> Solves the a*x2+b*x+c=0 polynomial: <a href="#item_poly2"><code>poly2(1,0,-1)</code></a> gives <code>(1, -1)</code> </dd> <p></p> <dt><strong><a name="item_permutations"><code>permutations(n,p)</code></a></strong><br /> </dt> <dd> <code>A(n,p)</code> </dd> <p></p> <dt><strong><a name="item_combinaisons"><code>combinaisons(n,p)</code></a></strong><br /> </dt> <dd> <code>C(n,p)</code> </dd> <p></p></dl> <p> </p> <hr /> <h1><a name="exports_from_mdk::common::string.pm">EXPORTS from MDK::Common::String.pm</a></h1> <dl> <dt><strong><a name="item_bestmatchsentence">bestMatchSentence(STRING, LIST)</a></strong><br /> </dt> <dd> finds in the list the best corresponding string </dd> <p></p> <dt><strong><a name="item_formatlist">formatList(INT, LIST)</a></strong><br /> </dt> <dd> if the list size is bigger than INT, replace the remaining elements with ``...''. </dd> <dd> <p>formatList(3, qw(a b c d e)) # => ``a, b, c, ...''</p> </dd> <p></p> <dt><strong><a name="item_formaterror"><code>formatError(STRING)</code></a></strong><br /> </dt> <dd> 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 </dd> <p></p> <dt><strong><a name="item_formattimeraw"><code>formatTimeRaw(TIME)</code></a></strong><br /> </dt> <dd> the TIME is an epoch as returned by <code>time</code>, the formatted time looks like ``23:59:00'' </dd> <p></p> <dt><strong><a name="item_formatlines"><code>formatLines(STRING)</code></a></strong><br /> </dt> <dd> remove ``\n''s when the next line doesn't start with a space. Otherwise keep ``\n''s to keep the indentation. </dd> <p></p> <dt><strong><a name="item_formatalatex"><code>formatAlaTeX(STRING)</code></a></strong><br /> </dt> <dd> handle carriage return just like LaTeX: merge lines that are not separated by an empty line </dd> <p></p> <dt><strong><a name="item_warp_text">warp_text(STRING, INT)</a></strong><br /> </dt> <dd> return a list of lines which do not exceed INT characters (or a string in scalar context) </dd> <p></p> <dt><strong><code>warp_text(STRING)</code></strong><br /> </dt> <dd> warp_text at a default width (80) </dd> <p></p></dl> <p> </p> <hr /> <h1><a name="exports_from_mdk::common::system.pm">EXPORTS from MDK::Common::System.pm</a></h1> <dl> <dt><strong><a name="item_%compat_arch">%compat_arch</a></strong><br /> </dt> <dd> architecture compatibility mapping (eg: k6 => i586, k7 => k6 ...) </dd> <p></p> <dt><strong><a name="item_%printable_chars">%printable_chars</a></strong><br /> </dt> <dd> 7 bit ascii characters </dd> <p></p> <dt><strong><a name="item_%24sizeof_int">$sizeof_int</a></strong><br /> </dt> <dd> <code>sizeof(int)</code> </dd> <p></p> <dt><strong><a name="item_%24bitof_int">$bitof_int</a></strong><br /> </dt> <dd> $sizeof_int * 8 </dd> <p></p> <dt><strong><a name="item_arch"><code>arch()</code></a></strong><br /> </dt> <dd> return the architecture (eg: i686, ppc, ia64, k7...) </dd> <p></p> <dt><strong><a name="item_typefrommagic">typeFromMagic(FILENAME, LIST)</a></strong><br /> </dt> <dd> find the first corresponding magic in FILENAME. eg of LIST: </dd> <dd> <pre> [ 'empty', 0, "\0\0\0\0" ], [ 'grub', 0, "\xEBG", 0x17d, "stage1 \0" ], [ 'lilo', 0x2, "LILO" ],</pre> </dd> <dd> <p>where each entry is [ magic_name, offset, string, offset, string, ... ].</p> </dd> <p></p> <dt><strong><a name="item_list_passwd"><code>list_passwd()</code></a></strong><br /> </dt> <dd> return the list of users as given by <code>getpwent</code> (see perlfunc) </dd> <p></p> <dt><strong><a name="item_list_home"><code>list_home()</code></a></strong><br /> </dt> <dd> return the list of home (eg: /home/foo, /home/pixel, ...) </dd> <p></p> <dt><strong><a name="item_list_skels"><code>list_skels()</code></a></strong><br /> </dt> <dd> return the directories where we can find dot files: homes, /root and /etc/skel </dd> <p></p> <dt><strong><a name="item_list_users"><code>list_users()</code></a></strong><br /> </dt> <dd> return the list of unprivilegied users (aka those whose uid is greater than 500 and who are not ``nobody''). </dd> <p></p> <dt><strong><a name="item_syscall_">syscall_(NAME, PARA)</a></strong><br /> </dt> <dd> calls the syscall NAME </dd> <p></p> <dt><strong><a name="item_psizeof"><code>psizeof(STRING)</code></a></strong><br /> </dt> <dd> useful to know the length of a <code>pack</code> format string. </dd> <dd> <pre> psizeof("I I I C C S") = 4 + 4 + 4 + 1 + 1 + 2 = 16</pre> </dd> <p></p> <dt><strong><a name="item_availablememory"><code>availableMemory()</code></a></strong><br /> </dt> <dd> size of swap + memory </dd> <p></p> <dt><strong><a name="item_availablerammb"><code>availableRamMB()</code></a></strong><br /> </dt> <dd> size of RAM as reported by the BIOS (it is a round number that can be displayed or given as ``mem=128M'' to the kernel) </dd> <dd> <p>!! ``mem=...'' is dangerous in 2.4 kernels</p> </dd> <p></p> <dt><strong><a name="item_gettimeofday"><code>gettimeofday()</code></a></strong><br /> </dt> <dd> returns the epoch in microseconds </dd> <p></p> <dt><strong><a name="item_unix2dos"><code>unix2dos(STRING)</code></a></strong><br /> </dt> <dd> takes care of CR/LF translation </dd> <p></p> <dt><strong><a name="item_getvarsfromsh"><code>getVarsFromSh(FILENAME)</code></a></strong><br /> </dt> <dd> returns a hash associating shell variables to their value. useful for config files such as /etc/sysconfig files </dd> <p></p> <dt><strong><a name="item_setvarsinsh">setVarsInSh(FILENAME, HASH REF)</a></strong><br /> </dt> <dd> write file in shell format association a shell variable + value for each key/value </dd> <p></p> <dt><strong>setVarsInSh(FILENAME, HASH REF, LIST)</strong><br /> </dt> <dd> restrict the fields that will be printed to LIST </dd> <p></p> <dt><strong><a name="item_setvarsinshmode">setVarsInShMode(FILENAME, INT, HASH REF, LIST)</a></strong><br /> </dt> <dd> like setVarsInSh with INT being the chmod value for the config file </dd> <p></p> <dt><strong><a name="item_setexportedvarsincsh">setExportedVarsInCsh(FILENAME, HASH REF, LIST)</a></strong><br /> </dt> <dd> same as <code>setExportedVarsInSh</code> for csh format </dd> <p></p> <dt><strong><a name="item_template2file">template2file(FILENAME_IN, FILENAME_OUT, HASH)</a></strong><br /> </dt> <dd> read in a template file, replace keys @@@key@@@ with value, save it in out file </dd> <p></p> <dt><strong><a name="item_template2userfile">template2userfile(PREFIX, FILENAME_IN, FILENAME_OUT, BOOL, HASH)</a></strong><br /> </dt> <dd> read in a template file, replace keys @@@key@@@ with value, save it in every homes. If BOOL is true, overwrite existing files. FILENAME_OUT must be a relative filename </dd> <p></p> <dt><strong><a name="item_update_gnomekderc">update_gnomekderc(FILENAME, STRING, HASH)</a></strong><br /> </dt> <dd> modifies GNOME-like and KDE-like config files (aka windows-like). If the category doesn't exist, it creates it. eg: </dd> <dd> <pre> update_gnomekderc("/etc/skels/.kderc", 'KDE', kfmIconStyle => "Large")</pre> </dd> <p></p> <dt><strong><a name="item_fuzzy_pidofs"><code>fuzzy_pidofs(REGEXP)</code></a></strong><br /> </dt> <dd> return the list of process ids matching the regexp </dd> <p></p></dl> <p> </p> <hr /> <h1><a name="other_in_mdk::common::system.pm">OTHER in MDK::Common::System.pm</a></h1> <dl> <dt><strong><a name="item_better_arch">better_arch(ARCH1, ARCH2)</a></strong><br /> </dt> <dd> is ARCH1 compatible with ARCH2? </dd> <dd> <p>better_arch('i386', 'ia64') and better_arch('ia64', 'i386') are false</p> </dd> <dd> <p>better_arch('k7', 'k6') is true and better_arch('k6', 'k7') is false</p> </dd> <p></p> <dt><strong><a name="item_compat_arch"><code>compat_arch(STRING)</code></a></strong><br /> </dt> <dd> test the architecture compatibility. eg: </dd> <dd> <p><a href="#item_compat_arch"><code>compat_arch('i386')</code></a> is false on a ia64</p> </dd> <dd> <p><a href="#item_compat_arch"><code>compat_arch('k6')</code></a> is true on a k6 and k7 but false on a i386 and i686</p> </dd> <p></p></dl> <p> </p> <hr /> <h1><a name="exports_from_mdk::common::various.pm">EXPORTS from MDK::Common::Various.pm</a></h1> <dl> <dt><strong><a name="item_first"><code>first(LIST)</code></a></strong><br /> </dt> <dd> returns the first value. <a href="#item_first"><code>first(XXX)</code></a> is an alternative for <code>((XXX)[0])</code> </dd> <p></p> <dt><strong><a name="item_second"><code>second(LIST)</code></a></strong><br /> </dt> <dd> returns the second value. <a href="#item_second"><code>second(XXX)</code></a> is an alternative for <code>((XXX)[1])</code> </dd> <p></p> <dt><strong><a name="item_top"><code>top(LIST)</code></a></strong><br /> </dt> <dd> returns the last value. <a href="#item_top"><code>top(@l)</code></a> is an alternative for <code>$l[$#l]</code> </dd> <p></p> <dt><strong><a name="item_to_bool"><code>to_bool(SCALAR)</code></a></strong><br /> </dt> <dd> returns a value in { 0, 1 } </dd> <p></p> <dt><strong><a name="item_to_int"><code>to_int(STRING)</code></a></strong><br /> </dt> <dd> extracts the number from the string. You could use directly <code>int "11 foo"</code>, but you'll get <em>Argument ``11 foo'' isn't numeric in int</em>. It also handles returns 11 for <code>"foo 11 bar"</code> </dd> <p></p> <dt><strong><a name="item_to_float"><code>to_float(STRING)</code></a></strong><br /> </dt> <dd> extract a decimal number from the string </dd> <p></p> <dt><strong><a name="item_bool2text"><code>bool2text(SCALAR)</code></a></strong><br /> </dt> <dd> returns a value in { ``true'', ``false'' } </dd> <p></p> <dt><strong><a name="item_bool2yesno"><code>bool2yesno(SCALAR)</code></a></strong><br /> </dt> <dd> returns a value in { ``yes'', ``no'' } </dd> <p></p> <dt><strong><a name="item_text2bool"><code>text2bool(STRING)</code></a></strong><br /> </dt> <dd> inverse of <a href="#item_bool2text"><code>bool2text</code></a> and <a href="#item_bool2yesno"><code>bool2yesno</code></a> </dd> <p></p> <dt><strong><a name="item_chomp_"><code>chomp_(STRING)</code></a></strong><br /> </dt> <dd> non-mutable version of chomp: do not modify the argument, returns the chomp'ed value. Also works on lists: <a href="#item_chomp_"><code>chomp_($a, $b)</code></a> is equivalent to <code>chomp($a) ; chomp($b) ; ($a,$b)</code> </dd> <p></p> <dt><strong><a name="item_backtrace"><code>backtrace()</code></a></strong><br /> </dt> <dd> returns a string describing the backtrace. eg: </dd> <dd> <pre> sub g { print "oops\n", backtrace() } sub f { &g } f();</pre> </dd> <dd> <p>gives</p> </dd> <dd> <pre> oops main::g() called from /tmp/t.pl:2 main::f() called from /tmp/t.pl:4</pre> </dd> <p></p> <dt><strong><a name="item_internal_error"><code>internal_error(STRING)</code></a></strong><br /> </dt> <dd> another way to <code>die</code> with a nice error message and a backtrace </dd> <p></p> <dt><strong><a name="item_noreturn"><code>noreturn()</code></a></strong><br /> </dt> <dd> use this to ensure nobody uses the return value of the function. eg: </dd> <dd> <pre> sub g { print "g called\n"; noreturn } sub f { print "g returns ", g() } f();</pre> </dd> <dd> <p>gives</p> </dd> <dd> <pre> test.pl:3: main::f() expects a value from main::g(), but main::g() doesn't return any value =back</pre> </dd> <p></p></dl> <p> </p> <hr /> <h1><a name="copyright">COPYRIGHT</a></h1> <p>Copyright (c) 2001 MandrakeSoft <<a href="mailto:pixel@mandrakesoft.com">pixel@mandrakesoft.com</a>>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.</p> </body> </html>