File Coverage

blib/lib/Acme/Machi.pm
Criterion Covered Total %
statement 71 71 100.0
branch 23 30 76.6
condition 5 8 62.5
subroutine 20 20 100.0
pod 8 8 100.0
total 127 137 92.7


line stmt bran cond sub pod time code
1             package Acme::Machi v1.00.1 {
2            
3 2     2   26286 use v5.16.2;
  2         4  
4 2     2   7 use strict;
  2         2  
  2         29  
5 2     2   6 use warnings;
  2         4  
  2         38  
6 2     2   761 use IO::Dir;
  2         30574  
  2         81  
7 2     2   9 use File::Spec;
  2         3  
  2         23  
8 2     2   6 use File::Basename;
  2         2  
  2         90  
9 2     2   1097 use Data::Dumper;
  2         9710  
  2         95  
10 2     2   9 use Cwd;
  2         1  
  2         81  
11 2     2   7 use Carp;
  2         2  
  2         70  
12              
13             #import CPAN libs
14 2     2   791 use namespace::autoclean;
  2         24098  
  2         6  
15              
16            
17             =head1 NAME
18            
19             Machi - Awesome Machi here!
20              
21             =head1 VERSION
22              
23             Version v1.00.1
24              
25             =cut
26              
27             =head1 SYNOPSIS
28              
29             Quick summary of what the module does.
30             Exactly a little code snippet.
31              
32             use Acme::Machi;
33              
34             my $loli = Acme::Machi->new( $name ); # Give birth to a person; accept an optional argument to set the person's name.
35              
36             $loli->named( $name ); # Name the person. Default name is 'Machi'.
37              
38             $loli->name(); # Get person's name.
39              
40             $loli->have_the_habit_of( $habit ); # Person gets into certain searching habit.
41              
42             $loli->habit(); # Get one's searching habit.
43              
44             $loli->learning( @words ); # Teach the person saying something endearing.
45              
46             $loli->affectionate( $file_handle ); # The person shall randomly tell about what you previously teached her/him to say.
47              
48             $loli->search_file_from( $target, $dir, $RESP ); # Search file/dir from certain spcified directory using BFS or DFS.
49             # The third argument $RESP representing 'Responsible', which means she/he will
50             # stop searching and come back in a moment when finding the target one.
51             # In case $RESP is in zero state or $RESP is set but the target isn't found,
52             # she/he will finally print out the tree-like structure of your file system
53             # before coming back in despair.
54            
55            
56             =head1 METHODS
57              
58             =head2 new
59              
60             Create a Machi-type instance.
61              
62             =cut
63             sub new {
64 6 100   6 1 74289 (ref $_[0]) && croak "Oops! Cannot use instance method to construt an object!";
65 5   100     32 bless {
66             Name => $_[1] // "Machi",
67             Words => ["I am starving!!"], # In general, creatures always know how to express their hunger.
68             SRCH_Habit => 'BFS',
69             }, $_[0];
70             }
71              
72             =head2 named
73              
74             Assign a new value to scalar-type instance variable, 'Name', in the object.
75             Return: value of assignment.
76              
77             =cut
78             sub named {
79 1 50   1 1 6 (ref $_[0]) || croak "Oops! Cannot use class method setting the object!";
80 1         3 $_[0]{Name} = $_[1];
81             }
82              
83             =head2 name
84              
85             Return: person's name.
86              
87             =cut
88             sub name {
89 45     45 1 556 $_[0]{Name};
90             }
91              
92              
93             =head2 have_the_habit_of
94              
95             Assign a new searching habit to scalar-type instance variable, 'SRCH_Habit'.
96             Only strings 'BFS' and 'DFS' are valid, setting the others will be ignored.
97             Return: value of assignment.
98              
99             =cut
100             sub have_the_habit_of {
101 5 50   5 1 907 (ref $_[0]) || croak "Oops! Cannot use class method setting the object!";
102 5 100       25 $_[0]{SRCH_Habit} = $_[1] if($_[1] =~ m/([DB]FS)/);
103             }
104              
105             =head2 habit
106              
107             Return: person's searching habit.
108              
109             =cut
110             sub habit {
111 6     6 1 239 $_[0]{SRCH_Habit};
112             }
113              
114              
115             =head2 learning
116              
117             Append a list of words to array-type instance variable, 'Words', in the object.
118             Return: how many words have she/he learnt.
119              
120             =cut
121             sub learning {
122 1 50   1 1 7 (ref $_[0]) || croak "Oops! Cannot use class method setting the object!";
123 1         1 unshift (@{$_[0]{Words}}, @_[1 .. $#_]);
  1         7  
124             }
125            
126             =head2 affectionate
127              
128             Randomly output one of predefined words to FILE_HANDLE, which default of is STDOUT.
129             Return: 1 if no problems while calling this method.
130              
131             =cut
132             sub affectionate {
133 42 50   42 1 781 (ref $_[0]) || croak "Oops! Cannot call affectionate() using class method!";
134 42         41 my $words_list = $_[0]{Words};
135 42   33     75 ($_[1] // *STDOUT)->print( $_[0]->name(),": ", $words_list->[int(rand($#$words_list))], "\n");
136             }
137            
138            
139             =head2 search_file_from
140              
141             Using BFS or DFS to search the target from certain directory.
142             Return: a two-element list:
143             the first element is boolean value denoting whether the target was found or not.
144             the second element is the result string outputed from the core module, Data::Dumper.
145             You may get to know files distribution even better after printing the string.
146              
147             =cut
148             sub search_file_from {
149 4 50   4 1 450 ref $_[0] || croak "Oops! Cannot ask non-human to search!";
150 4         12 my ($target, $dir, $RESP) = @_[1,2,3];
151 4         61 my $obj = File::Spec->catfile(getcwd, $target);
152 4         35 my $s_dir = File::Spec->catfile(getcwd, $dir);
153             my $push_front_back = ($_[0]->habit() eq 'DFS')?
154             sub {
155 42     42   28 unshift(@{$_[0]}, $_[1]);
  42         80  
156             }
157             : sub {
158 42     42   31 push(@{$_[0]}, $_[1]);
  42         77  
159 4 100       11 };
160              
161 4         5 my $data = {};
162 4         8 my @queue = ( [$s_dir, $data] );
163 4         5 my ($elm, $np, $nd, $key, $found);
164 4 50       7 ($obj eq $s_dir) && ($found = 1);
165 4 100       15 return 1 if $RESP;
166 2         6 while($elm = shift @queue){;
167 86         511 ($np, $nd) = @$elm;
168 86         1822 $key = basename($np);
169 86 100 66     1241 $nd->{$key} = (-l $np or -f _)? undef : +{};
170 86 100       202 if (ref $nd->{$key}) {;
171 26         115 my $dh = IO::Dir->new("$np");
172 26         950 my $npp;
173 26         50 foreach ($dh->read()) {;
174 136         902 $npp = File::Spec->catfile($np,$_);
175 136 100       226 ($obj eq $npp) && ($found = 1);
176 136 50       160 return 1 if $RESP;
177 136 100       401 (m/\A\.{1,2}?\z/aa) || $push_front_back->(\@queue, [ $npp, $nd->{$key} ]);
178             }
179             }
180             }
181 2         28 print Data::Dumper->Dump([\$data],[qw% *data %]);
182 2         366 $found;
183             }
184            
185              
186             =head1 AUTHOR
187              
188             Machi Amayadori, C<< >>
189              
190             =head1 BUGS
191              
192             Please report any bugs or feature requests to C, or through the web interface at
193             L.
194             I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
195              
196              
197              
198             =head1 SUPPORT
199              
200             You can find documentation for this module with the perldoc command.
201              
202             perldoc Acme::Machi
203              
204              
205             You can also look for information at:
206              
207             =over 4
208              
209             =item * RT: CPAN's request tracker (report bugs here)
210              
211             L
212              
213             =item * AnnoCPAN: Annotated CPAN documentation
214              
215             L
216              
217             =item * CPAN Ratings
218              
219             L
220              
221             =item * Search CPAN
222              
223             L
224              
225             =back
226              
227              
228             =head1 ACKNOWLEDGEMENTS
229              
230              
231             =head1 LICENSE AND COPYRIGHT
232              
233             Copyright 2016 Machi Amayadori.
234              
235             This program is free software; you can redistribute it and/or modify it
236             under the terms of the the Artistic License (2.0). You may obtain a
237             copy of the full license at:
238              
239             L
240              
241             Any use, modification, and distribution of the Standard or Modified
242             Versions is governed by this Artistic License. By using, modifying or
243             distributing the Package, you accept this license. Do not use, modify,
244             or distribute the Package, if you do not accept this license.
245              
246             If your Modified Version has been derived from a Modified Version made
247             by someone other than you, you are nevertheless required to ensure that
248             your Modified Version complies with the requirements of this license.
249              
250             This license does not grant you the right to use any trademark, service
251             mark, tradename, or logo of the Copyright Holder.
252              
253             This license includes the non-exclusive, worldwide, free-of-charge
254             patent license to make, have made, use, offer to sell, sell, import and
255             otherwise transfer the Package with respect to any patent claims
256             licensable by the Copyright Holder that are necessarily infringed by the
257             Package. If you institute patent litigation (including a cross-claim or
258             counterclaim) against any party alleging that the Package constitutes
259             direct or contributory patent infringement, then this Artistic License
260             to you shall terminate on the date that such litigation is filed.
261              
262             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
263             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
264             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
265             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
266             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
267             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
268             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
269             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
270              
271              
272              
273             =head1 INSTALLATION
274              
275             To install this module, run the following commands:
276              
277             perl Build.PL
278             ./Build
279             ./Build test
280             ./Build install
281              
282              
283             After installing, you can find documentation for this module with the
284             perldoc command.
285              
286             perldoc Acme::Machi
287              
288             =cut
289             }
290             'END_MACHI'; # End of Acme::Machi