1Mojo::ByteStream(3) User Contributed Perl Documentation Mojo::ByteStream(3)
2
3
4
6 Mojo::ByteStream - ByteStream
7
9 use Mojo::ByteStream;
10
11 # Manipulate bytestream
12 my $stream = Mojo::ByteStream->new('foo_bar_baz');
13 say $stream->camelize;
14
15 # Chain methods
16 my $stream = Mojo::ByteStream->new('foo bar baz')->quote;
17 $stream = $stream->unquote->encode('UTF-8')->b64_encode('');
18 say "$stream";
19
20 # Use the alternative constructor
21 use Mojo::ByteStream 'b';
22 my $stream = b('foobarbaz')->b64_encode('')->say;
23
25 Mojo::ByteStream is a scalar-based container for bytestreams that
26 provides a more friendly API for many of the functions in Mojo::Util.
27
28 # Access scalar directly to manipulate bytestream
29 my $stream = Mojo::ByteStream->new('foo');
30 $$stream .= 'bar';
31
33 Mojo::ByteStream implements the following functions, which can be
34 imported individually.
35
36 b
37 my $stream = b('test123');
38
39 Construct a new scalar-based Mojo::ByteStream object.
40
42 Mojo::ByteStream implements the following methods.
43
44 b64_decode
45 $stream = $stream->b64_decode;
46
47 Base64 decode bytestream with "b64_decode" in Mojo::Util.
48
49 b64_encode
50 $stream = $stream->b64_encode;
51 $stream = $stream->b64_encode("\n");
52
53 Base64 encode bytestream with "b64_encode" in Mojo::Util.
54
55 # "Zm9vIGJhciBiYXo="
56 b('foo bar baz')->b64_encode('');
57
58 camelize
59 $stream = $stream->camelize;
60
61 Camelize bytestream with "camelize" in Mojo::Util.
62
63 clone
64 my $stream2 = $stream->clone;
65
66 Return a new Mojo::ByteStream object cloned from this bytestream.
67
68 decamelize
69 $stream = $stream->decamelize;
70
71 Decamelize bytestream with "decamelize" in Mojo::Util.
72
73 decode
74 $stream = $stream->decode;
75 $stream = $stream->decode('iso-8859-1');
76
77 Decode bytestream with "decode" in Mojo::Util, defaults to using
78 "UTF-8".
79
80 # "♥"
81 b('%E2%99%A5')->url_unescape->decode;
82
83 encode
84 $stream = $stream->encode;
85 $stream = $stream->encode('iso-8859-1');
86
87 Encode bytestream with "encode" in Mojo::Util, defaults to using
88 "UTF-8".
89
90 # "%E2%99%A5"
91 b('♥')->encode->url_escape;
92
93 gunzip
94 $stream = $stream->gunzip;
95
96 Uncompress bytestream with "gunzip" in Mojo::Util.
97
98 gzip
99 stream = $stream->gzip;
100
101 Compress bytestream with "gzip" in Mojo::Util.
102
103 hmac_sha1_sum
104 $stream = $stream->hmac_sha1_sum('passw0rd');
105
106 Generate HMAC-SHA1 checksum for bytestream with "hmac_sha1_sum" in
107 Mojo::Util.
108
109 # "7fbdc89263974a89210ea71f171c77d3f8c21471"
110 b('foo bar baz')->hmac_sha1_sum('secr3t');
111
112 html_unescape
113 $stream = $stream->html_unescape;
114
115 Unescape all HTML entities in bytestream with "html_unescape" in
116 Mojo::Util.
117
118 # "%3Chtml%3E"
119 b('<html>')->html_unescape->url_escape;
120
121 md5_bytes
122 $stream = $stream->md5_bytes;
123
124 Generate binary MD5 checksum for bytestream with "md5_bytes" in
125 Mojo::Util.
126
127 md5_sum
128 $stream = $stream->md5_sum;
129
130 Generate MD5 checksum for bytestream with "md5_sum" in Mojo::Util.
131
132 new
133 my $stream = Mojo::ByteStream->new('test123');
134
135 Construct a new scalar-based Mojo::ByteStream object.
136
137 punycode_decode
138 $stream = $stream->punycode_decode;
139
140 Punycode decode bytestream with "punycode_decode" in Mojo::Util.
141
142 punycode_encode
143 $stream = $stream->punycode_encode;
144
145 Punycode encode bytestream with "punycode_encode" in Mojo::Util.
146
147 quote
148 $stream = $stream->quote;
149
150 Quote bytestream with "quote" in Mojo::Util.
151
152 say
153 $stream = $stream->say;
154 $stream = $stream->say(*STDERR);
155
156 Print bytestream to handle and append a newline, defaults to using
157 "STDOUT".
158
159 secure_compare
160 my $bool = $stream->secure_compare($str);
161
162 Compare bytestream with "secure_compare" in Mojo::Util.
163
164 sha1_bytes
165 $stream = $stream->sha1_bytes;
166
167 Generate binary SHA1 checksum for bytestream with "sha1_bytes" in
168 Mojo::Util.
169
170 sha1_sum
171 $stream = $stream->sha1_sum;
172
173 Generate SHA1 checksum for bytestream with "sha1_sum" in Mojo::Util.
174
175 size
176 my $size = $stream->size;
177
178 Size of bytestream.
179
180 slugify
181 $stream = $stream->slugify;
182 $stream = $stream->slugify($bool);
183
184 Generate URL slug for bytestream with "slugify" in Mojo::Util.
185
186 split
187 my $collection = $stream->split(',');
188
189 Turn bytestream into Mojo::Collection object containing
190 Mojo::ByteStream objects.
191
192 # "One,Two,Three"
193 b("one,two,three")->split(',')->map('camelize')->join(',');
194
195 tap
196 $stream = $stream->tap(sub {...});
197
198 Alias for "tap" in Mojo::Base.
199
200 term_escape
201 $stream = $stream->term_escape;
202
203 Escape POSIX control characters in bytestream with "term_escape" in
204 Mojo::Util.
205
206 # Print binary checksum to terminal
207 b('foo')->sha1_bytes->term_escape->say;
208
209 to_string
210 my $str = $stream->to_string;
211
212 Stringify bytestream.
213
214 trim
215 $stream = $stream->trim;
216
217 Trim whitespace characters from both ends of bytestream with "trim" in
218 Mojo::Util.
219
220 unindent
221 $stream = $stream->unindent;
222
223 Unindent bytestream with "unindent" in Mojo::Util.
224
225 unquote
226 $stream = $stream->unquote;
227
228 Unquote bytestream with "unquote" in Mojo::Util.
229
230 url_escape
231 $stream = $stream->url_escape;
232 $stream = $stream->url_escape('^A-Za-z0-9\-._~');
233
234 Percent encode all unsafe characters in bytestream with "url_escape" in
235 Mojo::Util.
236
237 # "%E2%98%83"
238 b('☃')->encode->url_escape;
239
240 url_unescape
241 $stream = $stream->url_unescape;
242
243 Decode percent encoded characters in bytestream with "url_unescape" in
244 Mojo::Util.
245
246 # "<html>"
247 b('%3Chtml%3E')->url_unescape->xml_escape;
248
249 with_roles
250 my $new_class = Mojo::ByteStream->with_roles('Mojo::ByteStream::Role::One');
251 my $new_class = Mojo::ByteStream->with_roles('+One', '+Two');
252 $stream = $stream->with_roles('+One', '+Two');
253
254 Alias for "with_roles" in Mojo::Base.
255
256 xml_escape
257 $stream = $stream->xml_escape;
258
259 Escape only the characters "&", "<", ">", """ and "'" in bytestream
260 with "xml_escape" in Mojo::Util.
261
262 xor_encode
263 $stream = $stream->xor_encode($key);
264
265 XOR encode bytestream with "xor_encode" in Mojo::Util.
266
267 # "%04%0E%15B%03%1B%10"
268 b('foo bar')->xor_encode('baz')->url_escape;
269
271 Mojo::ByteStream overloads the following operators.
272
273 bool
274 my $bool = !!$bytestream;
275
276 Always true.
277
278 stringify
279 my $str = "$bytestream";
280
281 Alias for "to_string".
282
284 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
285
286
287
288perl v5.28.0 2018-09-29 Mojo::ByteStream(3)