File Coverage

blib/lib/Acme/ELLEDNERA/Utils.pm
Criterion Covered Total %
statement 24 24 100.0
branch 2 2 100.0
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 34 34 100.0


line stmt bran cond sub pod time code
1             package Acme::ELLEDNERA::Utils;
2            
3 3     3   219015 use 5.006;
  3         30  
4 3     3   16 use strict;
  3         6  
  3         82  
5 3     3   16 use warnings;
  3         6  
  3         185  
6            
7             =head1 NAME
8            
9             Acme::ELLEDNERA::Utils - Done for the sake of learning :)
10            
11             =head1 VERSION
12            
13             Version 0.02
14            
15             =cut
16            
17             our $VERSION = '0.02';
18            
19            
20             =head1 SYNOPSIS
21            
22             This module allows you to do addition and shuffle an array.
23            
24             use Acme::ELLEDNERA::Utils qw( sum shuffle );
25            
26             # or
27            
28             # use Acme::ELLEDNERA::Utils ":all";
29            
30             # addition
31            
32             $sum = sum(1, 2, 3);
33            
34             $sum = sum(1.2 + 3.14159);
35            
36             $sum = sum( qw(t1 10 t2 5 6) ); # will only do 10+5+6 = 21
37            
38             # shuffling an array
39            
40             @ori_nums = (1, 3, 5, 7, 9, 11, 13, 15);
41            
42             @shuffled = shuffle(@ori_nums); # returns a shuffled array
43            
44             =cut
45            
46 3     3   21 use Exporter qw(import);
  3         7  
  3         866  
47             our @EXPORT_OK = qw( sum shuffle );
48             our %EXPORT_TAGS = ( all => [@EXPORT_OK] );
49            
50             =head1 EXPORT
51            
52             None by default
53            
54             =head1 SUBROUTINES/METHODS
55            
56             =head2 sum
57            
58             Obtains the sum of a list of numbers. If no numbers are passed in, it will return an empty list.
59             A mixture of numbers and non-numerics will work too. However, complex and scientific
60             numbers are not supported.
61            
62             =cut
63            
64             sub sum {
65 5     5 1 625 my @valid_nums = grep { /\A-?\d+(?:\.\d+)?\z/ } @_; # 这个是我自己的
  15         76  
66            
67 5         10 my $sum;
68 5         11 for ( @valid_nums ) {
69 11         19 $sum += $_;
70             }
71            
72             $sum
73 5         22 }
74            
75             =head2 shuffle
76            
77             Shuffle a list of anything :) This subroutine uses the Fisher Yates Shuffle algorithm.
78             I just copied and pasted them from L
79            
80             Unlike the original implementation, this subroutine takes in an actual array
81             and returns a new shuffled one. It is the same one as in the of Intermediate
82             Perl(2nd edition)
83            
84             =cut
85            
86             sub shuffle {
87 2     2 1 1326 my @deck = @_;
88 2 100       8 return unless @deck;
89            
90 1         3 my $i = @deck;
91 1         4 while (--$i) {
92 7         55 my $j = int rand ($i+1);
93 7         20 @deck[$i, $j] = @deck[$j, $i];
94             }
95            
96 1         5 @deck;
97             }
98            
99             =head1 AUTHOR
100            
101             Raphael Jun Jie Jong, C<< >>
102            
103             =head1 BUGS
104            
105             Please report any bugs or feature requests to C, or through
106             the web interface at L. I will be notified, and then you'll
107             automatically be notified of progress on your bug as I make changes.
108            
109            
110            
111            
112             =head1 SUPPORT
113            
114             You can find documentation for this module with the perldoc command.
115            
116             perldoc Acme::ELLEDNERA::Utils
117            
118            
119             You can also look for information at:
120            
121             =over 4
122            
123             =item * RT: CPAN's request tracker (report bugs here)
124            
125             L
126            
127             =item * CPAN Ratings
128            
129             L
130            
131             =item * Search CPAN
132            
133             L
134            
135             =back
136            
137            
138             =head1 ACKNOWLEDGEMENTS
139            
140             Besiyata d'shmaya, Intermediate Perl 2nd Edition
141            
142             =head1 LICENSE AND COPYRIGHT
143            
144             This software is Copyright (c) 2021 by Raphael Jun Jie Jong.
145            
146             This is free software, licensed under:
147            
148             The Artistic License 2.0 (GPL Compatible)
149            
150            
151             =cut
152            
153             1; # End of Acme::ELLEDNERA::Utils