File Coverage

blib/lib/Mixin/Linewise/Readers.pm
Criterion Covered Total %
statement 53 53 100.0
branch 22 28 78.5
condition 4 6 66.6
subroutine 13 13 100.0
pod n/a
total 92 100 92.0


line stmt bran cond sub pod time code
1 3     3   206924 use strict;
  3         29  
  3         75  
2 3     3   13 use warnings;
  3         4  
  3         119  
3             package Mixin::Linewise::Readers 0.109;
4             # ABSTRACT: get linewise readers for strings and filenames
5              
6 3     3   56 use 5.008001; # PerlIO
  3         10  
7 3     3   14 use Carp ();
  3         5  
  3         62  
8 3     3   1321 use IO::File;
  3         23612  
  3         293  
9 3     3   1426 use PerlIO::utf8_strict;
  3         1197  
  3         215  
10              
11             use Sub::Exporter -setup => {
12 3         8 exports => { map {; "read_$_" => \"_mk_read_$_" } qw(file string) },
  6         46  
13             groups => {
14             default => [ qw(read_file read_string) ],
15             readers => [ qw(read_file read_string) ],
16             },
17 3     3   1746 };
  3         38085  
18              
19             #pod =head1 SYNOPSIS
20             #pod
21             #pod package Your::Pkg;
22             #pod use Mixin::Linewise::Readers -readers;
23             #pod
24             #pod sub read_handle {
25             #pod my ($self, $handle) = @_;
26             #pod
27             #pod LINE: while (my $line = $handle->getline) {
28             #pod next LINE if $line =~ /^#/;
29             #pod
30             #pod print "non-comment: $line";
31             #pod }
32             #pod }
33             #pod
34             #pod Then:
35             #pod
36             #pod use Your::Pkg;
37             #pod
38             #pod Your::Pkg->read_file($filename);
39             #pod
40             #pod Your::Pkg->read_string($string);
41             #pod
42             #pod Your::Pkg->read_handle($fh);
43             #pod
44             #pod =head1 EXPORTS
45             #pod
46             #pod C and C are exported by default. Either can be
47             #pod requested individually, or renamed. They are generated by
48             #pod L, so consult its documentation for more
49             #pod information.
50             #pod
51             #pod Both can be generated with the option "method" which requests that a method
52             #pod other than "read_handle" is called with the created IO::Handle.
53             #pod
54             #pod If given a "binmode" option, any C type functions will use
55             #pod that as an IO layer, otherwise, the default is C.
56             #pod
57             #pod use Mixin::Linewise::Readers -readers => { binmode => "raw" };
58             #pod use Mixin::Linewise::Readers -readers => { binmode => "encoding(iso-8859-1)" };
59             #pod
60             #pod =head2 read_file
61             #pod
62             #pod Your::Pkg->read_file($filename);
63             #pod Your::Pkg->read_file(\%options, $filename);
64             #pod
65             #pod If generated, the C export attempts to open the named file for
66             #pod reading, and then calls C on the opened handle.
67             #pod
68             #pod An optional hash reference may be passed before C<$filename> with options.
69             #pod The only valid option currently is C, which overrides any
70             #pod default set from C or the built-in C.
71             #pod
72             #pod Any arguments after C<$filename> are passed along after to C.
73             #pod
74             #pod =cut
75              
76             sub _mk_read_file {
77 15     15   2385 my ($self, $name, $arg) = @_;
78              
79 15 100       33 my $method = defined $arg->{method} ? $arg->{method} : 'read_handle';
80 15 100       30 my $dflt_enc = defined $arg->{binmode} ? $arg->{binmode} : 'utf8_strict';
81              
82             sub {
83 6     6   3506 my ($invocant, $options, $filename);
84 6 100       22 if ( ref $_[1] eq 'HASH' ) {
85             # got options before filename
86 1         4 ($invocant, $options, $filename) = splice @_, 0, 3;
87             }
88             else {
89 5         15 ($invocant, $filename) = splice @_, 0, 2;
90             }
91              
92 6 100       20 my $binmode = defined $options->{binmode} ? $options->{binmode} : $dflt_enc;
93 6         30 $binmode =~ s/^://; # we add it later
94              
95             # Check the file
96 6 50       16 Carp::croak "no filename specified" unless $filename;
97 6 50       121 Carp::croak "file '$filename' does not exist" unless -e $filename;
98 6 50       40 Carp::croak "'$filename' is not readable" unless -r _;
99              
100 6 50       52 my $handle = IO::File->new($filename, "<:$binmode")
101             or Carp::croak "couldn't read file '$filename': $!";
102              
103 6         576 $invocant->$method($handle, @_);
104             }
105 15         69 }
106              
107             #pod =head2 read_string
108             #pod
109             #pod Your::Pkg->read_string($string);
110             #pod Your::Pkg->read_string(\%option, $string);
111             #pod
112             #pod If generated, the C creates a handle on the given string, and
113             #pod then calls C on the opened handle. Because handles on strings
114             #pod must be octet-oriented, the string B. It will be opened
115             #pod in the default binmode established by importing. (See L, above.)
116             #pod
117             #pod Any arguments after C<$string> are passed along after to C.
118             #pod
119             #pod Like C, this method can take a leading hashref with one valid
120             #pod argument: C.
121             #pod
122             #pod =cut
123              
124             sub _mk_read_string {
125 15     15   382 my ($self, $name, $arg) = @_;
126              
127 15 100       29 my $method = defined $arg->{method} ? $arg->{method} : 'read_handle';
128 15 100       25 my $dflt_enc = defined $arg->{binmode} ? $arg->{binmode} : 'utf8_strict';
129              
130             sub {
131 5 100 66 5   7812 my ($opt) = @_ > 2 && ref $_[1] ? splice(@_, 1, 1) : undef;
132 5         21 my ($invocant, $string) = splice @_, 0, 2;
133              
134 5 100 66     20 my $binmode = ($opt && $opt->{binmode}) ? $opt->{binmode} : $dflt_enc;
135 5         14 $binmode =~ s/^://; # we add it later
136              
137 5 50       25 Carp::croak "no string provided" unless defined $string;
138              
139 5 50   1   118 open my $handle, "<:$binmode", \$string
  1     1   12  
  1         2  
  1         7  
  1         8  
  1         3  
  1         9  
140             or die "error opening string for reading: $!";
141              
142 5         1811 $invocant->$method($handle, @_);
143             }
144 15         90 }
145              
146             1;
147              
148             __END__