blib/lib/IO/File/Cycle.pm

filter: Statementstmt 94.1% 32 / 34 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 8 / 9 Podpod 100% 5 / 5 totaltotal 92.3% 48 / 52 CC 3 SCAR 3.8
CC
3
Cov
92%
open
2.0
cycle
2.0
filename
2.0
CRAP 1.5
↑ Summary
1 package IO::File::Cycle;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use base 'IO::File';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 IO::File::Cycle - Easily split output file while writing
11  
12 =head1 VERSION
13  
14 Version 0.03
15  
16 =cut
17  
18 2 our $VERSION = '0.03';
19  
20 =head1 SYNOPSIS
21  
22 This module makes it easy to split a file while writing to it. This, for example, will create a number of files in /tmp called "foo.1.txt", "foo.2.txt", &c., all at most 10,000 bytes in size:
23  
24 use IO::File::Cycle;
25  
26 my $file = IO::File::Cycle->new('>/tmp/foo.txt');
27 for ( 1..100_000 ) {
28 $file->cycle if tell($file) + length($_) + 1 > 10_000;
29 print $file $_, "\n";
30 }
31 $file->close;
32  
33 =head1 SUBROUTINES/METHODS
34  
35 =head2 start_at()
36  
37 Defines the starting number, which can be easily overridden in a subclass.
38  
39 =cut
40  
41 5 ▶sub start_at { 1 }
Subroutine start_at: called CC=1 SCAR=0.0
Pod documented
42  
43 =head2 open()
44  
45 Sets up some internal variables, then calls IO::File::open().
46  
47 =cut
48  
49 1 ▶sub open {
Subroutine open: called CC=2 SCAR=6.9
Pod documented
50 1 my $io = shift;
51 1 my $filename = shift;
52 1 my @mode_and_perms = @_;
53 1 ▶ if ( $filename =~ s/^(\+?(<|>|>>))// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ s/^(\+?(<|>|>>))//) 1 0 1
54 1 $mode_and_perms[0] = $1;
55 }
56 1 (my $base = $filename) =~ s/\.([^.]+)$//;
57 1 *{$io} = \ {
58 count => $io->start_at,
59 current => $filename,
60 base => $base,
61 extension => $1,
62 mode_and_perms => \@mode_and_perms,
63 };
64 1 $io->SUPER::open($filename, @mode_and_perms);
65 }
66  
67 =head2 format_filename()
68  
69 Formats the filename.
70  
71 =cut
72  
73 5 ▶sub format_filename {
Subroutine format_filename: called CC=1 SCAR=0.0
Pod documented
74 5 my $io = shift;
75 5 return join '.', grep $_, $$$io->{base}, shift, $$$io->{extension};
76 }
77  
78 =head2 cycle()
79  
80 Closes the current file, then opens a new file with an incremented number in the filename (before the extension if there is one, and after a "."). After closing the initial file, it renames it to have the index "1" – for example, "filename.1.ext".
81  
82 =cut
83  
84 4 ▶sub cycle {
Subroutine cycle: called CC=2 SCAR=6.9
Pod documented
85 4 my $io = shift;
86 4 $io->close;
87 4 ▶ if ( $$$io->{count} == (my $start_at = $io->start_at) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$$io->{"count"} == (my $start_at = $io->start_at)) 1 3 4
88 1 rename $$$io->{current}, $io->format_filename( $start_at );
89 }
90 4 $$$io->{current} = $io->format_filename( ++$$$io->{count} );
91 4 $io->SUPER::open($$$io->{current}, @{$$$io->{mode_and_perms}});
92 }
93  
94 =head2 filename()
95  
96 Returns the current file's name. This can be called from a close() method in a subclass to post-process each file.
97  
98 =cut
99  
100 0 ▶sub filename {
Subroutine filename: not called CC=1 SCAR=6.9
Pod documented
101 0 my $io = shift;
102 0 return $$$io->{current};
103 }
104  
105 =head2 close()
106  
107 This is a sample close() method, which in a subclass could post-process each file.
108  
109 sub close {
110 my $io = shift;
111 my $filename = $io->filename;
112 $io->SUPER::close;
113 system "gzip -f $filename";
114 }
115  
116 =cut
117  
118 =head1 AUTHOR
119  
120 Nic Wolff, C<< <nic at angel.net> >>
121  
122 =head1 BUGS
123  
124 Please report any bugs or feature requests to C<bug-io-file-cycle at rt.cpan.org>, or through
125 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IO-File-Cycle>. I will be notified, and then you'll
126 automatically be notified of progress on your bug as I make changes.
127  
128  
129  
130  
131 =head1 SUPPORT
132  
133 You can find documentation for this module with the perldoc command.
134  
135 perldoc IO::File::Cycle
136  
137  
138 You can also look for information at:
139  
140 =over 4
141  
142 =item * RT: CPAN's request tracker (report bugs here)
143  
144 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=IO-File-Cycle>
145  
146 =item * AnnoCPAN: Annotated CPAN documentation
147  
148 L<http://annocpan.org/dist/IO-File-Cycle>
149  
150 =item * CPAN Ratings
151  
152 L<http://cpanratings.perl.org/d/IO-File-Cycle>
153  
154 =item * Search CPAN
155  
156 L<http://search.cpan.org/dist/IO-File-Cycle/>
157  
158 =back
159  
160  
161 =head1 ACKNOWLEDGEMENTS
162  
163  
164 =head1 LICENSE AND COPYRIGHT
165  
166 Copyright 2012 Nic Wolff.
167  
168 This program is free software; you can redistribute it and/or modify it
169 under the terms of either: the GNU General Public License as published
170 by the Free Software Foundation; or the Artistic License.
171  
172 See http://dev.perl.org/licenses/ for more information.
173  
174  
175 =cut
176  
177 2 1; # End of IO::File::Cycle
↑ Summary