copy gitweblite soruce code
|
1 |
package Mojo::ByteStream; |
update Mojolicious to 4.57
|
2 |
use Mojo::Base -strict; |
copy gitweblite soruce code
|
3 |
use overload '""' => sub { shift->to_string }, fallback => 1; |
4 | ||
upgraded Mojolicious to v3.7...
|
5 |
use Exporter 'import'; |
copy gitweblite soruce code
|
6 |
use Mojo::Collection; |
7 |
use Mojo::Util; |
|
8 | ||
upgraded Mojolicious to v3.7...
|
9 |
our @EXPORT_OK = ('b'); |
10 | ||
copy gitweblite soruce code
|
11 |
# Turn most functions from Mojo::Util into methods |
12 |
my @UTILS = ( |
|
update Mojolicious 4.07
|
13 |
qw(b64_decode b64_encode camelize decamelize hmac_sha1_sum html_unescape), |
14 |
qw(md5_bytes md5_sum punycode_decode punycode_encode quote sha1_bytes), |
|
15 |
qw(sha1_sum slurp spurt squish trim unquote url_escape url_unescape), |
|
16 |
qw(xml_escape xor_encode) |
|
copy gitweblite soruce code
|
17 |
); |
upgraded Mojolicious to v3.7...
|
18 |
for my $name (@UTILS) { |
19 |
my $sub = Mojo::Util->can($name); |
|
20 |
Mojo::Util::monkey_patch __PACKAGE__, $name, sub { |
|
21 |
my $self = shift; |
|
22 |
$$self = $sub->($$self, @_); |
|
23 |
return $self; |
|
24 |
}; |
|
copy gitweblite soruce code
|
25 |
} |
26 | ||
27 |
sub new { |
|
28 |
my $class = shift; |
|
29 |
return bless \(my $dummy = join '', @_), ref $class || $class; |
|
30 |
} |
|
31 | ||
upgraded Mojolicious to v3.7...
|
32 |
sub b { __PACKAGE__->new(@_) } |
33 | ||
34 |
sub clone { $_[0]->new(${$_[0]}) } |
|
copy gitweblite soruce code
|
35 | |
36 |
sub decode { |
|
37 |
my $self = shift; |
|
38 |
$$self = Mojo::Util::decode shift || 'UTF-8', $$self; |
|
39 |
return $self; |
|
40 |
} |
|
41 | ||
42 |
sub encode { |
|
43 |
my $self = shift; |
|
44 |
$$self = Mojo::Util::encode shift || 'UTF-8', $$self; |
|
45 |
return $self; |
|
46 |
} |
|
47 | ||
48 |
{ |
|
49 |
no warnings 'redefine'; |
|
50 |
sub say { |
|
51 |
my ($self, $handle) = @_; |
|
52 |
$handle ||= \*STDOUT; |
|
53 |
print $handle $$self. "\n"; |
|
54 |
} |
|
55 |
} |
|
56 | ||
upgraded Mojolicious to v3.7...
|
57 |
sub secure_compare { Mojo::Util::secure_compare ${shift()}, @_ } |
copy gitweblite soruce code
|
58 | |
upgraded Mojolicious to v3.7...
|
59 |
sub size { length ${$_[0]} } |
copy gitweblite soruce code
|
60 | |
61 |
sub split { |
|
62 |
my ($self, $pattern) = @_; |
|
63 |
return Mojo::Collection->new(map { $self->new($_) } split $pattern, $$self); |
|
64 |
} |
|
65 | ||
update Mojolicious to 4.57
|
66 |
sub tap { shift->Mojo::Base::tap(@_) } |
67 | ||
upgraded Mojolicious to v3.7...
|
68 |
sub to_string { ${$_[0]} } |
copy gitweblite soruce code
|
69 | |
70 |
1; |
|
71 | ||
update Mojolicious to 4.57
|
72 |
=encoding utf8 |
73 | ||
copy gitweblite soruce code
|
74 |
=head1 NAME |
75 | ||
76 |
Mojo::ByteStream - ByteStream |
|
77 | ||
78 |
=head1 SYNOPSIS |
|
79 | ||
80 |
# Manipulate bytestreams |
|
81 |
use Mojo::ByteStream; |
|
82 |
my $stream = Mojo::ByteStream->new('foo_bar_baz'); |
|
83 |
say $stream->camelize; |
|
84 | ||
85 |
# Chain methods |
|
86 |
my $stream = Mojo::ByteStream->new('foo bar baz')->quote; |
|
upgraded Mojolicious to v3.7...
|
87 |
$stream = $stream->unquote->encode('UTF-8')->b64_encode(''); |
88 |
say "$stream"; |
|
copy gitweblite soruce code
|
89 | |
90 |
# Use the alternative constructor |
|
91 |
use Mojo::ByteStream 'b'; |
|
update Mojolicious and added...
|
92 |
my $stream = b('foobarbaz')->b64_encode('')->say; |
copy gitweblite soruce code
|
93 | |
94 |
=head1 DESCRIPTION |
|
95 | ||
96 |
L<Mojo::ByteStream> provides a more friendly API for the bytestream |
|
97 |
manipulation functions in L<Mojo::Util>. |
|
98 | ||
upgraded Mojolicious to v3.7...
|
99 |
=head1 FUNCTIONS |
100 | ||
101 |
L<Mojo::ByteStream> implements the following functions. |
|
102 | ||
update Mojolicious and added...
|
103 |
=head2 b |
upgraded Mojolicious to v3.7...
|
104 | |
105 |
my $stream = b('test123'); |
|
106 | ||
107 |
Construct a new scalar-based L<Mojo::ByteStream> object. |
|
108 | ||
copy gitweblite soruce code
|
109 |
=head1 METHODS |
110 | ||
update Mojolicious to 4.57
|
111 |
L<Mojo::ByteStream> implements the following methods. |
copy gitweblite soruce code
|
112 | |
update Mojolicious and added...
|
113 |
=head2 new |
copy gitweblite soruce code
|
114 | |
115 |
my $stream = Mojo::ByteStream->new('test123'); |
|
116 | ||
upgraded Mojolicious to v3.7...
|
117 |
Construct a new scalar-based L<Mojo::ByteStream> object. |
copy gitweblite soruce code
|
118 | |
update Mojolicious and added...
|
119 |
=head2 b64_decode |
copy gitweblite soruce code
|
120 | |
121 |
$stream = $stream->b64_decode; |
|
122 | ||
upgraded Mojolicious to v3.7...
|
123 |
Base64 decode bytestream with L<Mojo::Util/"b64_decode">. |
copy gitweblite soruce code
|
124 | |
update Mojolicious and added...
|
125 |
=head2 b64_encode |
copy gitweblite soruce code
|
126 | |
127 |
$stream = $stream->b64_encode; |
|
upgraded Mojolicious to v3.7...
|
128 |
$stream = $stream->b64_encode("\n"); |
129 | ||
130 |
Base64 encode bytestream with L<Mojo::Util/"b64_encode">. |
|
copy gitweblite soruce code
|
131 | |
upgraded Mojolicious to v3.7...
|
132 |
b('foo bar baz')->b64_encode('')->say; |
copy gitweblite soruce code
|
133 | |
update Mojolicious and added...
|
134 |
=head2 camelize |
copy gitweblite soruce code
|
135 | |
136 |
$stream = $stream->camelize; |
|
137 | ||
upgraded Mojolicious to v3.7...
|
138 |
Camelize bytestream with L<Mojo::Util/"camelize">. |
copy gitweblite soruce code
|
139 | |
update Mojolicious and added...
|
140 |
=head2 clone |
copy gitweblite soruce code
|
141 | |
142 |
my $stream2 = $stream->clone; |
|
143 | ||
144 |
Clone bytestream. |
|
145 | ||
update Mojolicious and added...
|
146 |
=head2 decamelize |
copy gitweblite soruce code
|
147 | |
148 |
$stream = $stream->decamelize; |
|
149 | ||
upgraded Mojolicious to v3.7...
|
150 |
Decamelize bytestream with L<Mojo::Util/"decamelize">. |
copy gitweblite soruce code
|
151 | |
update Mojolicious and added...
|
152 |
=head2 decode |
copy gitweblite soruce code
|
153 | |
154 |
$stream = $stream->decode; |
|
upgraded Mojolicious to v3.7...
|
155 |
$stream = $stream->decode('iso-8859-1'); |
copy gitweblite soruce code
|
156 | |
upgraded Mojolicious to v3.7...
|
157 |
Decode bytestream with L<Mojo::Util/"decode">, defaults to C<UTF-8>. |
copy gitweblite soruce code
|
158 | |
upgraded Mojolicious to v3.7...
|
159 |
$stream->decode('UTF-16LE')->unquote->trim->say; |
copy gitweblite soruce code
|
160 | |
update Mojolicious and added...
|
161 |
=head2 encode |
copy gitweblite soruce code
|
162 | |
163 |
$stream = $stream->encode; |
|
upgraded Mojolicious to v3.7...
|
164 |
$stream = $stream->encode('iso-8859-1'); |
copy gitweblite soruce code
|
165 | |
upgraded Mojolicious to v3.7...
|
166 |
Encode bytestream with L<Mojo::Util/"encode">, defaults to C<UTF-8>. |
copy gitweblite soruce code
|
167 | |
upgraded Mojolicious to v3.7...
|
168 |
$stream->trim->quote->encode->say; |
copy gitweblite soruce code
|
169 | |
update Mojolicious and added...
|
170 |
=head2 hmac_sha1_sum |
copy gitweblite soruce code
|
171 | |
upgraded Mojolicious to v3.7...
|
172 |
$stream = $stream->hmac_sha1_sum('passw0rd'); |
copy gitweblite soruce code
|
173 | |
upgraded Mojolicious to v3.7...
|
174 |
Generate HMAC-SHA1 checksum for bytestream with L<Mojo::Util/"hmac_sha1_sum">. |
175 | ||
176 |
b('foo bar baz')->hmac_sha1_sum('secr3t')->quote->say; |
|
copy gitweblite soruce code
|
177 | |
update Mojolicious and added...
|
178 |
=head2 html_unescape |
copy gitweblite soruce code
|
179 | |
180 |
$stream = $stream->html_unescape; |
|
181 | ||
upgraded Mojolicious to v3.7...
|
182 |
Unescape all HTML entities in bytestream with L<Mojo::Util/"html_unescape">. |
183 | ||
184 |
b('<html>')->html_unescape->url_escape->say; |
|
copy gitweblite soruce code
|
185 | |
update Mojolicious and added...
|
186 |
=head2 md5_bytes |
copy gitweblite soruce code
|
187 | |
188 |
$stream = $stream->md5_bytes; |
|
189 | ||
upgraded Mojolicious to v3.7...
|
190 |
Generate binary MD5 checksum for bytestream with L<Mojo::Util/"md5_bytes">. |
copy gitweblite soruce code
|
191 | |
update Mojolicious and added...
|
192 |
=head2 md5_sum |
copy gitweblite soruce code
|
193 | |
194 |
$stream = $stream->md5_sum; |
|
195 | ||
upgraded Mojolicious to v3.7...
|
196 |
Generate MD5 checksum for bytestream with L<Mojo::Util/"md5_sum">. |
copy gitweblite soruce code
|
197 | |
update Mojolicious and added...
|
198 |
=head2 punycode_decode |
copy gitweblite soruce code
|
199 | |
200 |
$stream = $stream->punycode_decode; |
|
201 | ||
upgraded Mojolicious to v3.7...
|
202 |
Punycode decode bytestream with L<Mojo::Util/"punycode_decode">. |
copy gitweblite soruce code
|
203 | |
update Mojolicious and added...
|
204 |
=head2 punycode_encode |
copy gitweblite soruce code
|
205 | |
206 |
$stream = $stream->punycode_encode; |
|
207 | ||
upgraded Mojolicious to v3.7...
|
208 |
Punycode encode bytestream with L<Mojo::Util/"punycode_encode">. |
copy gitweblite soruce code
|
209 | |
update Mojolicious and added...
|
210 |
=head2 quote |
copy gitweblite soruce code
|
211 | |
212 |
$stream = $stream->quote; |
|
213 | ||
upgraded Mojolicious to v3.7...
|
214 |
Quote bytestream with L<Mojo::Util/"quote">. |
copy gitweblite soruce code
|
215 | |
update Mojolicious and added...
|
216 |
=head2 say |
copy gitweblite soruce code
|
217 | |
218 |
$stream->say; |
|
219 |
$stream->say(*STDERR); |
|
220 | ||
upgraded Mojolicious to v3.7...
|
221 |
Print bytestream to handle and append a newline, defaults to C<STDOUT>. |
copy gitweblite soruce code
|
222 | |
update Mojolicious and added...
|
223 |
=head2 secure_compare |
copy gitweblite soruce code
|
224 | |
update Mojolicious to 4.57
|
225 |
my $bool = $stream->secure_compare($str); |
copy gitweblite soruce code
|
226 | |
upgraded Mojolicious to v3.7...
|
227 |
Compare bytestream with L<Mojo::Util/"secure_compare">. |
228 | ||
229 |
say 'Match!' if b('foo')->secure_compare('foo'); |
|
copy gitweblite soruce code
|
230 | |
update Mojolicious and added...
|
231 |
=head2 sha1_bytes |
copy gitweblite soruce code
|
232 | |
233 |
$stream = $stream->sha1_bytes; |
|
234 | ||
upgraded Mojolicious to v3.7...
|
235 |
Generate binary SHA1 checksum for bytestream with L<Mojo::Util/"sha1_bytes">. |
copy gitweblite soruce code
|
236 | |
update Mojolicious and added...
|
237 |
=head2 sha1_sum |
copy gitweblite soruce code
|
238 | |
239 |
$stream = $stream->sha1_sum; |
|
240 | ||
upgraded Mojolicious to v3.7...
|
241 |
Generate SHA1 checksum for bytestream with L<Mojo::Util/"sha1_sum">. |
copy gitweblite soruce code
|
242 | |
update Mojolicious and added...
|
243 |
=head2 size |
copy gitweblite soruce code
|
244 | |
245 |
my $size = $stream->size; |
|
246 | ||
247 |
Size of bytestream. |
|
248 | ||
update Mojolicious and added...
|
249 |
=head2 slurp |
upgraded Mojolicious to v3.7...
|
250 | |
251 |
$stream = $stream->slurp; |
|
252 | ||
253 |
Read all data at once from file into bytestream with L<Mojo::Util/"slurp">. |
|
254 | ||
255 |
b('/home/sri/myapp.pl')->slurp->split("\n")->shuffle->join("\n")->say; |
|
256 | ||
update Mojolicious and added...
|
257 |
=head2 spurt |
upgraded Mojolicious to v3.7...
|
258 | |
259 |
$stream = $stream->spurt('/home/sri/myapp.pl'); |
|
260 | ||
261 |
Write all data from bytestream at once to file with L<Mojo::Util/"spurt">. |
|
262 | ||
263 |
b('/home/sri/foo.txt')->slurp->squish->spurt('/home/sri/bar.txt'); |
|
264 | ||
update Mojolicious and added...
|
265 |
=head2 split |
copy gitweblite soruce code
|
266 | |
267 |
my $collection = $stream->split(','); |
|
268 | ||
update Mojolicious to 4.57
|
269 |
Turn bytestream into L<Mojo::Collection> object containing L<Mojo::ByteStream> |
270 |
objects. |
|
copy gitweblite soruce code
|
271 | |
update Mojolicious to 4.57
|
272 |
b('a,b,c')->split(',')->quote->join(',')->say; |
upgraded Mojolicious to v3.7...
|
273 | |
update Mojolicious and added...
|
274 |
=head2 squish |
upgraded Mojolicious to v3.7...
|
275 | |
276 |
$stream = $stream->squish; |
|
277 | ||
278 |
Trim whitespace characters from both ends of bytestream and then change all |
|
279 |
consecutive groups of whitespace into one space each with |
|
280 |
L<Mojo::Util/"squish">. |
|
copy gitweblite soruce code
|
281 | |
update Mojolicious to 4.57
|
282 |
=head2 tap |
283 | ||
284 |
$stream = $stream->tap(sub {...}); |
|
285 | ||
286 |
Alias for L<Mojo::Base/"tap">. |
|
287 | ||
update Mojolicious and added...
|
288 |
=head2 to_string |
copy gitweblite soruce code
|
289 | |
update Mojolicious 4.07
|
290 |
my $str = $stream->to_string; |
291 |
my $str = "$stream"; |
|
copy gitweblite soruce code
|
292 | |
293 |
Stringify bytestream. |
|
294 | ||
update Mojolicious and added...
|
295 |
=head2 trim |
copy gitweblite soruce code
|
296 | |
297 |
$stream = $stream->trim; |
|
298 | ||
upgraded Mojolicious to v3.7...
|
299 |
Trim whitespace characters from both ends of bytestream with |
300 |
L<Mojo::Util/"trim">. |
|
copy gitweblite soruce code
|
301 | |
update Mojolicious and added...
|
302 |
=head2 unquote |
copy gitweblite soruce code
|
303 | |
304 |
$stream = $stream->unquote; |
|
305 | ||
upgraded Mojolicious to v3.7...
|
306 |
Unquote bytestream with L<Mojo::Util/"unquote">. |
copy gitweblite soruce code
|
307 | |
update Mojolicious and added...
|
308 |
=head2 url_escape |
copy gitweblite soruce code
|
309 | |
310 |
$stream = $stream->url_escape; |
|
upgraded Mojolicious to v3.7...
|
311 |
$stream = $stream->url_escape('^A-Za-z0-9\-._~'); |
copy gitweblite soruce code
|
312 | |
upgraded Mojolicious to v3.7...
|
313 |
Percent encode all unsafe characters in bytestream with |
314 |
L<Mojo::Util/"url_escape">. |
|
315 | ||
316 |
b('foo bar baz')->url_escape->say; |
|
copy gitweblite soruce code
|
317 | |
update Mojolicious and added...
|
318 |
=head2 url_unescape |
copy gitweblite soruce code
|
319 | |
320 |
$stream = $stream->url_unescape; |
|
321 | ||
upgraded Mojolicious to v3.7...
|
322 |
Decode percent encoded characters in bytestream with |
323 |
L<Mojo::Util/"url_unescape">. |
|
324 | ||
update Mojolicious and added...
|
325 |
b('%3Chtml%3E')->url_unescape->xml_escape->say; |
copy gitweblite soruce code
|
326 | |
update Mojolicious and added...
|
327 |
=head2 xml_escape |
copy gitweblite soruce code
|
328 | |
329 |
$stream = $stream->xml_escape; |
|
330 | ||
upgraded Mojolicious to v3.7...
|
331 |
Escape only the characters C<&>, C<E<lt>>, C<E<gt>>, C<"> and C<'> in |
332 |
bytestream with L<Mojo::Util/"xml_escape">. |
|
333 | ||
update Mojolicious and added...
|
334 |
=head2 xor_encode |
upgraded Mojolicious to v3.7...
|
335 | |
336 |
$stream = $stream->xor_encode($key); |
|
337 | ||
338 |
XOR encode bytestream with L<Mojo::Util/"xor_encode">. |
|
copy gitweblite soruce code
|
339 | |
update Mojolicious to 4.57
|
340 |
=head1 BYTESTREAM |
341 | ||
342 |
Direct scalar reference access to the bytestream is also possible. |
|
343 | ||
344 |
$$stream .= 'foo'; |
|
345 | ||
copy gitweblite soruce code
|
346 |
=head1 SEE ALSO |
347 | ||
348 |
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>. |
|
349 | ||
350 |
=cut |