File Coverage

lib/Egg/Plugin/File/Rotate.pm
Criterion Covered Total %
statement 9 37 24.3
branch 0 18 0.0
condition 0 12 0.0
subroutine 3 7 42.8
pod 2 2 100.0
total 14 76 18.4


line stmt bran cond sub pod time code
1             package Egg::Plugin::File::Rotate;
2             #
3             # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
4             #
5             # $Id: Rotate.pm 337 2008-05-14 12:30:09Z lushe $
6             #
7 1     1   4 use strict;
  1         2  
  1         24  
8 1     1   5 use warnings;
  1         2  
  1         20  
9 1     1   4 use Carp qw/croak/;
  1         2  
  1         630  
10              
11             our $VERSION = '3.00';
12              
13             sub rotate {
14 0     0 1   my $e = shift;
15 0   0       my $base = shift || croak q{ I want base filepath. };
16 0   0       my $report= $e->{rotate_report} ||= [];
17 0 0         my $attr = ref($_[0]) eq 'HASH' ? $_[0]: {@_};
18 0   0       my $stock = $attr->{stock} || 5;
19 0 0         $stock< 3 and $stock= 3;
20             my($renamecode, @loop)= $attr->{reverse} ? do {
21             ( sub {
22 0 0   0     -e "$base$_[0]" || return 0;
23 0           rename("$base$_[0]", "$base$_[1]");
24 0           push @$report, " + rename : $base$_[0] -> $base$_[1]";
25 0           }, 1..$stock );
26 0 0         }: do {
27             -e $base || return
28 0 0         do { push @$report, "'$base' is not found."; (undef) };
  0            
  0            
29             ( sub {
30 0 0   0     -e "$base$_[1]" || return 0;
31 0           rename("$base$_[1]", "$base$_[0]");
32 0           push @$report, " + rename : $base$_[1] -> $base$_[0]";
33 0           }, reverse(1..$stock) );
34             };
35 0           for my $num (@loop) {
36 0           my $old_num= $num- 1;
37 0 0         $renamecode->(".$num", ( $old_num< 1 ? "": ".$old_num" ));
38             }
39 0           return 1;
40             }
41             sub rotate_report {
42 0     0 1   my $e= shift;
43 0 0 0       if (@_ and ! $_[0]) {
44 0           delete($e->{rotate_report});
45 0           return 0;
46             } else {
47 0   0       my $report= $e->{rotate_report} || return (undef);
48 0 0         return wantarray ? @$report: join("\n", @$report);
49             }
50             }
51              
52             1;
53              
54             __END__
55              
56             =head1 NAME
57              
58             Egg::Plugin::File::Rotate - Plugin that does file rotation.
59              
60             =head1 SYNOPSIS
61              
62             use Egg qw/ File::Rotate /;
63            
64            
65             my $file_path= '/path/to/savefile';
66            
67             if ( -e $file_path ) {
68             $e->rotate($file_path, stock => 5 );
69             }
70             my $fh= FileHandle->new("> $file_path") || return do {
71             $e->rotate($file_path, reverse => 1 );
72             die $!;
73             };
74            
75             % ls -la /path/to
76             drwxr-x--- *** .
77             drwxr-x--- *** ..
78             drw-r--r-- *** savefile
79             drw-r--r-- *** savefile.1
80              
81             =head1 DESCRIPTION
82              
83             It numbers and the backup is left for the file that already exists.
84              
85             =head1 METHODS
86              
87             =head2 rotate ([FILE_PATH], [OPTION])
88              
89             It file rotates.
90              
91             Passing to the object file is specified for FILE_PATH. If the file doesn't exist,
92             undefined is returned without doing anything.
93              
94             OPTION is HASH.
95              
96             If reverse of OPTION is undefined, it file usually rotates. At this time,
97             the rotation file of the number specified for stock is left. The file that leaks
98             from the number of stock is annulled. The defaults of the number of stock are 5,
99             and the lowest value is 3.
100              
101             $e->rotate( '/path/to/save.txt', stock=> 10 );
102              
103             FILE_PATH is renamed and doesn't exist after it processes it.
104              
105             When reverse of OPTION is defined, processing opposite to a usual file rotation
106             is done. After usual roteate, this is an option to want to return it.
107              
108             $e->rotate( ...... );
109             my $fh= FileHandle->new("/path/to/save.txt") || do {
110             $e->rotate( "/path/to/save.txt", reverse=> 1 );
111             die $!;
112             };
113              
114             =head2 rotate_report
115              
116             The report of the processing situation of the rotate method is returned.
117              
118             $e->rotate( ...... );
119             .......
120             ....
121             print $e->rotate_report;
122              
123             =head1 SEE ALSO
124              
125             L<Egg::Release>,
126              
127             =head1 AUTHOR
128              
129             Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
130              
131             =head1 COPYRIGHT AND LICENSE
132              
133             Copyright (C) 2008 Bee Flag, Corp. E<lt>L<http://egg.bomcity.com/>E<gt>.
134              
135             This library is free software; you can redistribute it and/or modify
136             it under the same terms as Perl itself, either Perl version 5.8.6 or,
137             at your option, any later version of Perl 5 you may have available.
138              
139             =cut
140