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: MDK::Common::DataStructure, MDK::Common::File, MDK::Common::Func, MDK::Common::Math, MDK::Common::String, MDK::Common::System, MDK::Common::Various.
MDK::Common
numerical sort (small numbers at beginning)
aka sorted integer keys, as simple as sort { $a <=> $b } keys
sort { $a <=> $b } keys
adds to the first hash the second hash if the key/value is not already there
adds to the first hash the second hash if the key is not already there
adds to the first hash the second hash, crushing existing key/values
is the value in the list?
toggles the boolean value
returns the length of the list. Useful in list (opposed to array) context:
sub f { "a", "b" } my $l = listlength f();
whereas scalar f() would return "b"
scalar f()
de-reference
de-reference arrays:
deref_array [ "a", "b" ] #=> ("a", "b") deref_array "a" #=> "a"
is the scalar undefined or is the array empty
is the scalar undefined or is the hash empty
returns the list with no duplicates (keeping the first elements)
returns the list with no duplicates according to the scalar results of CODE on each element of LIST (keeping the first elements)
uniq_ { $_->[1] } [ 1, "fo" ], [ 2, "fob" ], [ 3, "fo" ], [ 4, "bar" ]
gives [ 1, "fo" ], [ 2, "fob" ], [ 4, "bar" ]
returns the first list without the element of the second list
returns the elements which are in all lists
finds the value that follow the scalar in the list (circular): next_val_in_array(3, [1, 2, 3]) gives 1 (do not use a list with duplicates)
next_val_in_array(3, [1, 2, 3])
1
interprets the list as an ordered hash, returns a list of [key,value]: group_by2(1 = 2, 3 => 4, 5 => 6)> gives [1,2], [3,4], [5,6]
group_by2(1 =
[1,2], [3,4], [5,6]
interprets the list as an ordered hash, returns the keys and the values: list2kv(1 = 2, 3 => 4, 5 => 6)> gives [1,3,5], [2,4,6]
list2kv(1 =
[1,3,5], [2,4,6]
returns the dirname/basename of the file name
returns the files contents: in scalar context it returns a single string, in array context it returns the lines.
If no file is found, undef is returned
same as cat_ but dies when something goes wrong
cat_
same as C(<cat_>) but reads utf8 encoded strings
same as C(<cat_or_die>) but reads utf8 encoded strings
returns the file content: in scalar context it returns a single string, in array context it returns the lines
creates a file and outputs the list (if the file exists, it is clobbered)
same as C(<output>) but writes utf8 encoded strings
likes output() but prevents insecured usage (it dies if somebody try to exploit the race window between unlink() and creat())
add the LIST at the end of the file
just like output but creates directories if needed
output
same as output_p but sets FILENAME permission to PERMISSION (using chmod)
output_p
creates the directory (make parent directories as needed)
remove the files (including sub-directories)
just like "cp -f"
just like "cp -af"
just like "cp -afx"
same as link/symlink/rename but removes the destination file first
ensure the file exists, set the modification time to current time
returns all the file in directory (except "." and "..")
returns all the files in directory and the sub-directories (except "." and "..")
simple version of glob: doesn't handle wildcards in directory (eg: */foo.c), nor special constructs (eg: [0-9] or {a,b})
glob
executes the code for each line of the file. You can know the end of the file is reached using eof
eof
expand the symlinks in the absolute filename: expand_symlinks("/etc/X11/X") gives "/usr/bin/Xorg"
expand_symlinks("/etc/X11/X")
opens the file and returns the file handle. If the file is not found, tries to gunzip the file + .gz
cat_ alike. If the file is not found, tries to gunzip the file + .gz
may_apply($f, $v) is $f ? $f->($v) : $v
may_apply($f, $v)
$f ? $f->($v) : $v
may_apply($f, $v, $otherwise) is $f ? $f->($v) : $otherwise
may_apply($f, $v, $otherwise)
$f ? $f->($v) : $otherwise
special constructs to workaround a missing perl feature: if_($b, "a", "b") is $b ? ("a", "b") : ()
if_($b, "a", "b")
$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")
f("a", if_(arch() =~ /i.86/, "b"), "c")
f("a", arch()=~ /i.86/ && "b", "c")
if_ alike. Test if the value is defined
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)
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
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
returns the first element where CODE returns true (or returns undef)
find { /foo/ } "fo", "fob", "foobar", "foobir"
gives "foobar"
returns 1 if CODE returns true for an element in LIST (otherwise returns 0)
any { /foo/ } "fo", "fob", "foobar", "foobir"
gives 1
returns 1 if CODE returns true for every element in LIST (otherwise returns 0)
every { /foo/ } "fo", "fob", "foobar", "foobir"
gives 0
just like map, but set $::i to the current index in the list:
map
$::i
map_index { "$::i $_" } "a", "b"
gives "0 a", "1 b"
just like map_index, but doesn't return anything
map_index
each_index { print "$::i $_\n" } "a", "b"
prints "0 a", "1 b"
just like grep, but set $::i to the current index in the list:
grep
grep_index { $::i == $_ } 0, 2, 2, 3
gives (0, 2, 3)
returns the index of the first element where CODE returns true (or throws an exception)
find_index { /foo/ } "fo", "fob", "foobar", "foobir"
gives 2
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"
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
alike 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 ]
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
aka conditional die. If a cdie is catched, the execution continues after the cdie, not where it was catched (as happens with die & eval)
cdie
If a cdie is not catched, it mutates in real exception that can be catched with eval
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.
If a cdie occurs while executing CODE1, CODE2 is executed. If CODE2 returns true, the cdie is catched.
the well-known constant
is the number even or odd?
sqr(3) gives 9
sqr(3)
9
returns a value in { -1, 0, 1 }
round(1.2) gives 1, round(1.6) gives 2
round(1.2)
round(1.6)
2
returns the number rounded up to the modulo: round_up(11,10) gives 20
round_up(11,10)
20
returns the number rounded down to the modulo: round_down(11,10) gives 10
round_down(11,10)
10
integer division (which is lacking in perl). In array context, also returns the remainder: ($a, $b) = divide(10,3) gives $a is 3 and $b is 1
($a, $b) = divide(10,3)
$a is 3
$b is 1
returns the minimum/maximum number in the list
is there a true value in the list?
are all values true in the list?
returns the sum/product of all the element in the list
factorial(4) gives 24 (4*3*2)
factorial(4)
24
the following functions are provided, but not exported:
factorize(40) gives ([2,3], [5,1]) as 40 = 2^3 + 5^1
factorize(40)
([2,3], [5,1])
decimal2fraction(1.3333333333) gives (4, 3) ($PRECISION is used to decide which precision to use)
decimal2fraction(1.3333333333)
(4, 3)
Solves the a*x2+b*x+c=0 polynomial: poly2(1,0,-1) gives (1, -1)
poly2(1,0,-1)
(1, -1)
A(n,p)
C(n,p)
finds in the list the best corresponding string
if the list size is bigger than INT, replace the remaining elements with "...".
formatList(3, qw(a b c d e)) # => "a, b, c, ..."
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
the TIME is an epoch as returned by time, the formatted time looks like "23:59:00"
time
remove "\n"s when the next line doesn't start with a space. Otherwise keep "\n"s to keep the indentation.
handle carriage return just like LaTeX: merge lines that are not separated by an empty line
return true if first argument begins with the second argument. Use this instead of regexps if you don't want regexps.
begins_with("hello world", "hello") # => 1
return a list of lines which do not exceed INT characters (or a string in scalar context)
warp_text at a default width (80)
architecture compatibility mapping (eg: k6 => i586, k7 => k6 ...)
7 bit ascii characters
sizeof(int)
$sizeof_int * 8
return the architecture (eg: i686, ppc, ia64, k7...)
find the first corresponding magic in FILENAME. eg of LIST:
[ 'empty', 0, "\0\0\0\0" ], [ 'grub', 0, "\xEBG", 0x17d, "stage1 \0" ], [ 'lilo', 0x2, "LILO" ], sub { my ($F) = @_; #- standard grub has no good magic (Mageia's grub is patched to have "GRUB" at offset 6) #- so scanning a range of possible places where grub can have its string my ($min, $max, $magic) = (0x176, 0x181, "GRUB \0"); my $tmp; sysseek($F, 0, 0) && sysread($F, $tmp, $max + length($magic)) or return; substr($tmp, 0, 2) eq "\xEBH" or return; index($tmp, $magic, $min) >= 0 && "grub"; },
where each entry is [ magic_name, offset, string, offset, string, ... ].
return the list of users as given by getpwent (see perlfunc)
getpwent
checks whether or not the user is a system user or a real user
checks whether or not the group is a system group or a real group
return the list of home (eg: /home/foo, /home/pixel, ...)
return the directories where we can find dot files: homes, /root and /etc/skel
return the list of unprivilegied users (uses the is_real_user function to filter out system users from the full list)
calls the syscall NAME
useful to know the length of a pack format string.
pack
psizeof("I I I C C S") = 4 + 4 + 4 + 1 + 1 + 2 = 16
size of swap + memory
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)
returns the epoch in microseconds
takes care of CR/LF translation
return the first absolute file in $PATH (similar to which(1) and whereis(1))
returns a hash associating shell variables to their value. useful for config files such as /etc/sysconfig files
write file in shell format association a shell variable + value for each key/value
restrict the fields that will be printed to LIST
like setVarsInSh with INT being the chmod value for the config file
like setVarsInSh but keeping the entries in the file
like addVarsInShMode but keeping the entries in the file
same as setExportedVarsInSh for csh format
setExportedVarsInSh
read in a template file, replace keys @@@key@@@ with value, save it in out file
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
reads GNOME-like and KDE-like config files (aka windows-like). You must give a category. eg:
read_gnomekderc("/etc/skels/.kderc", 'KDE')
modifies GNOME-like and KDE-like config files (aka windows-like). If the category doesn't exist, it creates it. eg:
update_gnomekderc("/etc/skels/.kderc", 'KDE', kfmIconStyle => "Large")
return the list of process ids matching the regexp
is ARCH1 compatible with ARCH2?
better_arch('i386', 'ia64') and better_arch('ia64', 'i386') are false
better_arch('k7', 'k6') is true and better_arch('k6', 'k7') is false
test the architecture compatibility. eg:
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
returns the first value. first(XXX) is an alternative for ((XXX)[0])
first(XXX)
((XXX)[0])
returns the second value. second(XXX) is an alternative for ((XXX)[1])
second(XXX)
((XXX)[1])
returns the last value. top(@l) is an alternative for $l[$#l]
top(@l)
$l[$#l]
returns a value in { 0, 1 }
extracts the number from the string. You could use directly int "11 foo", but you'll get Argument "11 foo" isn't numeric in int. It also handles returns 11 for "foo 11 bar"
int "11 foo"
"foo 11 bar"
extract a decimal number from the string
returns a value in { "true", "false" }
returns a value in { "yes", "no" }
inverse of bool2text and bool2yesno
bool2text
bool2yesno
non-mutable version of chomp: do not modify the argument, returns the chomp'ed value. Also works on lists: chomp_($a, $b) is equivalent to chomp($a) ; chomp($b) ; ($a,$b)
chomp_($a, $b)
chomp($a) ; chomp($b) ; ($a,$b)
returns a string describing the backtrace. eg:
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
another way to die with a nice error message and a backtrace
die
use this to ensure nobody uses the return value of the function. eg:
sub g { print "g called\n"; noreturn } sub f { print "g returns ", g() } f();
test.pl:3: main::f() expects a value from main::g(), but main::g() doesn't return any value
Copyright (c) 2001-2005 Mandriva <pixel@mandriva.com>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install MDK::Common, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MDK::Common
CPAN shell
perl -MCPAN -e shell install MDK::Common
For more information on module installation, please visit the detailed CPAN module installation guide.