Debian Codegraph @

lib/Mixin/Linewise/Writers.pm

raw symbols SCIP
1 use strictscip-perl cpan Mixin-Linewise 0.111 strict/;
2 use warningsscip-perl cpan Mixin-Linewise 0.111 warnings/;
3 package Mixin::Linewise::Writersscip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/ 0.111;
4 # ABSTRACT: get linewise writers for strings and filenames
5
6 use 5.008001scip-perl cpan Mixin-Linewise 0.111 `5.008001`/; # PerlIO
7 use Carpscip-perl cpan Mixin-Linewise 0.111 Carp/ ();
8 use IO::Filescip-perl cpan Mixin-Linewise 0.111 IO/File/;
9
10 use Sub::Exporterscip-perl cpan Mixin-Linewise 0.111 Sub/Exporter/ -setup => {
11 exports => { map {; "write_$_" => \"_mk_write_$_" } qw(file string) },
12 groups => {
13 default => [ qw(write_file write_string) ],
14 writers => [ qw(write_file write_string) ],
15 },
16 };
17
18 #pod =head1 SYNOPSIS
19 #pod
20 #pod package Your::Pkg;
21 #pod use Mixin::Linewise::Writers -writers;
22 #pod
23 #pod sub write_handle {
24 #pod my ($self, $data, $handle) = @_;
25 #pod
26 #pod $handle->print("datum: $_\n") for @$data;
27 #pod }
28 #pod
29 #pod Then:
30 #pod
31 #pod use Your::Pkg;
32 #pod
33 #pod Your::Pkg->write_file($data, $filename);
34 #pod
35 #pod Your::Pkg->write_string($data, $string);
36 #pod
37 #pod Your::Pkg->write_handle($data, $fh);
38 #pod
39 #pod =head1 EXPORTS
40 #pod
41 #pod C<write_file> and C<write_string> are exported by default. Either can be
42 #pod requested individually, or renamed. They are generated by
43 #pod L<Sub::Exporter|Sub::Exporter>, so consult its documentation for more
44 #pod information.
45 #pod
46 #pod Both can be generated with the option "method" which requests that a method
47 #pod other than "write_handle" is called with the created IO::Handle.
48 #pod
49 #pod If given a "binmode" option, any C<write_file> type functions will use
50 #pod that as an IO layer, otherwise, the default is C<encoding(UTF-8)>.
51 #pod
52 #pod use Mixin::Linewise::Writers -writers => { binmode => "raw" };
53 #pod use Mixin::Linewise::Writers -writers => { binmode => "encoding(iso-8859-1)" };
54 #pod
55 #pod =head2 write_file
56 #pod
57 #pod Your::Pkg->write_file($data, $filename);
58 #pod Your::Pkg->write_file($data, $options, $filename);
59 #pod
60 #pod This method will try to open a new file with the given name. It will then call
61 #pod C<write_handle> with that handle.
62 #pod
63 #pod An optional hash reference may be passed before C<$filename> with options.
64 #pod The only valid option currently is C<binmode>, which overrides any
65 #pod default set from C<use> or the built-in C<encoding(UTF-8)>.
66 #pod
67 #pod Any arguments after C<$filename> are passed along after to C<write_handle>.
68 #pod
69 #pod =cut
70
71 sub _mk_write_filescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/_mk_write_file(). {
72 my ($self, $name, $arg) = @_scip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/`@_`.;
73 my $method = definedscip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/defined(). $arg->{method} ? $arg->{method} : 'write_handle';
74 my $dflt_enc = definedscip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/defined(). $arg->{binmodescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/binmode().} ? $arg->{binmodescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/binmode().} : 'encoding(UTF-8)';
75
76 sub {
77 my ($invocant, $data, $options, $filename);
78 if ( refscip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/ref(). $_scip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/`@_`.[2] eq 'HASH' ) {
79 # got options before filename
80 ($invocant, $data, $options, $filename) = splicescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/splice(). @_scip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/`@_`., 0, 4;
81 }
82 else {
83 ($invocant, $data, $filename) = splicescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/splice(). @_scip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/`@_`., 0, 3;
84 }
85
86 my $binmode = definedscip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/defined(). $options->{binmodescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/binmode().} ? $options->{binmodescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/binmode().} : $dflt_enc;
87 $binmode =~ s/^://; # we add it later
88
89 # Check the file
90 Carp::croakscip-perl cpan Mixin-Linewise 0.111 Carp/croak(). "no filename specified" unless $filename;
91 Carp::croakscip-perl cpan Mixin-Linewise 0.111 Carp/croak(). "'$filename' is not a plain file" if -e $filename && ! -f _;
92
93 # Write out the file
94 my $handle = IO::File->newscip-perl cpan Mixin-Linewise 0.111 IO/File/new().($filename, ">:$binmode")
95 or Carp::croakscip-perl cpan Mixin-Linewise 0.111 Carp/croak(). "couldn't write to file '$filename': $!";
96
97 $invocant->write_handlescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/write_handle().($data, $handle, @_scip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/`@_`.);
98 }
99 }
100
101 #pod =head2 write_string
102 #pod
103 #pod my $string = Your::Pkg->write_string($data);
104 #pod my $string = Your::Pkg->write_string(\%option, $data);
105 #pod
106 #pod C<write_string> will create a new handle on the given string, then call
107 #pod C<write_handle> to write to that handle, and return the resulting string.
108 #pod Because handles on strings must be octet-oriented, the string B<will contain
109 #pod octets>. It will be opened in the default binmode established by importing.
110 #pod (See L</EXPORTS>, above, and the options, below.)
111 #pod
112 #pod Any arguments after C<$data> are passed along after to C<write_handle>.
113 #pod
114 #pod Like C<write_file>, this method can take a leading hashref with one valid
115 #pod argument: C<binmode>.
116 #pod
117 #pod =cut
118
119 sub _mk_write_stringscip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/_mk_write_string(). {
120 my ($self, $name, $arg) = @_scip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/`@_`.;
121 my $method = definedscip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/defined(). $arg->{method} ? $arg->{method} : 'write_handle';
122 my $dflt_enc = definedscip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/defined(). $arg->{binmodescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/binmode().} ? $arg->{binmodescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/binmode().} : 'encoding(UTF-8)';
123
124 sub {
125 my ($opt) = @_scip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/`@_`. > 2 && refscip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/ref(). $_scip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/`@_`.[1] ? splicescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/splice().(@_scip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/`@_`., 1, 1) : undef;
126 my ($invocant, $data) = splicescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/splice(). @_scip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/`@_`., 0, 2;
127
128 my $binmode = ($opt && $opt->{binmodescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/binmode().}) ? $opt->{binmodescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/binmode().} : $dflt_enc;
129 $binmode =~ s/^://; # we add it later
130
131 my $string = '';
132 openscip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/open(). my $handle, ">:$binmode", \$string
133 or diescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/die(). "error opening string for output: $!";
134
135 $invocant->write_handlescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/write_handle().($data, $handle, @_scip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/`@_`.);
136 closescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/close(). $handle or diescip-perl cpan Mixin-Linewise 0.111 Mixin/Linewise/`Writers 0.111`/die(). "error closing string after output: $!";
137
138 return $string;
139 }
140 }
141
142 1;
143
144 __END__
145
146 =pod
147
148 =encoding UTF-8
149
150 =head1 NAME
151
152 Mixin::Linewise::Writers - get linewise writers for strings and filenames
153
154 =head1 VERSION
155
156 version 0.111
157
158 =head1 SYNOPSIS
159
160 package Your::Pkg;
161 use Mixin::Linewise::Writers -writers;
162
163 sub write_handle {
164 my ($self, $data, $handle) = @_;
165
166 $handle->print("datum: $_\n") for @$data;
167 }
168
169 Then:
170
171 use Your::Pkg;
172
173 Your::Pkg->write_file($data, $filename);
174
175 Your::Pkg->write_string($data, $string);
176
177 Your::Pkg->write_handle($data, $fh);
178
179 =head1 PERL VERSION
180
181 This module should work on any version of perl still receiving updates from
182 the Perl 5 Porters. This means it should work on any version of perl released
183 in the last two to three years. (That is, if the most recently released
184 version is v5.40, then this module should work on both v5.40 and v5.38.)
185
186 Although it may work on older versions of perl, no guarantee is made that the
187 minimum required version will not be increased. The version may be increased
188 for any reason, and there is no promise that patches will be accepted to lower
189 the minimum required perl.
190
191 =head1 EXPORTS
192
193 C<write_file> and C<write_string> are exported by default. Either can be
194 requested individually, or renamed. They are generated by
195 L<Sub::Exporter|Sub::Exporter>, so consult its documentation for more
196 information.
197
198 Both can be generated with the option "method" which requests that a method
199 other than "write_handle" is called with the created IO::Handle.
200
201 If given a "binmode" option, any C<write_file> type functions will use
202 that as an IO layer, otherwise, the default is C<encoding(UTF-8)>.
203
204 use Mixin::Linewise::Writers -writers => { binmode => "raw" };
205 use Mixin::Linewise::Writers -writers => { binmode => "encoding(iso-8859-1)" };
206
207 =head2 write_file
208
209 Your::Pkg->write_file($data, $filename);
210 Your::Pkg->write_file($data, $options, $filename);
211
212 This method will try to open a new file with the given name. It will then call
213 C<write_handle> with that handle.
214
215 An optional hash reference may be passed before C<$filename> with options.
216 The only valid option currently is C<binmode>, which overrides any
217 default set from C<use> or the built-in C<encoding(UTF-8)>.
218
219 Any arguments after C<$filename> are passed along after to C<write_handle>.
220
221 =head2 write_string
222
223 my $string = Your::Pkg->write_string($data);
224 my $string = Your::Pkg->write_string(\%option, $data);
225
226 C<write_string> will create a new handle on the given string, then call
227 C<write_handle> to write to that handle, and return the resulting string.
228 Because handles on strings must be octet-oriented, the string B<will contain
229 octets>. It will be opened in the default binmode established by importing.
230 (See L</EXPORTS>, above, and the options, below.)
231
232 Any arguments after C<$data> are passed along after to C<write_handle>.
233
234 Like C<write_file>, this method can take a leading hashref with one valid
235 argument: C<binmode>.
236
237 =head1 AUTHOR
238
239 Ricardo SIGNES <[email protected]>
240
241 =head1 COPYRIGHT AND LICENSE
242
243 This software is copyright (c) 2008 by Ricardo SIGNES.
244
245 This is free software; you can redistribute it and/or modify it under
246 the same terms as the Perl 5 programming language system itself.
247
248 =cut