MDK::Common - miscellaneous functions
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);
MDK::Common
is a collection of packages containing various simple functions:
the MDK::Common::DataStructure manpage,
the MDK::Common::File manpage,
the MDK::Common::Func manpage,
the MDK::Common::Globals manpage,
the MDK::Common::Math manpage,
the MDK::Common::String manpage,
the MDK::Common::System manpage,
the MDK::Common::Various manpage.
ikeys(HASH)
sort { $a <=> $b } keys
listlength(LIST)
sub f { "a", "b" } my $l = listlength f();
whereas scalar f()
would return ``b''
deref(REF)
is_empty_array_ref(SCALAR)
is_empty_hash_ref(SCALAR)
uniq(LIST)
next_val_in_array(3, [1, 2, 3])
gives 1
(do not use a list with duplicates)
list2kv(LIST)
list2kv(1 =
2, 3 => 4)> gives [1,3], [2,4]
dirname(FILENAME)
basename(FILENAME)
cat_(FILENAME)
If the file doesn't exist, it returns undef
output
but creates directories if needed
mkdir_p(DIRNAME)
rm_rf(FILES)
touch(FILENAME)
all(DIRNAME)
glob_(STRING)
glob
: doesn't handle wildcards in directory (eg:
*/foo.c), nor special constructs (eg: [0-9] or {a,b})
eof
expand_symlinks(FILENAME)
expand_symlinks("/etc/X11/X")
gives ``/usr/X11R6/bin/XFree86''
openFileMaybeCompressed(FILENAME)
catMaybeCompressed(FILENAME)
may_apply($f, $v)
is $f ? $f->($v) : $v
may_apply($f, $v, $otherwise)
is $f ? $f->($v) : $otherwise
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")
fold_left { $::a + $::b } 1, 3, 6
gives 10 (aka 1+3+6)
mapn { $_[0] + $_[1] } [1, 2], [2, 4] # gives 3, 6 mapn { $_[0] + $_[1] + $_[2] } [1, 2], [2, 4], [3, 6] gives 6, 12
map
, but set $::i
to the current index in the list:
map_index { "$::i $_" } "a", "b"
gives ``0 a'', ``1 b''
map_index
, but doesn't return anything
each_index { print "$::i $_\n" } "a", "b"
prints ``0 a'', ``1 b''
grep
, but set $::i
to the current index in the list:
grep_index { $::i == $_ } 0, 2, 2, 3
gives (0, 2, 3)
find_index { /foo/ } "fo", "fob", "foobar", "foobir"
gives 2
map_each { "$::a is $::b" } 1=>2, 3=>4
gives ``1 is 2'', ``3 is 4''
grep_each { $::b == 2 } 1=>2, 3=>4, 4=>2
gives 1=>2, 4=>2
# 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)
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.
cdie
occurs while executing CODE1, CODE2 is executed. If CODE2
returns true, the cdie
is catched.
even(INT)
odd(INT)
sqr(FLOAT)
sqr(3)
gives 9
sign(FLOAT)
round(FLOAT)
round(1.2)
gives 1
, round(1.6)
gives 2
round_up(11,10)
gives 20
round_down(11,10)
gives 10
($a, $b) = divide(10,3)
gives $a is 3
and $b is 1
min(LIST)
max(LIST)
returns the minimum/maximum number in the list
or_(LIST)
and_(LIST)
sum(LIST)
product(LIST)
factorial(INT)
factorial(4)
gives 24
(4*3*2)
the following functions are provided, but not exported:
factorize(INT)
factorize(40)
gives ([2,3], [5,1])
as 40 = 2^3 + 5^1
decimal2fraction(FLOAT)
decimal2fraction(1.3333333333)
gives (4, 3)
($PRECISION is used to decide which precision to use)
poly2(a,b,c)
poly2(1,0,-1)
gives (1, -1)
permutations(n,p)
A(n,p)
combinaisons(n,p)
C(n,p)
formatList(3, qw(a b c d e)) # => ``a, b, c, ...''
formatError(STRING)
formatTimeRaw(TIME)
time
, the formatted time looks like ``23:59:00''
formatLines(STRING)
formatAlaTeX(STRING)
warp_text(STRING)
sizeof(int)
arch()
[ 'empty', 0, "\0\0\0\0" ], [ 'grub', 0, "\xEBG", 0x17d, "stage1 \0" ], [ 'lilo', 0x2, "LILO" ],
where each entry is [ magic_name, offset, string, offset, string, ... ].
list_passwd()
getpwent
(see perlfunc)
list_home()
list_skels()
psizeof(STRING)
pack
format string.
psizeof("I I I C C S") = 4 + 4 + 4 + 1 + 1 + 2 = 16
availableMemory()
availableRamMB()
!! ``mem=...'' is dangerous in 2.4 kernels
gettimeofday()
unix2dos(STRING)
getVarsFromSh(FILENAME)
setVarsInSh
for csh format
update_gnomekderc("/etc/skels/.kderc", 'KDE', kfmIconStyle => "Large")
better_arch('i386', 'ia64') and better_arch('ia64', 'i386') are false
better_arch('k7', 'k6') is true and better_arch('k6', 'k7') is false
compat_arch(STRING)
compat_arch('i386')
is false on a ia64
compat_arch('k6')
is true on a k6 and k7 but false on a i386 and i686
first(LIST)
first(XXX)
is an alternative for ((XXX)[0])
second(LIST)
second(XXX)
is an alternative for ((XXX)[1])
top(LIST)
top(@l)
is an alternative for $l[$#l]
to_bool(SCALAR)
to_int(STRING)
int "11 foo"
, but
you'll get Argument ``11 foo'' isn't numeric in int. It also handles returns
11 for "foo 11 bar"
to_float(STRING)
bool2text(SCALAR)
bool2yesno(SCALAR)
text2bool(STRING)
bool2text
and bool2yesno
chomp_(STRING)
chomp_($a, $b)
is equivalent to
chomp($a) ; chomp($b) ; ($a,$b)
backtrace()
sub g { print "oops\n", backtrace() } sub f { &g } f();
gives
oops main::g() called from /tmp/t.pl:2 main::f() called from /tmp/t.pl:4
=back
Copyright (c) 2001 MandrakeSoft <pixel@mandrakesoft.com>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.