File Coverage

blib/lib/Acme/Pythonic/Functions.pm
Criterion Covered Total %
statement 15 201 7.4
branch 0 94 0.0
condition 0 21 0.0
subroutine 5 31 16.1
pod 24 26 92.3
total 44 373 11.8


line stmt bran cond sub pod time code
1             package Acme::Pythonic::Functions;
2              
3 1     1   22781 use 5;
  1         4  
  1         41  
4 1     1   5 use warnings;
  1         1  
  1         26  
5 1     1   4 use strict;
  1         11  
  1         34  
6              
7             #######################################################################
8             # Acme::Pythonic::Functions is Copyright (C) 2009-2012, Hauke Lubenow.
9             #
10             # This module is free software; you can redistribute it and/or modify it
11             # under the same terms as Perl 5.14.2.
12             # For more details, see the full text of the licenses in the directory
13             # LICENSES. The full text of the licenses can also be found in the
14             # documents 'perldoc perlgpl' and 'perldoc perlartistic' of the official
15             # Perl 5.14.2-distribution. In case of any contradictions, these
16             # 'perldoc'-texts are decisive.
17             #
18             # THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
19             # WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
20             # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
21             # FOR MORE DETAILS, SEE THE FULL TEXTS OF THE LICENSES IN THE DIRECTORY
22             # LICENSES AND IN THE 'PERLDOC'-TEXTS MENTIONED ABOVE.
23             #######################################################################
24              
25 1     1   5 use Carp;
  1         1  
  1         90  
26              
27 1     1   6 use Exporter;
  1         3  
  1         2258  
28              
29             our ($VERSION, @ISA, @EXPORT);
30             @ISA = qw(Exporter);
31              
32             $VERSION = 0.38;
33              
34             @EXPORT = qw(append endswith extend has_key insert isdigit isin isdir isfile len listdir lstrip lstrip2 oslistdir osname pront readfile remove replace rstrip rstrip2 startswith strip writefile);
35              
36             # Internal Functions
37              
38             sub checkArgs {
39              
40 0     0 0   my ($nr, @vals) = @_;
41              
42 0           my $lenvals = @vals;
43              
44 0 0         if($lenvals != $nr) {
45              
46 0           my $name = (caller 1)[3];
47 0           my @temp = split("::", $name);
48 0           $name = pop(@temp) . "()";
49              
50 0           my $arg = "arguments";
51              
52 0 0         if($nr == 1) {
53 0           $arg = "argument";
54             }
55              
56 0           croak "Error: Function '$name' takes exactly $nr $arg ($lenvals given),";
57             }
58             }
59              
60             # print-Replacement-Function
61              
62             sub pront {
63 0 0   0 1   if ($#_ == -1) {
64 0           @_ = ("");
65             }
66 0           for (@_) {
67 0           print"$_\n"
68             }
69             }
70              
71             # String-Functions
72              
73             sub endswith {
74              
75 0     0 1   &checkArgs(2, @_);
76              
77 0 0         if ($_[0] =~ /\Q$_[1]\E$/) {
78 0           return 1;
79             }
80             else {
81 0           return 0;
82             }
83             }
84              
85              
86             sub lstrip {
87              
88 0     0 1   &checkArgs(1, @_);
89              
90 0           my $a = $_[0];
91              
92 0           $a =~ s/^\s+//;
93              
94 0           return $a;
95             }
96              
97              
98             sub isdigit {
99              
100 0     0 1   &checkArgs(1, @_);
101              
102 0 0         if($_[0] =~ /\D/) {
103 0           return 0;
104             }
105             else {
106 0           return 1;
107             }
108             }
109              
110              
111             sub lstrip2 {
112              
113 0     0 1   &checkArgs(2, @_);
114              
115 0           my ($a, $b) = @_;
116              
117 0 0         if($a !~ /^\Q$b\E/) {
118 0           return $a;
119             }
120              
121 0 0         if (length($b) > length($a)) {
122 0           return $a;
123             }
124              
125 0           return substr($a, length($b));
126             }
127              
128              
129             sub replace {
130              
131 0     0 1   my $nrargs = @_;
132              
133 0 0 0       if($nrargs < 3 || $nrargs > 4) {
134 0           croak "Error: Function 'replace()' takes either 3 or 4 arguments ($nrargs given),";
135             }
136              
137 0           my $count = 0;
138              
139 0 0         if($nrargs == 4) {
140 0           $count = pop(@_);
141             }
142              
143 0 0         unless (isdigit($count)) {
144 0           carp "Warning: Argument 4 of function 'replace()' should be a number; assuming 0,";
145 0           $count = 0;
146             }
147              
148 0           my ($all, $old, $new) = @_;
149              
150 0 0         if ($count == 0) {
151 0           $all =~ s/\Q$old\E/$new/g;
152 0           return $all;
153             }
154              
155 0           for (1 .. $count) {
156 0           $all =~ s/\Q$old\E/$new/;
157             }
158              
159 0           return $all;
160             }
161              
162              
163             sub rstrip {
164              
165 0     0 1   &checkArgs(1, @_);
166              
167 0           my $a = $_[0];
168              
169 0           $a =~ s/\s+$//;
170              
171 0           return $a;
172             }
173              
174              
175             sub rstrip2 {
176              
177 0     0 1   &checkArgs(2, @_);
178              
179 0           my ($a, $b) = @_;
180              
181 0 0         if($a !~ /\Q$b\E$/) {
182 0           return $a;
183             }
184              
185 0 0         if (length($b) > length($a)) {
186 0           return $a;
187             }
188              
189 0           return substr($a, 0, length($a) - length($b));
190             }
191              
192              
193             sub startswith {
194              
195 0     0 1   &checkArgs(2, @_);
196              
197 0 0         if ($_[0] =~ /^\Q$_[1]\E/) {
198 0           return 1;
199             }
200             else {
201 0           return 0;
202             }
203             }
204              
205              
206             sub strip {
207              
208 0     0 1   &checkArgs(1, @_);
209              
210 0           my $a = $_[0];
211              
212 0           $a =~ s/^\s+//;
213 0           $a =~ s/\s+$//;
214              
215 0           return $a;
216             }
217              
218             # List-Functions
219              
220             sub append {
221              
222 0 0   0 1   if($#_ < 1) {
223 0           carp "Warning: Not enough arguments for 'append()',";
224             }
225              
226 0           return @_;
227             }
228              
229             sub extend {
230              
231 0 0   0 1   if($#_ < 1) {
232 0           carp "Warning: Not enough arguments for 'extend()',";
233             }
234              
235 0           return @_;
236             }
237              
238              
239             sub insert {
240              
241 0 0   0 1   if($#_ < 1) {
242 0           carp "Warning: Not enough arguments for 'insert()'; nothing inserted,";
243 0           return @_;
244             }
245              
246 0           my $a = pop;
247 0           my $b = pop;
248              
249 0 0         if($b =~ /\D/) {
250 0           carp "Warning: Second argument for 'insert()' must be a number; nothing inserted,";
251 0           return @_;
252             }
253            
254 0           my @c = @_;
255              
256 0 0         if ($b > $#c + 1) {
257 0           carp "Warning: Position for 'insert()' beyond list; nothing inserted,";
258 0           return @_;
259             }
260              
261 0           splice(@c, $b, 0, $a);
262              
263 0           return @c;
264             }
265              
266              
267             sub lenlist {
268 0     0 0   return $#_ + 1;
269             }
270              
271              
272             sub remove {
273              
274 0 0   0 1   if($#_ < 1) {
275 0           carp "Warning: Not enough arguments for 'remove()',";
276 0           return @_;
277             }
278              
279 0           my $a = pop;
280              
281 0           my @b = @_;
282 0           my $i;
283 0           my $x = 0;
284              
285 0           for $i (0 .. $#_) {
286 0 0         if ($_[$i] eq $a) {
287 0           splice(@b, $i, 1);
288 0           $x = 1;
289 0           last;
290             }
291             }
292              
293 0 0         unless($x) {
294 0           carp "Warning: Element to remove not found in list; nothing removed,";
295             }
296              
297 0           return @b;
298             }
299              
300             # Hash-Functions
301              
302             sub has_key {
303              
304 0 0 0 0 1   if($#_ < 2 || $#_ % 2) {
305 0           croak "Error: Unsuitable arguments to 'has_key()',";
306             }
307              
308 0           my $key = pop;
309 0           my %hash = @_;
310              
311 0 0         if (exists $hash{$key}) {
312 0           return 1;
313             }
314             else {
315 0           return 0;
316             }
317             }
318              
319              
320             # Functions for several datatypes
321              
322             sub isin {
323              
324 0     0 1   my $lenarg = @_;
325              
326 0 0         if ($lenarg < 3) {
327 0           croak "Error: 'isin()' takes at least 3 arguments ($lenarg given),";
328             }
329              
330 0           my $mode = pop;
331              
332 0 0 0       if ($mode ne "s" && $mode ne "l" && $mode ne "h") {
      0        
333 0           croak "Error: Last argument to 'isin()' must be 's', 'l' or 'h',";
334             }
335              
336 0 0         if ($mode eq "s") {
337              
338 0 0         if ($lenarg != 3) {
339 0           croak "Error: 'isin()' in mode 's' takes exactly 3 arguments ($lenarg given),";
340             }
341              
342 0 0         if ($_[0] =~ m/\Q$_[1]\E/) {
343 0           return 1;
344             }
345 0           return 0;
346             }
347              
348 0 0         if ($mode eq "l") {
349 0           my $a = pop;
350 0           for (@_) {
351 0 0         if ($_ eq $a) {
352 0           return 1;
353             }
354             }
355 0           return 0;
356             }
357              
358             # Only mode 'h' is left by now.
359              
360 0 0         if ($lenarg % 2) {
361 0           croak "Error: Unsuitable arguments to 'isin()' in 'h'-mode,";
362             }
363              
364 0           my $key = pop;
365 0           my %hash = @_;
366              
367 0 0         if (exists $hash{$key}) {
368 0           return 1;
369             }
370             else {
371 0           return 0;
372             }
373             }
374              
375             sub len {
376              
377 0     0 1   my $lenarg = @_;
378              
379 0 0         if ($lenarg < 2) {
380 0           croak "Error: 'len()' takes at least 2 arguments ($lenarg given),";
381             }
382              
383 0           my $mode = pop;
384              
385 0 0 0       if ($mode ne "s" && $mode ne "l" && $mode ne "h") {
      0        
386 0           croak "Error: Last argument to 'len()' must be 's', 'l' or 'h',";
387             }
388              
389 0 0         if ($mode eq "s") {
390              
391 0 0         if ($lenarg != 2) {
392 0           croak "Error: 'len()' in mode 's' takes exactly 2 arguments ($lenarg given),";
393             }
394              
395 0           return length($_[0]);
396             }
397              
398 0 0         if ($mode eq "l") {
399 0           return $lenarg - 1;
400             }
401              
402             # Only mode 'h' is left by now.
403              
404 0 0         if (! $lenarg % 2) {
405 0           croak "Error: Unsuitable arguments to 'isin()' in 'h'-mode,";
406             }
407              
408 0           return ($lenarg - 1) / 2;
409             }
410              
411              
412             # File-related-Functions
413              
414             sub isdir {
415              
416 0     0 1   &checkArgs(1, @_);
417              
418 0 0         if(-d $_[0]) {
419 0           return 1;
420             }
421             else {
422 0           return 0;
423             }
424             }
425              
426             sub isfile {
427              
428 0     0 1   &checkArgs(1, @_);
429              
430 0 0         if(-f $_[0]) {
431 0           return 1;
432             }
433             else {
434 0           return 0;
435             }
436             }
437              
438             sub readfile {
439              
440 0     0 1   &checkArgs(1, @_);
441              
442 0           my $file = shift;
443              
444 0 0         open(FH, "<$file") or croak "Error reading file '$file',";
445 0           my @a = ; # Gulp !
446 0           close(FH);
447              
448 0           return @a;
449             }
450              
451              
452             sub writefile {
453              
454 0 0   0 1   if($#_ < 1) {
455 0           croak "Error: Function 'writefile()' needs a list to write to a file as an argument,";
456             }
457              
458 0           my ($file, @a) = @_;
459              
460 0 0         open(FH, ">$file") or croak "Error writing to file '$file',";
461              
462 0           print(FH @a);
463              
464 0           close(FH);
465             }
466              
467             sub listdir {
468              
469 0     0 1   &checkArgs(1, @_);
470              
471 0           my $dir = shift;
472 0 0         if (! -d $dir) {
473 0           croak "Error: No such directory: '$dir',";
474             }
475 0           my $filename;
476 0           my @files = ();
477 0 0         opendir (DIR, $dir) || croak "Error opening directory '$dir',";
478 0           while(($filename = readdir(DIR))){
479 0 0 0       if ($filename ne "." && $filename ne "..") {
480 0           push(@files, "$filename");
481             }
482             }
483 0           return @files;
484             }
485              
486             sub oslistdir {
487 0     0 1   return listdir(@_);
488             }
489              
490              
491             # System-related-Functions
492              
493             sub osname {
494              
495 0     0 1   &checkArgs(0);
496              
497 0           return $^O;
498             }
499              
500             1;
501              
502             __END__