1 | package Mojo::ByteStream;
|
---|
2 | use Mojo::Base -strict;
|
---|
3 | use overload bool => sub {1}, '""' => sub { ${$_[0]} }, fallback => 1;
|
---|
4 |
|
---|
5 | use Exporter 'import';
|
---|
6 | use Mojo::Collection;
|
---|
7 | use Mojo::Util;
|
---|
8 |
|
---|
9 | our @EXPORT_OK = ('b');
|
---|
10 |
|
---|
11 | # Turn most functions from Mojo::Util into methods
|
---|
12 | my @UTILS = (
|
---|
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 slugify term_escape trim unindent unquote url_escape),
|
---|
16 | qw(url_unescape xml_escape xor_encode)
|
---|
17 | );
|
---|
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 | };
|
---|
25 | }
|
---|
26 |
|
---|
27 | sub b { __PACKAGE__->new(@_) }
|
---|
28 |
|
---|
29 | sub clone { $_[0]->new(${$_[0]}) }
|
---|
30 |
|
---|
31 | sub decode { shift->_delegate(\&Mojo::Util::decode, @_) }
|
---|
32 | sub encode { shift->_delegate(\&Mojo::Util::encode, @_) }
|
---|
33 |
|
---|
34 | sub new {
|
---|
35 | my $class = shift;
|
---|
36 | return bless \(my $dummy = join '', @_), ref $class || $class;
|
---|
37 | }
|
---|
38 |
|
---|
39 | sub say {
|
---|
40 | my ($self, $handle) = @_;
|
---|
41 | $handle ||= \*STDOUT;
|
---|
42 | say $handle $$self;
|
---|
43 | return $self;
|
---|
44 | }
|
---|
45 |
|
---|
46 | sub secure_compare { Mojo::Util::secure_compare ${shift()}, shift }
|
---|
47 |
|
---|
48 | sub size { length ${$_[0]} }
|
---|
49 |
|
---|
50 | sub split {
|
---|
51 | my ($self, $pattern) = @_;
|
---|
52 | return Mojo::Collection->new(map { $self->new($_) } split $pattern, $$self);
|
---|
53 | }
|
---|
54 |
|
---|
55 | sub tap { shift->Mojo::Base::tap(@_) }
|
---|
56 |
|
---|
57 | sub to_string { ${$_[0]} }
|
---|
58 |
|
---|
59 | sub with_roles { shift->Mojo::Base::with_roles(@_) }
|
---|
60 |
|
---|
61 | sub _delegate {
|
---|
62 | my ($self, $sub) = (shift, shift);
|
---|
63 | $$self = $sub->(shift || 'UTF-8', $$self);
|
---|
64 | return $self;
|
---|
65 | }
|
---|
66 |
|
---|
67 | 1;
|
---|
68 |
|
---|
69 | =encoding utf8
|
---|
70 |
|
---|
71 | =head1 NAME
|
---|
72 |
|
---|
73 | Mojo::ByteStream - ByteStream
|
---|
74 |
|
---|
75 | =head1 SYNOPSIS
|
---|
76 |
|
---|
77 | use Mojo::ByteStream;
|
---|
78 |
|
---|
79 | # Manipulate bytestream
|
---|
80 | my $stream = Mojo::ByteStream->new('foo_bar_baz');
|
---|
81 | say $stream->camelize;
|
---|
82 |
|
---|
83 | # Chain methods
|
---|
84 | my $stream = Mojo::ByteStream->new('foo bar baz')->quote;
|
---|
85 | $stream = $stream->unquote->encode('UTF-8')->b64_encode('');
|
---|
86 | say "$stream";
|
---|
87 |
|
---|
88 | # Use the alternative constructor
|
---|
89 | use Mojo::ByteStream 'b';
|
---|
90 | my $stream = b('foobarbaz')->b64_encode('')->say;
|
---|
91 |
|
---|
92 | =head1 DESCRIPTION
|
---|
93 |
|
---|
94 | L<Mojo::ByteStream> is a scalar-based container for bytestreams that provides a
|
---|
95 | more friendly API for many of the functions in L<Mojo::Util>.
|
---|
96 |
|
---|
97 | # Access scalar directly to manipulate bytestream
|
---|
98 | my $stream = Mojo::ByteStream->new('foo');
|
---|
99 | $$stream .= 'bar';
|
---|
100 |
|
---|
101 | =head1 FUNCTIONS
|
---|
102 |
|
---|
103 | L<Mojo::ByteStream> implements the following functions, which can be imported
|
---|
104 | individually.
|
---|
105 |
|
---|
106 | =head2 b
|
---|
107 |
|
---|
108 | my $stream = b('test123');
|
---|
109 |
|
---|
110 | Construct a new scalar-based L<Mojo::ByteStream> object.
|
---|
111 |
|
---|
112 | =head1 METHODS
|
---|
113 |
|
---|
114 | L<Mojo::ByteStream> implements the following methods.
|
---|
115 |
|
---|
116 | =head2 b64_decode
|
---|
117 |
|
---|
118 | $stream = $stream->b64_decode;
|
---|
119 |
|
---|
120 | Base64 decode bytestream with L<Mojo::Util/"b64_decode">.
|
---|
121 |
|
---|
122 | =head2 b64_encode
|
---|
123 |
|
---|
124 | $stream = $stream->b64_encode;
|
---|
125 | $stream = $stream->b64_encode("\n");
|
---|
126 |
|
---|
127 | Base64 encode bytestream with L<Mojo::Util/"b64_encode">.
|
---|
128 |
|
---|
129 | # "Zm9vIGJhciBiYXo="
|
---|
130 | b('foo bar baz')->b64_encode('');
|
---|
131 |
|
---|
132 | =head2 camelize
|
---|
133 |
|
---|
134 | $stream = $stream->camelize;
|
---|
135 |
|
---|
136 | Camelize bytestream with L<Mojo::Util/"camelize">.
|
---|
137 |
|
---|
138 | =head2 clone
|
---|
139 |
|
---|
140 | my $stream2 = $stream->clone;
|
---|
141 |
|
---|
142 | Return a new L<Mojo::ByteStream> object cloned from this bytestream.
|
---|
143 |
|
---|
144 | =head2 decamelize
|
---|
145 |
|
---|
146 | $stream = $stream->decamelize;
|
---|
147 |
|
---|
148 | Decamelize bytestream with L<Mojo::Util/"decamelize">.
|
---|
149 |
|
---|
150 | =head2 decode
|
---|
151 |
|
---|
152 | $stream = $stream->decode;
|
---|
153 | $stream = $stream->decode('iso-8859-1');
|
---|
154 |
|
---|
155 | Decode bytestream with L<Mojo::Util/"decode">, defaults to using C<UTF-8>.
|
---|
156 |
|
---|
157 | # "â¥"
|
---|
158 | b('%E2%99%A5')->url_unescape->decode;
|
---|
159 |
|
---|
160 | =head2 encode
|
---|
161 |
|
---|
162 | $stream = $stream->encode;
|
---|
163 | $stream = $stream->encode('iso-8859-1');
|
---|
164 |
|
---|
165 | Encode bytestream with L<Mojo::Util/"encode">, defaults to using C<UTF-8>.
|
---|
166 |
|
---|
167 | # "%E2%99%A5"
|
---|
168 | b('â¥')->encode->url_escape;
|
---|
169 |
|
---|
170 | =head2 hmac_sha1_sum
|
---|
171 |
|
---|
172 | $stream = $stream->hmac_sha1_sum('passw0rd');
|
---|
173 |
|
---|
174 | Generate HMAC-SHA1 checksum for bytestream with L<Mojo::Util/"hmac_sha1_sum">.
|
---|
175 |
|
---|
176 | # "7fbdc89263974a89210ea71f171c77d3f8c21471"
|
---|
177 | b('foo bar baz')->hmac_sha1_sum('secr3t');
|
---|
178 |
|
---|
179 | =head2 html_unescape
|
---|
180 |
|
---|
181 | $stream = $stream->html_unescape;
|
---|
182 |
|
---|
183 | Unescape all HTML entities in bytestream with L<Mojo::Util/"html_unescape">.
|
---|
184 |
|
---|
185 | # "%3Chtml%3E"
|
---|
186 | b('<html>')->html_unescape->url_escape;
|
---|
187 |
|
---|
188 | =head2 md5_bytes
|
---|
189 |
|
---|
190 | $stream = $stream->md5_bytes;
|
---|
191 |
|
---|
192 | Generate binary MD5 checksum for bytestream with L<Mojo::Util/"md5_bytes">.
|
---|
193 |
|
---|
194 | =head2 md5_sum
|
---|
195 |
|
---|
196 | $stream = $stream->md5_sum;
|
---|
197 |
|
---|
198 | Generate MD5 checksum for bytestream with L<Mojo::Util/"md5_sum">.
|
---|
199 |
|
---|
200 | =head2 new
|
---|
201 |
|
---|
202 | my $stream = Mojo::ByteStream->new('test123');
|
---|
203 |
|
---|
204 | Construct a new scalar-based L<Mojo::ByteStream> object.
|
---|
205 |
|
---|
206 | =head2 punycode_decode
|
---|
207 |
|
---|
208 | $stream = $stream->punycode_decode;
|
---|
209 |
|
---|
210 | Punycode decode bytestream with L<Mojo::Util/"punycode_decode">.
|
---|
211 |
|
---|
212 | =head2 punycode_encode
|
---|
213 |
|
---|
214 | $stream = $stream->punycode_encode;
|
---|
215 |
|
---|
216 | Punycode encode bytestream with L<Mojo::Util/"punycode_encode">.
|
---|
217 |
|
---|
218 | =head2 quote
|
---|
219 |
|
---|
220 | $stream = $stream->quote;
|
---|
221 |
|
---|
222 | Quote bytestream with L<Mojo::Util/"quote">.
|
---|
223 |
|
---|
224 | =head2 say
|
---|
225 |
|
---|
226 | $stream = $stream->say;
|
---|
227 | $stream = $stream->say(*STDERR);
|
---|
228 |
|
---|
229 | Print bytestream to handle and append a newline, defaults to using C<STDOUT>.
|
---|
230 |
|
---|
231 | =head2 secure_compare
|
---|
232 |
|
---|
233 | my $bool = $stream->secure_compare($str);
|
---|
234 |
|
---|
235 | Compare bytestream with L<Mojo::Util/"secure_compare">.
|
---|
236 |
|
---|
237 | =head2 sha1_bytes
|
---|
238 |
|
---|
239 | $stream = $stream->sha1_bytes;
|
---|
240 |
|
---|
241 | Generate binary SHA1 checksum for bytestream with L<Mojo::Util/"sha1_bytes">.
|
---|
242 |
|
---|
243 | =head2 sha1_sum
|
---|
244 |
|
---|
245 | $stream = $stream->sha1_sum;
|
---|
246 |
|
---|
247 | Generate SHA1 checksum for bytestream with L<Mojo::Util/"sha1_sum">.
|
---|
248 |
|
---|
249 | =head2 size
|
---|
250 |
|
---|
251 | my $size = $stream->size;
|
---|
252 |
|
---|
253 | Size of bytestream.
|
---|
254 |
|
---|
255 | =head2 slugify
|
---|
256 |
|
---|
257 | $stream = $stream->slugify;
|
---|
258 | $stream = $stream->slugify($bool);
|
---|
259 |
|
---|
260 | Generate URL slug for bytestream with L<Mojo::Util/"slugify">.
|
---|
261 |
|
---|
262 | =head2 split
|
---|
263 |
|
---|
264 | my $collection = $stream->split(',');
|
---|
265 |
|
---|
266 | Turn bytestream into L<Mojo::Collection> object containing L<Mojo::ByteStream>
|
---|
267 | objects.
|
---|
268 |
|
---|
269 | # "One,Two,Three"
|
---|
270 | b("one,two,three")->split(',')->map('camelize')->join(',');
|
---|
271 |
|
---|
272 | =head2 tap
|
---|
273 |
|
---|
274 | $stream = $stream->tap(sub {...});
|
---|
275 |
|
---|
276 | Alias for L<Mojo::Base/"tap">.
|
---|
277 |
|
---|
278 | =head2 term_escape
|
---|
279 |
|
---|
280 | $stream = $stream->term_escape;
|
---|
281 |
|
---|
282 | Escape POSIX control characters in bytestream with L<Mojo::Util/"term_escape">.
|
---|
283 |
|
---|
284 | # Print binary checksum to terminal
|
---|
285 | b('foo')->sha1_bytes->term_escape->say;
|
---|
286 |
|
---|
287 | =head2 to_string
|
---|
288 |
|
---|
289 | my $str = $stream->to_string;
|
---|
290 |
|
---|
291 | Stringify bytestream.
|
---|
292 |
|
---|
293 | =head2 trim
|
---|
294 |
|
---|
295 | $stream = $stream->trim;
|
---|
296 |
|
---|
297 | Trim whitespace characters from both ends of bytestream with
|
---|
298 | L<Mojo::Util/"trim">.
|
---|
299 |
|
---|
300 | =head2 unindent
|
---|
301 |
|
---|
302 | $stream = $stream->unindent;
|
---|
303 |
|
---|
304 | Unindent bytestream with L<Mojo::Util/"unindent">.
|
---|
305 |
|
---|
306 | =head2 unquote
|
---|
307 |
|
---|
308 | $stream = $stream->unquote;
|
---|
309 |
|
---|
310 | Unquote bytestream with L<Mojo::Util/"unquote">.
|
---|
311 |
|
---|
312 | =head2 url_escape
|
---|
313 |
|
---|
314 | $stream = $stream->url_escape;
|
---|
315 | $stream = $stream->url_escape('^A-Za-z0-9\-._~');
|
---|
316 |
|
---|
317 | Percent encode all unsafe characters in bytestream with
|
---|
318 | L<Mojo::Util/"url_escape">.
|
---|
319 |
|
---|
320 | # "%E2%98%83"
|
---|
321 | b('â')->encode->url_escape;
|
---|
322 |
|
---|
323 | =head2 url_unescape
|
---|
324 |
|
---|
325 | $stream = $stream->url_unescape;
|
---|
326 |
|
---|
327 | Decode percent encoded characters in bytestream with
|
---|
328 | L<Mojo::Util/"url_unescape">.
|
---|
329 |
|
---|
330 | # "<html>"
|
---|
331 | b('%3Chtml%3E')->url_unescape->xml_escape;
|
---|
332 |
|
---|
333 | =head2 with_roles
|
---|
334 |
|
---|
335 | my $new_class = Mojo::ByteStream->with_roles('Mojo::ByteStream::Role::One');
|
---|
336 | my $new_class = Mojo::ByteStream->with_roles('+One', '+Two');
|
---|
337 | $stream = $stream->with_roles('+One', '+Two');
|
---|
338 |
|
---|
339 | Alias for L<Mojo::Base/"with_roles">.
|
---|
340 |
|
---|
341 | =head2 xml_escape
|
---|
342 |
|
---|
343 | $stream = $stream->xml_escape;
|
---|
344 |
|
---|
345 | Escape only the characters C<&>, C<E<lt>>, C<E<gt>>, C<"> and C<'> in
|
---|
346 | bytestream with L<Mojo::Util/"xml_escape">.
|
---|
347 |
|
---|
348 | =head2 xor_encode
|
---|
349 |
|
---|
350 | $stream = $stream->xor_encode($key);
|
---|
351 |
|
---|
352 | XOR encode bytestream with L<Mojo::Util/"xor_encode">.
|
---|
353 |
|
---|
354 | # "%04%0E%15B%03%1B%10"
|
---|
355 | b('foo bar')->xor_encode('baz')->url_escape;
|
---|
356 |
|
---|
357 | =head1 OPERATORS
|
---|
358 |
|
---|
359 | L<Mojo::ByteStream> overloads the following operators.
|
---|
360 |
|
---|
361 | =head2 bool
|
---|
362 |
|
---|
363 | my $bool = !!$bytestream;
|
---|
364 |
|
---|
365 | Always true.
|
---|
366 |
|
---|
367 | =head2 stringify
|
---|
368 |
|
---|
369 | my $str = "$bytestream";
|
---|
370 |
|
---|
371 | Alias for L</"to_string">.
|
---|
372 |
|
---|
373 | =head1 SEE ALSO
|
---|
374 |
|
---|
375 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
|
---|
376 |
|
---|
377 | =cut
|
---|