1Mojo::ByteStream(3)   User Contributed Perl Documentation  Mojo::ByteStream(3)
2
3
4

NAME

6       Mojo::ByteStream - ByteStream
7

SYNOPSIS

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

DESCRIPTION

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

FUNCTIONS

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

METHODS

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         my $collection = $stream->split(',', -1);
189
190       Turn bytestream into Mojo::Collection object containing
191       Mojo::ByteStream objects.
192
193         # "One,Two,Three"
194         b("one,two,three")->split(',')->map('camelize')->join(',');
195
196         # "One,Two,Three,,,"
197         b("one,two,three,,,")->split(',', -1)->map('camelize')->join(',');
198
199   tap
200         $stream = $stream->tap(sub {...});
201
202       Alias for "tap" in Mojo::Base.
203
204   term_escape
205         $stream = $stream->term_escape;
206
207       Escape POSIX control characters in bytestream with "term_escape" in
208       Mojo::Util.
209
210         # Print binary checksum to terminal
211         b('foo')->sha1_bytes->term_escape->say;
212
213   to_string
214         my $str = $stream->to_string;
215
216       Stringify bytestream.
217
218   trim
219         $stream = $stream->trim;
220
221       Trim whitespace characters from both ends of bytestream with "trim" in
222       Mojo::Util.
223
224   unindent
225         $stream = $stream->unindent;
226
227       Unindent bytestream with "unindent" in Mojo::Util.
228
229   unquote
230         $stream = $stream->unquote;
231
232       Unquote bytestream with "unquote" in Mojo::Util.
233
234   url_escape
235         $stream = $stream->url_escape;
236         $stream = $stream->url_escape('^A-Za-z0-9\-._~');
237
238       Percent encode all unsafe characters in bytestream with "url_escape" in
239       Mojo::Util.
240
241         # "%E2%98%83"
242         b('☃')->encode->url_escape;
243
244   url_unescape
245         $stream = $stream->url_unescape;
246
247       Decode percent encoded characters in bytestream with "url_unescape" in
248       Mojo::Util.
249
250         # "<html>"
251         b('%3Chtml%3E')->url_unescape->xml_escape;
252
253   with_roles
254         my $new_class = Mojo::ByteStream->with_roles('Mojo::ByteStream::Role::One');
255         my $new_class = Mojo::ByteStream->with_roles('+One', '+Two');
256         $stream       = $stream->with_roles('+One', '+Two');
257
258       Alias for "with_roles" in Mojo::Base.
259
260   xml_escape
261         $stream = $stream->xml_escape;
262
263       Escape only the characters "&", "<", ">", """ and "'" in bytestream
264       with "xml_escape" in Mojo::Util.
265
266   xor_encode
267         $stream = $stream->xor_encode($key);
268
269       XOR encode bytestream with "xor_encode" in Mojo::Util.
270
271         # "%04%0E%15B%03%1B%10"
272         b('foo bar')->xor_encode('baz')->url_escape;
273

OPERATORS

275       Mojo::ByteStream overloads the following operators.
276
277   bool
278         my $bool = !!$bytestream;
279
280       Always true.
281
282   stringify
283         my $str = "$bytestream";
284
285       Alias for "to_string".
286

SEE ALSO

288       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
289
290
291
292perl v5.30.1                      2020-01-30               Mojo::ByteStream(3)
Impressum