File Coverage

blib/lib/Acme/Pythonic/Functions.pm
Criterion Covered Total %
statement 14 212 6.6
branch 0 100 0.0
condition 0 21 0.0
subroutine 5 31 16.1
pod 24 26 92.3
total 43 390 11.0


line stmt bran cond sub pod time code
1             package Acme::Pythonic::Functions;
2              
3 1     1   14200 use 5;
  1         3  
4 1     1   3 use warnings;
  1         1  
  1         27  
5 1     1   4 use strict;
  1         5  
  1         20  
6              
7             #######################################################################
8             # Acme::Pythonic::Functions is Copyright (C) 2009-2017, 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   3 use Carp;
  1         0  
  1         63  
26              
27 1     1   3 use Exporter;
  1         6  
  1         1556  
28              
29             our ($VERSION, @ISA, @EXPORT);
30             @ISA = qw(Exporter);
31              
32             $VERSION = 0.39;
33              
34             @EXPORT = qw(append endswith extend has_key insert isdigit isin isdir isfile len listdir lstrip lstrip2 oslistdir osname pyprint 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 pyprint {
63 0 0   0 1   if ($#_ == -1) {
64 0           return;
65             }
66 0 0         if ($#_ == 0) {
67 0           print ("$_[0]\n");
68 0           return;
69             }
70             # Print the array similar to a (2D-)Python-list:
71 0           my @a = @_;
72 0           my $s;
73             my $i;
74 0           $s = "[";
75 0           for $i (0 .. $#a) {
76 0 0         if ($a[$i] =~ /[^0-9-]/) {
77 0           $s .= "\"$a[$i]\"";
78             } else {
79 0           $s .= $a[$i];
80             }
81 0 0         if ($i != $#a) {
82 0           $s .= ", ";
83             } else {
84 0           $s .= "]\n";
85             }
86             }
87 0           print $s;
88             }
89              
90             # String-Functions
91              
92             sub endswith {
93              
94 0     0 1   &checkArgs(2, @_);
95              
96 0 0         if ($_[0] =~ /\Q$_[1]\E$/) {
97 0           return 1;
98             }
99             else {
100 0           return 0;
101             }
102             }
103              
104              
105             sub lstrip {
106              
107 0     0 1   &checkArgs(1, @_);
108              
109 0           my $a = $_[0];
110              
111 0           $a =~ s/^\s+//;
112              
113 0           return $a;
114             }
115              
116              
117             sub isdigit {
118              
119 0     0 1   &checkArgs(1, @_);
120              
121 0 0         if($_[0] =~ /\D/) {
122 0           return 0;
123             }
124             else {
125 0           return 1;
126             }
127             }
128              
129              
130             sub lstrip2 {
131              
132 0     0 1   &checkArgs(2, @_);
133              
134 0           my ($a, $b) = @_;
135              
136 0 0         if($a !~ /^\Q$b\E/) {
137 0           return $a;
138             }
139              
140 0 0         if (length($b) > length($a)) {
141 0           return $a;
142             }
143              
144 0           return substr($a, length($b));
145             }
146              
147              
148             sub replace {
149              
150 0     0 1   my $nrargs = @_;
151              
152 0 0 0       if($nrargs < 3 || $nrargs > 4) {
153 0           croak "Error: Function 'replace()' takes either 3 or 4 arguments ($nrargs given),";
154             }
155              
156 0           my $count = 0;
157              
158 0 0         if($nrargs == 4) {
159 0           $count = pop(@_);
160             }
161              
162 0 0         unless (isdigit($count)) {
163 0           carp "Warning: Argument 4 of function 'replace()' should be a number; assuming 0,";
164 0           $count = 0;
165             }
166              
167 0           my ($all, $old, $new) = @_;
168              
169 0 0         if ($count == 0) {
170 0           $all =~ s/\Q$old\E/$new/g;
171 0           return $all;
172             }
173              
174 0           for (1 .. $count) {
175 0           $all =~ s/\Q$old\E/$new/;
176             }
177              
178 0           return $all;
179             }
180              
181              
182             sub rstrip {
183              
184 0     0 1   &checkArgs(1, @_);
185              
186 0           my $a = $_[0];
187              
188 0           $a =~ s/\s+$//;
189              
190 0           return $a;
191             }
192              
193              
194             sub rstrip2 {
195              
196 0     0 1   &checkArgs(2, @_);
197              
198 0           my ($a, $b) = @_;
199              
200 0 0         if($a !~ /\Q$b\E$/) {
201 0           return $a;
202             }
203              
204 0 0         if (length($b) > length($a)) {
205 0           return $a;
206             }
207              
208 0           return substr($a, 0, length($a) - length($b));
209             }
210              
211              
212             sub startswith {
213              
214 0     0 1   &checkArgs(2, @_);
215              
216 0 0         if ($_[0] =~ /^\Q$_[1]\E/) {
217 0           return 1;
218             }
219             else {
220 0           return 0;
221             }
222             }
223              
224              
225             sub strip {
226              
227 0     0 1   &checkArgs(1, @_);
228              
229 0           my $a = $_[0];
230              
231 0           $a =~ s/^\s+//;
232 0           $a =~ s/\s+$//;
233              
234 0           return $a;
235             }
236              
237             # List-Functions
238              
239             sub append {
240              
241 0 0   0 1   if($#_ < 1) {
242 0           carp "Warning: Not enough arguments for 'append()',";
243             }
244              
245 0           return @_;
246             }
247              
248             sub extend {
249              
250 0 0   0 1   if($#_ < 1) {
251 0           carp "Warning: Not enough arguments for 'extend()',";
252             }
253              
254 0           return @_;
255             }
256              
257              
258             sub insert {
259              
260 0 0   0 1   if($#_ < 1) {
261 0           carp "Warning: Not enough arguments for 'insert()'; nothing inserted,";
262 0           return @_;
263             }
264              
265 0           my $a = pop;
266 0           my $b = pop;
267              
268 0 0         if($b =~ /\D/) {
269 0           carp "Warning: Second argument for 'insert()' must be a number; nothing inserted,";
270 0           return @_;
271             }
272            
273 0           my @c = @_;
274              
275 0 0         if ($b > $#c + 1) {
276 0           carp "Warning: Position for 'insert()' beyond list; nothing inserted,";
277 0           return @_;
278             }
279              
280 0           splice(@c, $b, 0, $a);
281              
282 0           return @c;
283             }
284              
285              
286             sub lenlist {
287 0     0 0   return $#_ + 1;
288             }
289              
290              
291             sub remove {
292              
293 0 0   0 1   if($#_ < 1) {
294 0           carp "Warning: Not enough arguments for 'remove()',";
295 0           return @_;
296             }
297              
298 0           my $a = pop;
299              
300 0           my @b = @_;
301 0           my $i;
302 0           my $x = 0;
303              
304 0           for $i (0 .. $#_) {
305 0 0         if ($_[$i] eq $a) {
306 0           splice(@b, $i, 1);
307 0           $x = 1;
308 0           last;
309             }
310             }
311              
312 0 0         unless($x) {
313 0           carp "Warning: Element to remove not found in list; nothing removed,";
314             }
315              
316 0           return @b;
317             }
318              
319             # Hash-Functions
320              
321             sub has_key {
322              
323 0 0 0 0 1   if($#_ < 2 || $#_ % 2) {
324 0           croak "Error: Unsuitable arguments to 'has_key()',";
325             }
326              
327 0           my $key = pop;
328 0           my %hash = @_;
329              
330 0 0         if (exists $hash{$key}) {
331 0           return 1;
332             }
333             else {
334 0           return 0;
335             }
336             }
337              
338              
339             # Functions for several datatypes
340              
341             sub isin {
342              
343 0     0 1   my $lenarg = @_;
344              
345 0 0         if ($lenarg < 3) {
346 0           croak "Error: 'isin()' takes at least 3 arguments ($lenarg given),";
347             }
348              
349 0           my $mode = pop;
350              
351 0 0 0       if ($mode ne "s" && $mode ne "l" && $mode ne "h") {
      0        
352 0           croak "Error: Last argument to 'isin()' must be 's', 'l' or 'h',";
353             }
354              
355 0 0         if ($mode eq "s") {
356              
357 0 0         if ($lenarg != 3) {
358 0           croak "Error: 'isin()' in mode 's' takes exactly 3 arguments ($lenarg given),";
359             }
360              
361 0 0         if ($_[0] =~ m/\Q$_[1]\E/) {
362 0           return 1;
363             }
364 0           return 0;
365             }
366              
367 0 0         if ($mode eq "l") {
368 0           my $a = pop;
369 0           for (@_) {
370 0 0         if ($_ eq $a) {
371 0           return 1;
372             }
373             }
374 0           return 0;
375             }
376              
377             # Only mode 'h' is left by now.
378              
379 0 0         if ($lenarg % 2) {
380 0           croak "Error: Unsuitable arguments to 'isin()' in 'h'-mode,";
381             }
382              
383 0           my $key = pop;
384 0           my %hash = @_;
385              
386 0 0         if (exists $hash{$key}) {
387 0           return 1;
388             }
389             else {
390 0           return 0;
391             }
392             }
393              
394             sub len {
395              
396 0     0 1   my $lenarg = @_;
397              
398 0 0         if ($lenarg < 2) {
399 0           croak "Error: 'len()' takes at least 2 arguments ($lenarg given),";
400             }
401              
402 0           my $mode = pop;
403              
404 0 0 0       if ($mode ne "s" && $mode ne "l" && $mode ne "h") {
      0        
405 0           croak "Error: Last argument to 'len()' must be 's', 'l' or 'h',";
406             }
407              
408 0 0         if ($mode eq "s") {
409              
410 0 0         if ($lenarg != 2) {
411 0           croak "Error: 'len()' in mode 's' takes exactly 2 arguments ($lenarg given),";
412             }
413              
414 0           return length($_[0]);
415             }
416              
417 0 0         if ($mode eq "l") {
418 0           return $lenarg - 1;
419             }
420              
421             # Only mode 'h' is left by now.
422              
423 0 0         if (! $lenarg % 2) {
424 0           croak "Error: Unsuitable arguments to 'isin()' in 'h'-mode,";
425             }
426              
427 0           return ($lenarg - 1) / 2;
428             }
429              
430              
431             # File-related-Functions
432              
433             sub isdir {
434              
435 0     0 1   &checkArgs(1, @_);
436              
437 0 0         if(-d $_[0]) {
438 0           return 1;
439             }
440             else {
441 0           return 0;
442             }
443             }
444              
445             sub isfile {
446              
447 0     0 1   &checkArgs(1, @_);
448              
449 0 0         if(-f $_[0]) {
450 0           return 1;
451             }
452             else {
453 0           return 0;
454             }
455             }
456              
457             sub readfile {
458              
459 0     0 1   &checkArgs(1, @_);
460              
461 0           my $file = shift;
462              
463 0 0         open(FH, "<$file") or croak "Error reading file '$file',";
464 0           my @a = ; # Gulp !
465 0           close(FH);
466              
467 0           return @a;
468             }
469              
470              
471             sub writefile {
472              
473 0 0   0 1   if($#_ < 1) {
474 0           croak "Error: Function 'writefile()' needs a list to write to a file as an argument,";
475             }
476              
477 0           my ($file, @a) = @_;
478              
479 0 0         open(FH, ">$file") or croak "Error writing to file '$file',";
480              
481 0           print(FH @a);
482              
483 0           close(FH);
484             }
485              
486             sub listdir {
487              
488 0     0 1   &checkArgs(1, @_);
489              
490 0           my $dir = shift;
491 0 0         if (! -d $dir) {
492 0           croak "Error: No such directory: '$dir',";
493             }
494 0           my $filename;
495 0           my @files = ();
496 0 0         opendir (DIR, $dir) || croak "Error opening directory '$dir',";
497 0           while(($filename = readdir(DIR))){
498 0 0 0       if ($filename ne "." && $filename ne "..") {
499 0           push(@files, "$filename");
500             }
501             }
502 0           return @files;
503             }
504              
505             sub oslistdir {
506 0     0 1   return listdir(@_);
507             }
508              
509              
510             # System-related-Functions
511              
512             sub osname {
513              
514 0     0 1   &checkArgs(0);
515              
516 0           return $^O;
517             }
518              
519             1;
520              
521             __END__