IO/Moose/Seekable.pm untested

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 229 CC 23 SCAR 32.5
CC
23
Cov
0%
seek
42.0
tell
42.0
sysseek
30.0
CRAP 25.7
1 #!/usr/bin/perl -c
2  
3 package IO::Moose::Seekable;
4  
5 =head1 NAME
6  
7 IO::Moose::Seekable - Reimplementation of IO::Seekable with improvements
8  
9 =head1 SYNOPSIS
10  
11 package My::IO;
12 use Moose;
13 extends 'IO::Moose::Handle';
14 with 'IO::Moose::Seekable';
15  
16 package main;
17 my $stdin = My::IO->new( file => \*STDIN, mode => 'r' );
18 print $stdin->slurp;
19 print $stdin->tell, "\n";
20  
21 =head1 DESCRIPTION
22  
23 This class provides an interface mostly compatible with L<IO::Seekable>. The
24 differences:
25  
26 =over
27  
28 =item *
29  
30 It is based on L<Moose> object framework.
31  
32 =item *
33  
34 It uses L<Exception::Base> for signaling errors. Most of methods are throwing
35 exception on failure.
36  
37 =item *
38  
39 It doesn't export any constants. Use L<Fcntl> instead.
40  
41 =back
42  
43 =cut
44  
45  
46 use 5.008;
47 use strict;
48 use warnings FATAL => 'all';
49  
50 our $VERSION = '0.1004';
51  
52 use Moose;
53  
54  
55 =head1 INHERITANCE
56  
57 =over 2
58  
59 =item *
60  
61 extends L<IO::Moose::Handle>
62  
63 =over 2
64  
65 =item *
66  
67 extends L<MooseX::GlobRef::Object>
68  
69 =over 2
70  
71 =item *
72  
73 extends L<Moose::Object>
74  
75 =back
76  
77 =back
78  
79 =item *
80  
81 extends L<IO::Seekable>
82  
83 =over 2
84  
85 =item *
86  
87 extends L<IO::Handle>
88  
89 =back
90  
91 =back
92  
93 =cut
94  
95 extends 'IO::Moose::Handle', 'IO::Seekable';
96  
97  
98 =head1 EXCEPTIONS
99  
100 =over
101  
102 =item L<Exception::Argument>
103  
104 Thrown whether method is called with wrong argument.
105  
106 =item L<Exception::Fatal>
107  
108 Thrown whether fatal error is occurred by core function.
109  
110 =back
111  
112 =cut
113  
114 use Exception::Base (
115 '+ignore_package' => [ __PACKAGE__ ],
116 );
117  
118  
119 use constant::boolean;
120 use English '-no_match_vars';
121  
122 use Scalar::Util 'blessed', 'looks_like_number', 'reftype';
123  
124  
125 # Assertions
126 use Test::Assert ':assert';
127  
128 # Debugging flag
129 use if $ENV{PERL_DEBUG_IO_MOOSE_SEEKABLE}, 'Smart::Comments';
130  
131  
132 use namespace::clean -except => 'meta';
133  
134  
135 ## no critic qw(ProhibitBuiltinHomonyms)
136 ## no critic qw(RequireArgUnpacking)
137 ## no critic qw(RequireCheckingReturnValueOfEval)
138  
139 =head1 METHODS
140  
141 =over
142  
143 =item seek( I<pos> : Int, I<whence> : Int ) : Self
144  
145 Seek the file to position I<pos>, relative to I<whence>:
146  
147 =over
148  
149 =item I<whence>=0 (SEEK_SET)
150  
151 I<pos> is absolute position. (Seek relative to the start of the file)
152  
153 =item I<whence>=1 (SEEK_CUR)
154  
155 I<pos> is an offset from the current position. (Seek relative to current)
156  
157 =item I<whence>=2 (SEEK_END)
158  
159 =back
160  
161 I<pos> is an offset from the end of the file. (Seek relative to end)
162  
163 The SEEK_* constants can be imported from the L<Fcntl> module if you don't
164 wish to use the numbers 0, 1 or 2 in your code. The SEEK_* constants are more
165 portable.
166  
167 Returns self object on success or throws an exception.
168  
169 use Fcntl ':seek';
170 $file->seek(0, SEEK_END);
171 $file->say("*** End of file");
172  
173 =cut
174  
175 sub seek {
176 ### IO::Moose::Seekabe::seek: @_
177  
178 my $self = shift;
179  
180 # handle tie hook
181 $self = $$self if blessed $self and reftype $self eq 'REF';
182  
183 Exception::Argument->throw(
184 message => 'Usage: $io->seek(POS, WHENCE)',
185 ) if not blessed $self or @_ != 2 or not looks_like_number $_[0] or not looks_like_number $_[1];
186  
187 my $status;
188 eval {
189 $status = CORE::seek $self->fh, $_[0], $_[1];
190 };
191 if (not $status) {
192 $self->_set_error(FALSE);
193 my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new;
194 $e->throw( message => 'Cannot seek' );
195 };
196 assert_true($status) if ASSERT;
197  
198 return $self;
199 };
200  
201  
202 =item sysseek( I<pos> : Int, I<whence> : Int ) : Int
203  
204 Uses the system call lseek(2) directly so it can be used with B<sysread> and
205 B<syswrite> methods.
206  
207 Returns the new position or throws an exception.
208  
209 =cut
210  
211 sub sysseek {
212 ### IO::Moose::Seekabe::sysseek: @_
213  
214 my $self = shift;
215  
216 Exception::Argument->throw(
217 message => 'Usage: $io->sysseek(POS, WHENCE)'
218 ) if not blessed $self or @_ != 2 or not looks_like_number $_[0] or not looks_like_number $_[1];
219  
220 my $position;
221 eval {
222 $position = CORE::sysseek $self->fh, $_[0], $_[1];
223 };
224 if (not $position) {
225 $self->_set_error(TRUE);
226 my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new;
227 $e->throw( message => 'Cannot sysseek' );
228 };
229 assert_true($position) if ASSERT;
230  
231 return int $position;
232 };
233  
234  
235 =item tell(I<>) : Int
236  
237 Returns the current file position, or throws an exception on error.
238  
239 =cut
240  
241 sub tell {
242 ### IO::Moose::Seekabe::tell: @_
243  
244 my $self = shift;
245  
246 # handle tie hook
247 $self = $$self if blessed $self and reftype $self eq 'REF';
248  
249 Exception::Argument->throw(
250 message => 'Usage: $io->tell()'
251 ) if not blessed $self or @_ > 0;
252  
253 my $position;
254 eval {
255 $position = CORE::tell $self->fh;
256 };
257 if ($EVAL_ERROR or $position < 0) {
258 $self->_set_error(TRUE);
259 my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new;
260 $e->throw( message => 'Cannot tell' );
261 };
262 assert_not_null($position) if ASSERT;
263  
264 return $position;
265 };
266  
267  
268 =item getpos(I<>) : Int
269  
270 Returns a value that represents the current position of the file. This method
271 is implemented with B<tell> method.
272  
273 =cut
274  
275 sub getpos {
276 ### IO::Moose::Seekabe::getpos: @_
277  
278 my $self = shift;
279  
280 Exception::Argument->throw(
281 message => 'Usage: $io->getpos()'
282 ) if not blessed $self or @_ > 0;
283  
284 my $position;
285 eval {
286 $position = $self->tell;
287 };
288 if ($EVAL_ERROR or $position < 0) {
289 $self->_set_error(TRUE);
290 my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new;
291 $e->throw( message => 'Cannot tell' );
292 };
293 assert_not_null($position) if ASSERT;
294  
295 return $position;
296 };
297  
298  
299 =item setpos( I<pos> : Int ) : Self
300  
301 Goes to the position stored previously with B<getpos> method. Returns this
302 object on success, throws an exception on failure. This method is implemented
303 with B<seek> method.
304  
305 $pos = $file->getpos;
306 $file->print("something\n");
307 $file->setpos($pos);
308 print $file->readline; # prints "something"
309  
310 =cut
311  
312 sub setpos {
313 # IO::Moose::Seekabe::setpos: @_
314  
315 my $self = shift;
316  
317 Exception::Argument->throw(
318 message => 'Usage: $io->setpos(POS)'
319 ) if not blessed $self or @_ != 1 or not looks_like_number $_[0];
320  
321 my ($pos) = @_;
322  
323 my $status;
324 eval {
325 # Fcntl::SEEK_SET is 0
326 $status = $self->seek( $pos, 0 );
327 };
328 if (not $status) {
329 $self->_set_error(TRUE);
330 my $e = $EVAL_ERROR ? Exception::Fatal->catch : Exception::IO->new;
331 $e->throw( message => 'Cannot setpos' );
332 };
333 assert_true($status) if ASSERT;
334  
335 return $self;
336 };
337  
338  
339 # Aliasing tie hooks to real functions
340 {
341 foreach my $func (qw{ tell seek }) {
342 __PACKAGE__->meta->add_method(
343 uc($func) => __PACKAGE__->meta->get_method($func)
344 );
345 };
346 };
347  
348  
349 1;
350  
351  
352 =back
353  
354 =begin umlwiki
355  
356 = Class Diagram =
357  
358 [ IO::Moose::Seekable
359 -----------------------------------------------
360 +seek( pos : Int, whence : Int ) : Self
361 +sysseek( pos : Int, whence : Int ) : Int
362 +tell() : Int
363 +getpos() : Int
364 +setpos( pos : Int ) : Self
365 -----------------------------------------------
366 ]
367  
368 [IO::Moose::Seekable] ---|> [IO::Moose::Handle] [IO::Seekable]
369  
370 [IO::Moose::Seekable] ---> <<exception>> [Exception::Fatal] [Exception::IO]
371  
372 =end umlwiki
373  
374 =head1 SEE ALSO
375  
376 L<IO::Seekable>, L<IO::Moose>, L<IO::Moose::Handle>.
377  
378 =head1 BUGS
379  
380 The API is not stable yet and can be changed in future.
381  
382 =head1 AUTHOR
383  
384 Piotr Roszatycki <dexter@cpan.org>
385  
386 =head1 LICENSE
387  
388 Copyright 2008, 2009 by Piotr Roszatycki <dexter@cpan.org>.
389  
390 This program is free software; you can redistribute it and/or modify it
391 under the same terms as Perl itself.
392  
393 See L<http://www.perl.com/perl/misc/Artistic.html>