| 1 |
package Games::FrozenBubble::MDKCommon; |
| 2 |
use 5.006; |
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
our @ISA = 'Exporter'; |
| 7 |
our @EXPORT = qw{ |
| 8 |
$PI cat_ member difference2 any even odd sqr to_bool to_int if_ |
| 9 |
fold_left output append_to_file min max backtrace basename cp_af all |
| 10 |
}; |
| 11 |
|
| 12 |
our $PI = 3.1415926535897932384626433832795028841972; |
| 13 |
|
| 14 |
sub cat_ { |
| 15 |
local *F; |
| 16 |
open F, $_[0] or return; |
| 17 |
my @l = <F>; |
| 18 |
wantarray ? @l : join '', @l; |
| 19 |
} |
| 20 |
|
| 21 |
sub member { |
| 22 |
my $e = shift; |
| 23 |
foreach (@_) { $e eq $_ and return 1 } |
| 24 |
0; |
| 25 |
} |
| 26 |
|
| 27 |
sub difference2 { |
| 28 |
my %l; |
| 29 |
@l{ @{ $_[1] } } = (); |
| 30 |
grep { !exists $l{$_} } @{ $_[0] }; |
| 31 |
} |
| 32 |
|
| 33 |
sub any(&@) { |
| 34 |
my $f = shift; |
| 35 |
$f->($_) and return 1 foreach @_; |
| 36 |
0; |
| 37 |
} |
| 38 |
|
| 39 |
sub even { $_[0] % 2 == 0 } |
| 40 |
|
| 41 |
sub odd { $_[0] % 2 == 1 } |
| 42 |
|
| 43 |
sub sqr { $_[0] * $_[0] } |
| 44 |
|
| 45 |
sub to_bool { $_[0] ? 1 : 0 } |
| 46 |
|
| 47 |
sub to_int { $_[0] =~ /(\d*)/; $1 } |
| 48 |
|
| 49 |
sub if_($@) { |
| 50 |
my $b = shift; |
| 51 |
$b or return (); |
| 52 |
wantarray || @_ <= 1 |
| 53 |
or die( "if_ called in scalar context with more than one argument " |
| 54 |
. join( ":", caller() ) ); |
| 55 |
wantarray ? @_ : $_[0]; |
| 56 |
} |
| 57 |
|
| 58 |
sub fold_left(&@) { |
| 59 |
my ( $f, $initial, @l ) = @_; |
| 60 |
local ( $::a, $::b ); |
| 61 |
$::a = $initial; |
| 62 |
foreach $::b (@l) { $::a = &$f() } |
| 63 |
$::a; |
| 64 |
} |
| 65 |
|
| 66 |
sub output { |
| 67 |
my $f = shift; |
| 68 |
local *F; |
| 69 |
open F, ">$f" or die "output in file $f failed: $!\n"; |
| 70 |
print F foreach @_; |
| 71 |
} |
| 72 |
|
| 73 |
sub append_to_file { |
| 74 |
my $f = shift; |
| 75 |
local *F; |
| 76 |
open F, ">>$f" or die "output in file $f failed: $!\n"; |
| 77 |
print F foreach @_; |
| 78 |
1; |
| 79 |
} |
| 80 |
|
| 81 |
sub min { my $n = shift; $_ < $n and $n = $_ foreach @_; $n } |
| 82 |
|
| 83 |
sub max { my $n = shift; $_ > $n and $n = $_ foreach @_; $n } |
| 84 |
|
| 85 |
sub backtrace { |
| 86 |
my $s; |
| 87 |
for ( my $i = 1 ; caller($i) ; $i++ ) { |
| 88 |
my ( $package, $file, $line, $func ) = caller($i); |
| 89 |
$s .= "$func() called from $file:$line\n"; |
| 90 |
} |
| 91 |
$s; |
| 92 |
} |
| 93 |
|
| 94 |
sub basename { local $_ = shift; s|/*\s*$||; s|.*/||; $_ } |
| 95 |
|
| 96 |
sub cp_af { |
| 97 |
my $dest = pop @_; |
| 98 |
|
| 99 |
@_ or return; |
| 100 |
@_ == 1 || -d $dest |
| 101 |
or die |
| 102 |
"cp: copying multiple files, but last argument ($dest) is not a directory\n"; |
| 103 |
|
| 104 |
foreach my $src (@_) { |
| 105 |
my $dest = $dest; |
| 106 |
-d $dest and $dest .= '/' . basename($src); |
| 107 |
|
| 108 |
unlink $dest; |
| 109 |
|
| 110 |
if ( -d $src ) { |
| 111 |
-d $dest |
| 112 |
or mkdir $dest, ( stat($src) )[2] |
| 113 |
or die "mkdir: can't create directory $dest: $!\n"; |
| 114 |
cp_af( glob_($src), $dest ); |
| 115 |
} |
| 116 |
elsif ( -l $src ) { |
| 117 |
unless ( |
| 118 |
symlink( |
| 119 |
( readlink($src) || die "readlink failed: $!" ), $dest |
| 120 |
) |
| 121 |
) |
| 122 |
{ |
| 123 |
warn "symlink: can't create symlink $dest: $!\n"; |
| 124 |
} |
| 125 |
} |
| 126 |
else { |
| 127 |
local *F; |
| 128 |
open F, $src or die "can't open $src for reading: $!\n"; |
| 129 |
local *G; |
| 130 |
open G, "> $dest"; |
| 131 |
local $_; |
| 132 |
while (<F>) { print G $_ } |
| 133 |
chmod( ( stat($src) )[2], $dest ); |
| 134 |
} |
| 135 |
} |
| 136 |
1; |
| 137 |
} |
| 138 |
|
| 139 |
sub all { |
| 140 |
my $d = shift; |
| 141 |
|
| 142 |
local *F; |
| 143 |
opendir F, $d or return; |
| 144 |
my @l = grep { $_ ne '.' && $_ ne '..' } readdir F; |
| 145 |
closedir F; |
| 146 |
|
| 147 |
@l; |
| 148 |
} |
| 149 |
|
| 150 |
1; |
| 151 |
|
| 152 |
__END__ |
| 153 |
|
| 154 |
=encoding UTF-8 |
| 155 |
|
| 156 |
=head1 DESCRIPTION |
| 157 |
|
| 158 |
This is extracted from MDK::Common, a helper library that |
| 159 |
extends perl capabilities for very common use when programming |
| 160 |
perl, especially with functional style programming (but what |
| 161 |
other style one could decently adopt? ;p). |
| 162 |
|
| 163 |
This extract is provided here because only Mandrake distro |
| 164 |
includes the whole MDK::Common, so you're not obliged to |
| 165 |
install it. |
| 166 |
|
| 167 |
That said, if you're a perl programmer, I strongly advice you |
| 168 |
to have a look at this library and use it, it would |
| 169 |
dramatically increase the efficiency and readability of your |
| 170 |
perl programs. |
| 171 |
|
| 172 |
Go to google and type in "perl-MDK-Common" if interested. |