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.
sort_numbers(LIST)
ikeys(HASH)
sort { $a <=> $b } keys
listlength(LIST)
sub f { "a", "b" } my $l = listlength f();
whereas scalar f()
would return ``b''
deref(REF)
deref_array(REF)
deref_array [ "a", "b" ] #=> ("a", "b") deref_array "a" #=> "a"
is_empty_array_ref(SCALAR)
is_empty_hash_ref(SCALAR)
uniq(LIST)
uniq_ { $_->[1] } [ 1, "fo" ], [ 2, "fob" ], [ 3, "fo" ], [ 4, "bar" ]
gives [ 1, ``fo'' ], [ 2, ``fob'' ], [ 4, ``bar'' ]
next_val_in_array(3, [1, 2, 3])
gives 1
(do not use a list with duplicates)
group_by2(LIST)
group_by2(1 =
2, 3 => 4, 5 => 6)> gives [1,2], [3,4], [5,6]
list2kv(LIST)
list2kv(1 =
2, 3 => 4, 5 => 6)> gives [1,3,5], [2,4,6]
dirname(FILENAME)
basename(FILENAME)
cat_(FILENAME)
If the file doesn't exist, it returns undef
cat_or_die(FILENAME)
cat_
but dies when something goes wrong
output()
but prevents insecured usage (it dies if somebody try
to exploit the race window between unlink()
and creat())
output
but creates directories if needed
output_p
but sets FILENAME permission to PERMISSION (using chmod)
mkdir_p(DIRNAME)
rm_rf(FILES)
touch(FILENAME)
all(DIRNAME)
all_files_rec(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
find { /foo/ } "fo", "fob", "foobar", "foobir"
gives ``foobar''
any { /foo/ } "fo", "fob", "foobar", "foobir"
gives 1
every { /foo/ } "fo", "fob", "foobar", "foobir"
gives 0
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
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 ]
# 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)
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)
begins_with(``hello world'', ``hello'') # => 1
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()
list_users()
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)
whereis_binary(STRING)
which(1)
and whereis(1))
getVarsFromSh(FILENAME)
setExportedVarsInSh
for csh format
read_gnomekderc("/etc/skels/.kderc", 'KDE')
update_gnomekderc("/etc/skels/.kderc", 'KDE', kfmIconStyle => "Large")
fuzzy_pidofs(REGEXP)
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
internal_error(STRING)
die
with a nice error message and a backtrace
noreturn()
sub g { print "g called\n"; noreturn } sub f { print "g returns ", g() } f();
gives
test.pl:3: main::f() expects a value from main::g(), but main::g() doesn't return any value
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.