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