File Coverage

blib/lib/Mixin/Linewise/Readers.pm
Criterion Covered Total %
statement 53 53 100.0
branch 22 28 78.5
condition 5 9 55.5
subroutine 13 13 100.0
pod n/a
total 93 103 90.2


line stmt bran cond sub pod time code
1 3     3   209609 use strict;
  3         34  
  3         79  
2 3     3   14 use warnings;
  3         7  
  3         134  
3             package Mixin::Linewise::Readers 0.111;
4             # ABSTRACT: get linewise readers for strings and filenames
5              
6 3     3   49 use 5.008001; # PerlIO
  3         10  
7 3     3   16 use Carp ();
  3         6  
  3         53  
8 3     3   1381 use IO::File;
  3         23792  
  3         307  
9 3     3   1311 use PerlIO::utf8_strict;
  3         1131  
  3         214  
10              
11             use Sub::Exporter -setup => {
12 3         9 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   1769 };
  3         39705  
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   2530 my ($self, $name, $arg) = @_;
78              
79 15 100       35 my $method = defined $arg->{method} ? $arg->{method} : 'read_handle';
80 15 100       32 my $dflt_enc = defined $arg->{binmode} ? $arg->{binmode} : 'utf8_strict';
81              
82             sub {
83 6     6   3987 my ($invocant, $options, $filename);
84 6 100       20 if ( ref $_[1] eq 'HASH' ) {
85             # got options before filename
86 1         4 ($invocant, $options, $filename) = splice @_, 0, 3;
87             }
88             else {
89 5         12 ($invocant, $filename) = splice @_, 0, 2;
90             }
91              
92 6 100       21 my $binmode = defined $options->{binmode} ? $options->{binmode} : $dflt_enc;
93 6         29 $binmode =~ s/^://; # we add it later
94              
95             # Check the file
96 6 50       17 Carp::croak "no filename specified" unless $filename;
97 6 50       97 Carp::croak "file '$filename' does not exist" unless -e $filename;
98 6 50 33     56 Carp::croak "'$filename' is not readable" unless -r _ && ! -d _;
99              
100 6 50       65 my $handle = IO::File->new($filename, "<:$binmode")
101             or Carp::croak "couldn't read file '$filename': $!";
102              
103 6         580 $invocant->$method($handle, @_);
104             }
105 15         80 }
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   356 my ($self, $name, $arg) = @_;
126              
127 15 100       38 my $method = defined $arg->{method} ? $arg->{method} : 'read_handle';
128 15 100       27 my $dflt_enc = defined $arg->{binmode} ? $arg->{binmode} : 'utf8_strict';
129              
130             sub {
131 5 100 66 5   9352 my ($opt) = @_ > 2 && ref $_[1] ? splice(@_, 1, 1) : undef;
132 5         17 my ($invocant, $string) = splice @_, 0, 2;
133              
134 5 100 66     18 my $binmode = ($opt && $opt->{binmode}) ? $opt->{binmode} : $dflt_enc;
135 5         12 $binmode =~ s/^://; # we add it later
136              
137 5 50       23 Carp::croak "no string provided" unless defined $string;
138              
139 5 50   1   100 open my $handle, "<:$binmode", \$string
  1     1   12  
  1         2  
  1         7  
  1         6  
  1         3  
  1         4  
140             or die "error opening string for reading: $!";
141              
142 5         1893 $invocant->$method($handle, @_);
143             }
144 15         94 }
145              
146             1;
147              
148             __END__