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   249713 use strict;
  3         48  
  3         93  
2 3     3   17 use warnings;
  3         6  
  3         159  
3             package Mixin::Linewise::Readers 0.110;
4             # ABSTRACT: get linewise readers for strings and filenames
5              
6 3     3   116 use 5.008001; # PerlIO
  3         12  
7 3     3   19 use Carp ();
  3         6  
  3         80  
8 3     3   1674 use IO::File;
  3         29460  
  3         368  
9 3     3   2008 use PerlIO::utf8_strict;
  3         1632  
  3         251  
10              
11             use Sub::Exporter -setup => {
12 3         12 exports => { map {; "read_$_" => \"_mk_read_$_" } qw(file string) },
  6         62  
13             groups => {
14             default => [ qw(read_file read_string) ],
15             readers => [ qw(read_file read_string) ],
16             },
17 3     3   2098 };
  3         48102  
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   2940 my ($self, $name, $arg) = @_;
78              
79 15 100       40 my $method = defined $arg->{method} ? $arg->{method} : 'read_handle';
80 15 100       37 my $dflt_enc = defined $arg->{binmode} ? $arg->{binmode} : 'utf8_strict';
81              
82             sub {
83 6     6   4510 my ($invocant, $options, $filename);
84 6 100       24 if ( ref $_[1] eq 'HASH' ) {
85             # got options before filename
86 1         4 ($invocant, $options, $filename) = splice @_, 0, 3;
87             }
88             else {
89 5         19 ($invocant, $filename) = splice @_, 0, 2;
90             }
91              
92 6 100       25 my $binmode = defined $options->{binmode} ? $options->{binmode} : $dflt_enc;
93 6         34 $binmode =~ s/^://; # we add it later
94              
95             # Check the file
96 6 50       20 Carp::croak "no filename specified" unless $filename;
97 6 50       140 Carp::croak "file '$filename' does not exist" unless -e $filename;
98 6 50 33     71 Carp::croak "'$filename' is not readable" unless -r _ && ! -d _;
99              
100 6 50       61 my $handle = IO::File->new($filename, "<:$binmode")
101             or Carp::croak "couldn't read file '$filename': $!";
102              
103 6         691 $invocant->$method($handle, @_);
104             }
105 15         88 }
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   465 my ($self, $name, $arg) = @_;
126              
127 15 100       40 my $method = defined $arg->{method} ? $arg->{method} : 'read_handle';
128 15 100       29 my $dflt_enc = defined $arg->{binmode} ? $arg->{binmode} : 'utf8_strict';
129              
130             sub {
131 5 100 66 5   9184 my ($opt) = @_ > 2 && ref $_[1] ? splice(@_, 1, 1) : undef;
132 5         20 my ($invocant, $string) = splice @_, 0, 2;
133              
134 5 100 66     22 my $binmode = ($opt && $opt->{binmode}) ? $opt->{binmode} : $dflt_enc;
135 5         19 $binmode =~ s/^://; # we add it later
136              
137 5 50       25 Carp::croak "no string provided" unless defined $string;
138              
139 5 50   1   132 open my $handle, "<:$binmode", \$string
  1     1   14  
  1         2  
  1         8  
  1         10  
  1         2  
  1         10  
140             or die "error opening string for reading: $!";
141              
142 5         2153 $invocant->$method($handle, @_);
143             }
144 15         99 }
145              
146             1;
147              
148             __END__