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 qw(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 humanize_bytes
122 $stream = $stream->humanize_bytes;
123
124 Turn number of bytes into a simplified human readable format for
125 bytestream with "humanize_bytes" in Mojo::Util.
126
127 md5_bytes
128 $stream = $stream->md5_bytes;
129
130 Generate binary MD5 checksum for bytestream with "md5_bytes" in
131 Mojo::Util.
132
133 md5_sum
134 $stream = $stream->md5_sum;
135
136 Generate MD5 checksum for bytestream with "md5_sum" in Mojo::Util.
137
138 new
139 my $stream = Mojo::ByteStream->new('test123');
140
141 Construct a new scalar-based Mojo::ByteStream object.
142
143 punycode_decode
144 $stream = $stream->punycode_decode;
145
146 Punycode decode bytestream with "punycode_decode" in Mojo::Util.
147
148 punycode_encode
149 $stream = $stream->punycode_encode;
150
151 Punycode encode bytestream with "punycode_encode" in Mojo::Util.
152
153 quote
154 $stream = $stream->quote;
155
156 Quote bytestream with "quote" in Mojo::Util.
157
158 say
159 $stream = $stream->say;
160 $stream = $stream->say(*STDERR);
161
162 Print bytestream to handle and append a newline, defaults to using
163 "STDOUT".
164
165 secure_compare
166 my $bool = $stream->secure_compare($str);
167
168 Compare bytestream with "secure_compare" in Mojo::Util.
169
170 sha1_bytes
171 $stream = $stream->sha1_bytes;
172
173 Generate binary SHA1 checksum for bytestream with "sha1_bytes" in
174 Mojo::Util.
175
176 sha1_sum
177 $stream = $stream->sha1_sum;
178
179 Generate SHA1 checksum for bytestream with "sha1_sum" in Mojo::Util.
180
181 size
182 my $size = $stream->size;
183
184 Size of bytestream.
185
186 slugify
187 $stream = $stream->slugify;
188 $stream = $stream->slugify($bool);
189
190 Generate URL slug for bytestream with "slugify" in Mojo::Util.
191
192 split
193 my $collection = $stream->split(',');
194 my $collection = $stream->split(',', -1);
195
196 Turn bytestream into Mojo::Collection object containing
197 Mojo::ByteStream objects.
198
199 # "One,Two,Three"
200 b("one,two,three")->split(',')->map('camelize')->join(',');
201
202 # "One,Two,Three,,,"
203 b("one,two,three,,,")->split(',', -1)->map('camelize')->join(',');
204
205 tap
206 $stream = $stream->tap(sub {...});
207
208 Alias for "tap" in Mojo::Base.
209
210 term_escape
211 $stream = $stream->term_escape;
212
213 Escape POSIX control characters in bytestream with "term_escape" in
214 Mojo::Util.
215
216 # Print binary checksum to terminal
217 b('foo')->sha1_bytes->term_escape->say;
218
219 to_string
220 my $str = $stream->to_string;
221
222 Stringify bytestream.
223
224 trim
225 $stream = $stream->trim;
226
227 Trim whitespace characters from both ends of bytestream with "trim" in
228 Mojo::Util.
229
230 unindent
231 $stream = $stream->unindent;
232
233 Unindent bytestream with "unindent" in Mojo::Util.
234
235 unquote
236 $stream = $stream->unquote;
237
238 Unquote bytestream with "unquote" in Mojo::Util.
239
240 url_escape
241 $stream = $stream->url_escape;
242 $stream = $stream->url_escape('^A-Za-z0-9\-._~');
243
244 Percent encode all unsafe characters in bytestream with "url_escape" in
245 Mojo::Util.
246
247 # "%E2%98%83"
248 b('☃')->encode->url_escape;
249
250 url_unescape
251 $stream = $stream->url_unescape;
252
253 Decode percent encoded characters in bytestream with "url_unescape" in
254 Mojo::Util.
255
256 # "<html>"
257 b('%3Chtml%3E')->url_unescape->xml_escape;
258
259 with_roles
260 my $new_class = Mojo::ByteStream->with_roles('Mojo::ByteStream::Role::One');
261 my $new_class = Mojo::ByteStream->with_roles('+One', '+Two');
262 $stream = $stream->with_roles('+One', '+Two');
263
264 Alias for "with_roles" in Mojo::Base.
265
266 xml_escape
267 $stream = $stream->xml_escape;
268
269 Escape only the characters "&", "<", ">", """ and "'" in bytestream
270 with "xml_escape" in Mojo::Util.
271
272 xor_encode
273 $stream = $stream->xor_encode($key);
274
275 XOR encode bytestream with "xor_encode" in Mojo::Util.
276
277 # "%04%0E%15B%03%1B%10"
278 b('foo bar')->xor_encode('baz')->url_escape;
279
281 Mojo::ByteStream overloads the following operators.
282
283 bool
284 my $bool = !!$bytestream;
285
286 Always true.
287
288 stringify
289 my $str = "$bytestream";
290
291 Alias for "to_string".
292
294 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
295
296
297
298perl v5.38.0 2023-09-11 Mojo::ByteStream(3)