1perl5ifaq(3) User Contributed Perl Documentation perl5ifaq(3)
2
3
4
6 perl5ifaq - Frequently Asked Questions about perl5i
7
9 What is perl5i?
10 perl5i is a Perl module. It is short for "Perl 5 + i". The "+ i"
11 indicates a complex number, going off in a different direction than
12 traditional Perl 5 development.
13
14 What's the point of perl5i?
15 perl5i is also about getting the defaults righter. Make Perl 5 DWIM
16 better, without having to load a dozen Perl modules.
17
18 perl5i was originally conceived after Schwern had a conversation with a
19 Ruby programmer who had a job writing Perl. He was listening to their
20 complaints about Perl and providing better solutions where possible.
21 What he found was a lot of the solutions were "go get this module from
22 CPAN" or "turn on this pragma". "use strict", "use warnings", "use
23 autodie", "use autobox", "use List::Util", "use DateTime".
24
25 Not only does this cause you to litter your code with a dozen use
26 statements, it also requires tribal knowledge not necessarily available
27 to the new programmer. To use Perl 5 well requires an experienced Perl
28 programmer looking over your shoulder, giving you advice.
29
30 For the experienced Perl 5 programmer, using perl5i means writing less
31 boilerplate code. It means not having to decide between doing it the
32 right way and doing it the convenient way. For example, you probably
33 should be using a proper exception handling module, but which one? And
34 then you have to get it and remember to load it. perl5i gives you
35 "try" and "catch" that are always there, there's no excuse not to do it
36 right.
37
38 In some ways, perl5i is "The Best of CPAN" in code form.
39
40 What's perl5i's relation to Perl 6?
41 perl5i steals liberally from Perl 6.
42
43 perl5i is not intended as a competitor to Perl 6 nor an abandonment.
44 But it's going to be a while before Perl 6 is production stable and
45 we've got to get some work done before Christmas.
46
47 What is perl5i's relation to Perl 5?
48 perl5i is in some ways a release valve for the frustration surrounding
49 Perl 5 development, particularly with regard to Perl 5's conservative
50 backwards compatibility requirements. Patching Perl 5 is also out of
51 the league of most Perl programmers (and a lot of C programmers).
52 Can't get a feature into Perl 5? Put it in perl5i.
53
54 Since it has a liberal compatibility policy, perl5i serves as a testing
55 ground for new features. It will let the community try out new
56 concepts in the wild and see how it works out. For example, autoboxing
57 has been available for years but was rejected from incusion in Perl 5
58 in part because Perl 5 programmers do not grok the benefits of
59 everything being an object.
60
61 Is perl5i intended for production?
62 Yes, the API is stable and its well tested. Its effects are mostly
63 lexical and incompatibilities are with obscure "features" that you're
64 probably not using.
65
66 Rather than reinventing the wheel, perl5i is mostly a wrapper around
67 stable, well understood CPAN modules. It avoids unstable magic.
68
69 perl5i's interface is NOT compatible between major versions, but fear
70 not! perl5i has an intentional backwards incompatibility plan so that
71 code written for one version will continue to work even after you
72 upgrade. Please read "Using perl5i" in perl5i for details.
73
74 What's perl5i's performance like?
75 perl5i tries to make you only pay for what you use. It delays loading
76 most modules to keep startup time reasonable.
77
78 While we've been watching perl5i's weight, serious performance
79 optimization has not begun. Interface and correctness take priority.
80
81 Autoboxed methods carry a run-time performance penalty similar to a
82 normal method call. In general, because perl5i has to wrap much of
83 Perl 5 it will run slower. Whether this actually effects the
84 performance of your app should be determined by profiling your entire
85 app and not just benchmarking individual operators.
86
87 perl5i's true performance comes out in helping the programmer write
88 code faster and more consistently with less hand written code for
89 common tasks. In some cases we've discovered perl5i works faster than
90 the equivalent hand coded solution because perl5i can take advantage of
91 very clever CPAN modules written in XS. Of course, you can do that
92 without perl5i but we've done the research for you.
93
95 Here are some ways to do traditional Perl 5 things the perl5i way.
96
97 How do I tell if something is a number?
98 $thing->is_number; # it's something Perl thinks is a number
99 $thing->is_positive; # it's a positive number
100 $thing->is_negative; # it's a negative number
101 $thing->is_integer; # it's an integer, no decimal part
102 $thing->is_even; # it's an even integer
103 $thing->is_odd; # it's an odd integer
104 $thing->is_decimal; # it's a decimal number
105
106 This will work even if $thing is a reference (they will all return
107 false).
108
109 How do I get the difference between two arrays?
110 my @diff = @array1->diff(\@array2);
111
112 Will return the elements in @array1 which are not in @array2.
113
114 How do I merge two hashes?
115 If you don't mind overwriting one hash, and want to do a shallow merge,
116 then use a hash slice.
117
118 @hash1{ keys %hash2 } = values %hash2;
119
120 If you want to do a shallow copy but want to preserve the original
121 hashes, copy the first hash and then do the hash slice technique.
122
123 my %merged = %hash1;
124 @merged{ keys %hash2 } = values %hash2;
125
126 If you want to do a recursive merge, merging any subhashes, use the
127 "merge" method.
128
129 my %hash1 = ( a => 1, b => { foo => 23 } );
130 my %hash2 = ( a => 100, b => { bar => 42 } );
131
132 # %hash1 is now ( a => 100, b => { foo => 23, bar => 42 } )
133 %hash1->merge(\%hash2);
134
135 How can I get the unique keys from multiple hashes?
136 If the hashes are small, extract the keys into an array and use the
137 "uniq" method.
138
139 my @keys = (%hash1->keys, %hash2->keys);
140 my @uniq = @keys->uniq;
141
142 If the hashes contain a lot of keys, you can save memory by not
143 building the intermediate @keys.
144
145 my %seen;
146 for my $hash (\%hash1, \%hash2) {
147 $hash->each( func($key) {
148 $seen{$key} = 1;
149 });
150 }
151
152 my @uniq = %seen->keys;
153
154 How do I iterate through an array more than one at a time?
155 Pass the "foreach" method a function which takes more than one
156 parameter. "foreach" will iterate over the appropriate number of
157 items.
158
159 # Iterate two at a time.
160 @array->foreach( func($x,$y) { say "x: $x, y: $y" };
161
162 See "foreach" in perl5i for details.
163
164 How do I get information about the current date?
165 localtime(), gmtime() and time() all return DateTime objects in scalar
166 context.
167
168 No more mucking around with "$year += 1900". It's simply:
169
170 my $now = localtime;
171 my $year = $now->year;
172
173 Or even:
174
175 my $year = localtime->year;
176
177 The name of the current month can be gotten with:
178
179 my $month_name = localtime->month_name;
180
181 You have the full range of DateTime features available.
182
183 How do I alias a variable?
184 You call the "alias()" method on the variable you want to alias.
185
186 Here's an example turning an anonymous subroutine into a named method.
187
188 my $class = "Some::Class";
189 my $name = "method_name";
190 my $code = sub { ... };
191 $code->alias($class, $name);
192
193 "Some::Class->method_name" will now call the $code.
194
195 This works for arrays, hashes and scalars. See "alias()" in perl5i for
196 details.
197
198 How do I use a module from a variable?
199 Call the require method on that variable.
200
201 my $module = "Some::Module";
202 $module->require;
203
204 If you want to import symbols, you can call import as well.
205
206 $module->require->import;
207
208 See "require" in perl5i for details.
209
210 How do I strip whitespace off a string?
211 Use the "trim()" method.
212
213 my $string = " some stuff ";
214 $string = $string->trim; # $string is now "some stuff"
215
216 See "trim()" in perl5i for details.
217
218 How do I find information about my caller?
219 "caller()" returns an object in scalar context which you can query for
220 information.
221
222 my $caller = caller();
223 printf "Something something something dark side at %s line %d.\n",
224 $caller->filename, $caller->line;
225
226 How do I write my code in UTF8?
227 perl5i enables UTF8 processing of code, arguments, strings and
228 filehandles. Working with UTF8 should just work.
229
230 How do I read/write a non-UTF8 file?
231 Since all filehandles are treated as UTF8, if you want to work on
232 non-UTF8 data you will have to say so explicitly. Usually this
233 involves calling "binmode" on the filehandle.
234
235 Here's an example of writing an image file.
236
237 open my $fh, ">", $image_file;
238 binmode $fh;
239 print $fh $image_data;
240
241 Here's an example of Latin-1.
242
243 open my $fh, ">", $file;
244 binmode $fh, ":encoding(Latin-1)";
245 print $fh $text;
246
247 If UTF8 is not to your liking you can switch the default encoding of
248 newly opened filehandles with the "open" pragma.
249
250 use open ":encoding(Latin-1)"; # new filehandles will be Latin-1
251 use open ":std"; # so will STDOUT, STDERR and STDIN
252
253 See "utf8" in perl5i for details.
254
255 How do I get the name of the current class?
256 The $CLASS variable and CLASS constant are exported by perl5i and it
257 contains the name of the current class.
258
259 CLASS->class_method(@args);
260 say "OMG! You're using class $CLASS.";
261
262 See "CLASS" in per5i for details.
263
264 How do I get the current directory?
265 Simply read $CWD. See "File::chdir" in perl5i for details.
266
267 How do I temporarily change the directory?
268 If a function has to change directory, it's polite to change it back
269 before returning. perl5i provides "local $CWD" to accomplish this.
270
271 sub do_things {
272 local $CWD = "some/subdir";
273 ... do unspeakable things in some/subdir ...
274 return $whatever;
275 }
276
277 chdir "/some/path";
278 do_things(); # do_things operates in /some/path/some/subdir
279 say $CWD; # prints /some/path
280
281 Even if the code in do_things() dies, it will still return to the
282 original directory.
283
284 See "File::chdir" in perl5i for details.
285
286 How do I catch an exception?
287 Use "try/catch".
288
289 try { some_code() }
290 catch { warn "some_code() didn't work because: $_" };
291
292 See "Try::Tiny" in perl5i for details.
293
294 How do I get the output of "system"?
295 Use "capture".
296
297 my $output = capture {
298 system "command", "and", "some", "arguments";
299 };
300
301 See "capture()" in perl5i.
302
303 How can I capture STDERR?
304 Use "capture".
305
306 my($stdout, $stderr) = capture {
307 ...anything run in here will have STDOUT and STDERR captured.
308 };
309
310 This will capture "STDOUT" and "STDERR" separately. To capture them
311 together in one variable, use the "merge" option.
312
313 my $output = capture {
314 ...anything run in here will have STDOUT and STDERR captured...
315 } merge => 1;
316
317 See "capture()" in perl5i.
318
319 How can I call backticks without shell processing?
320 You can't. What you can do instead is use "capture" and "system" with
321 multiple arguments.
322
323 my $output = capture {
324 system $command, @options;
325 };
326
327 See "capture()" in perl5i.
328
329 How do I make my distribution depend on perl5i?
330 perl5i is not backwards compatible across major versions. This is why
331 when you use perl5i you use a major version such as "use perl5i::2".
332 This guarantees that code you write will continue to work even after
333 perl5i has changed.
334
335 When depending on perl5i, depend on the specific major version. That
336 is, depend on "perl5i::2" and not "perl5i". This is because older
337 versions will eventually be spun out into their own separate
338 distributions to avoid cluttering the main dist. If you depend on
339 "perl5i::2" then the CPAN shell will always be able to find it.
340
341 How do I make perlcritic recognize perl5i?
342 perl5i turns on strict and warnings, but by default perlcritic does not
343 recognize this. You can add perl5i to the default set of modules in
344 your .perlcriticrc.
345
346 [TestingAndDebugging::RequireUseWarnings]
347 equivalent_modules = perl5i::2
348
349 [TestingAndDebugging::RequireUseStrict]
350 equivalent_modules = perl5i::2
351
353 Where can I find out more about perl5i?
354 You can follow perl5i development on Twitter at
355 <http://twitter.com/perl5i>, our Github page at
356 <https://github.com/evalEmpire/perl5i> and wiki at
357 <https://github.com/evalEmpire/perl5i/wiki>. Discussions on IRC are on
358 <irc://irc.perl.org> on channel #perl5i.
359
360 I have a great idea I want to add! How can I help?
361 Wonderful! Let us know. The best way is to create an issue in the
362 issue tracker at <http://github.com/evalEmpire/perl5i/issues>. Think
363 of it less as an issue tracker and more of a web forum with great
364 tagging.
365
366 What is particularly useful to perl5i is to hear about problems you'd
367 like solved. Tell us about a simple problem that you had to write too
368 much code to solve, or load too many modules, or that had too many
369 caveats.
370
371 Finally, if you just want to write some code, you can fork and work on
372 it at <http://github.com/evalEmpire/perl5i>. Full details on our
373 patching policy can be read at
374 <http://github.com/evalEmpire/perl5i/raw/master/PATCHING>.
375
376 We'd like to hear from you. Don't worry if you're doing it right, come
377 talk with us.
378
379 Why doesn't perl5i use Moose?
380 We'd love to, but Moose more than doubles perl5i's startup time.
381
382 In addition, simply using Moose doesn't buy you much. Like perl5i, it
383 is one line to fix much of Perl's OO woes. But even Moose needs
384 fixing. What we would really like is to be able to conditionally use
385 MooseX::Declare which fixes Perl's OO syntax as well as provides some
386 better Moose defaults. But that has the double whammy of using
387 Devel::Declare and Moose.
388
389 Why doesn't perl5i use Class::MOP?
390 Class::MOP is more about method declaration and dispatch. Our meta-
391 object is more about things you want to do to every object but don't
392 want to pollute the UNIVERSAL namespace with.
393
394 perl5i has too many dependencies!
395 That's not a question. Eventually, perl5i will look into a bundling
396 solution to ease the dependency hell it's rapidly descending into. In
397 general we've favored using a CPAN module over writing it ourselves so
398 maintenance can be distributed.
399
400 We monitor the health of our dependencies and try to pick ones which
401 are solid or fix those which fail too often.
402
403
404
405perl v5.30.0 2019-07-26 perl5ifaq(3)