line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package perl5i; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
###################################### |
4
|
|
|
|
|
|
|
# The real code is in perl5i::2 # |
5
|
|
|
|
|
|
|
# Please patch that # |
6
|
|
|
|
|
|
|
###################################### |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
561
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
36
|
|
9
|
1
|
|
|
1
|
|
3
|
use parent 'perl5i::latest'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
58
|
use perl5i::VERSION; our $VERSION = perl5i::VERSION->VERSION; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
109
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $Latest = perl5i::VERSION->latest; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub import { |
16
|
1
|
50
|
|
1
|
|
485
|
if ($0 eq '-e') { |
17
|
0
|
|
|
|
|
0
|
goto &perl5i::latest::import; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
else { |
20
|
1
|
|
|
|
|
4
|
require Carp::Fix::1_25; |
21
|
1
|
|
|
|
|
223
|
Carp::Fix::1_25::croak(<<END); |
22
|
|
|
|
|
|
|
perl5i will break compatibility in the future, you can't just "use perl5i". |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Instead, "use $Latest" which will guarantee compatibility with all |
25
|
|
|
|
|
|
|
features supplied in that major version. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Type "perldoc perl5i" for details in the section "Using perl5i". |
28
|
|
|
|
|
|
|
END |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
1; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
__END__ |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=encoding utf8 |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 NAME |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
perl5i - Fix as much of Perl 5 as possible in one pragma |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 SYNOPSIS |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
use perl5i::2; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
or |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
$ perl5i your_script.pl |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 DESCRIPTION |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
Perl 5 has a lot of warts. There's a lot of individual modules and |
53
|
|
|
|
|
|
|
techniques out there to fix those warts. perl5i aims to pull the best |
54
|
|
|
|
|
|
|
of them together into one module so you can turn them on all at once. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
This includes adding features, changing existing core functions and |
57
|
|
|
|
|
|
|
changing defaults. It will likely not be 100% backwards compatible |
58
|
|
|
|
|
|
|
with Perl 5, though it will be 99%, perl5i will try to have a lexical |
59
|
|
|
|
|
|
|
effect. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
Please add to this imaginary world and help make it real, either by |
62
|
|
|
|
|
|
|
telling me what Perl looks like in your imagination |
63
|
|
|
|
|
|
|
(F<http://github.com/evalEmpire/perl5i/issues>) or make a fork (forking on |
64
|
|
|
|
|
|
|
github is like a branch you control) and implement it yourself. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 Rationale |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Changing perl 5 core is a slow and difficult process. |
70
|
|
|
|
|
|
|
Perl 5 aims to be compatible with ancient versions which means it is |
71
|
|
|
|
|
|
|
mostly stuck with design, decisions and defaults made way back in |
72
|
|
|
|
|
|
|
the 90's. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
There are modules in CPAN to solve or ease many of those issues but |
75
|
|
|
|
|
|
|
many people don't know about them or don't know which ones to use. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Documentation and books are updated slowly and don't usually keep up; |
78
|
|
|
|
|
|
|
this information becomes some sort of community knowledge, invisible |
79
|
|
|
|
|
|
|
from the wider audience. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Even if you know a solution, having to decide everytime which |
82
|
|
|
|
|
|
|
module to use and enable it individually might be enough for you to |
83
|
|
|
|
|
|
|
give up and just do things the old way. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Perl5i brings all this community knowledge in a coherent way, in |
86
|
|
|
|
|
|
|
something like 'the best of CPAN', enabled with a single command. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
You don't need to know all it does nor how it does it, you just |
89
|
|
|
|
|
|
|
C<use perl5i::2> on your code and you automatically get a modern |
90
|
|
|
|
|
|
|
environment, with perl defaults, problems and inconsistencies fixed. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
You can refer beginers to perl5i and they can benefit from it without |
93
|
|
|
|
|
|
|
needing to become a perl guru first. |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 Using perl5i |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Because perl5i I<plans> to be incompatible in the future, you do not |
99
|
|
|
|
|
|
|
simply C<use perl5i>. You must declare which major version of perl5i |
100
|
|
|
|
|
|
|
you are using. You do this like so: |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Use perl5i major version 2 |
103
|
|
|
|
|
|
|
use perl5i::2; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Thus the code you write with, for example, C<perl5i::2> will always |
106
|
|
|
|
|
|
|
remain compatible even as perl5i moves on. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
If you want to be daring, you can C<use perl5i::latest> to get the |
109
|
|
|
|
|
|
|
latest version. This will automatically happen if the program is C<-e>. |
110
|
|
|
|
|
|
|
This lets you do slightly less typing for one-liners like C<perl -Mperl5i -e ...> |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
If you want your module to depend on perl5i, you should depend on the |
113
|
|
|
|
|
|
|
versioned class. For example, depend on C<perl5i::2> and not |
114
|
|
|
|
|
|
|
C<perl5i>. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
See L</VERSIONING> for more information about perl5i's versioning |
117
|
|
|
|
|
|
|
scheme. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 What it does |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
perl5i enables each of these modules and adds/changes these functions. |
123
|
|
|
|
|
|
|
We'll provide a brief description here, but you should look at each of |
124
|
|
|
|
|
|
|
their documentation for full details. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 The Meta Object |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Every object (and everything is an object) now has a meta object |
130
|
|
|
|
|
|
|
associated with it. Using the meta object you can ask things about |
131
|
|
|
|
|
|
|
the object which were previously over complicated. For example... |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# the object's class |
134
|
|
|
|
|
|
|
my $class = $obj->mo->class; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# its parent classes |
137
|
|
|
|
|
|
|
my @isa = $obj->mo->isa; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
# the complete inheritance hierarchy |
140
|
|
|
|
|
|
|
my @complete_isa = $obj->mo->linear_isa; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# the reference type of the object |
143
|
|
|
|
|
|
|
my $reftype = $obj->mo->reftype; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
A meta object is used to avoid polluting the global method space. |
146
|
|
|
|
|
|
|
C<mo> was chosen to avoid clashing with Moose's meta object. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
See L<perl5i::Meta> for complete details. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head2 Subroutine and Method Signatures |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
perl5i makes it easier to declare what parameters a subroutine takes. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
func hello($place) { |
156
|
|
|
|
|
|
|
say "Hello, $place!\n"; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
method get($key) { |
160
|
|
|
|
|
|
|
return $self->{$key}; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
method new($class: %args) { |
164
|
|
|
|
|
|
|
return bless \%args, $class; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
C<func> and C<method> define subroutines as C<sub> does, with some |
168
|
|
|
|
|
|
|
extra conveniences. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
The signature syntax is currently very simple. The content will be |
171
|
|
|
|
|
|
|
assigned from @_. This: |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
func add($this, $that) { |
174
|
|
|
|
|
|
|
return $this + $that; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
is equivalent to: |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub add { |
180
|
|
|
|
|
|
|
my($this, $that) = @_; |
181
|
|
|
|
|
|
|
return $this + $that; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
C<method> defines a method. This is the same as a subroutine, but the |
185
|
|
|
|
|
|
|
first argument, the I<invocant>, will be removed and made into |
186
|
|
|
|
|
|
|
C<$self>. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
method get($key) { |
189
|
|
|
|
|
|
|
return $self->{$key}; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub get { |
193
|
|
|
|
|
|
|
my $self = shift; |
194
|
|
|
|
|
|
|
my($key) = @_; |
195
|
|
|
|
|
|
|
return $self->{$key}; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Methods have a special bit of syntax. If the first item in the |
199
|
|
|
|
|
|
|
siganture is C<$var:> it will change the variable used to store the |
200
|
|
|
|
|
|
|
invocant. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
method new($class: %args) { |
203
|
|
|
|
|
|
|
return bless \%args, $class; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
is equivalent to: |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
sub new { |
209
|
|
|
|
|
|
|
my $class = shift; |
210
|
|
|
|
|
|
|
my %args = @_; |
211
|
|
|
|
|
|
|
return bless \%args, $class; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
Anonymous functions and methods work, too. |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
my $code = func($message) { say $message }; |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
Guarantees include: |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
@_ will not be modified except by removing the invocant |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Future versions of perl5i will add to the signature syntax and |
223
|
|
|
|
|
|
|
capabilities. Planned expansions include: |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
Signature validation |
226
|
|
|
|
|
|
|
Signature documentation |
227
|
|
|
|
|
|
|
Named parameters |
228
|
|
|
|
|
|
|
Required parameters |
229
|
|
|
|
|
|
|
Read only parameters |
230
|
|
|
|
|
|
|
Aliased parameters |
231
|
|
|
|
|
|
|
Anonymous method and function declaration |
232
|
|
|
|
|
|
|
Variable method and function names |
233
|
|
|
|
|
|
|
Parameter traits |
234
|
|
|
|
|
|
|
Traditional prototypes |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
See L<http://github.com/evalEmpire/perl5i/issues/labels/syntax#issue/19> for |
237
|
|
|
|
|
|
|
more details about future expansions. |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
The equivalencies above should only be taken for illustrative |
240
|
|
|
|
|
|
|
purposes, they are not guaranteed to be literally equivalent. |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
Note that while all parameters are optional by default, the number of |
243
|
|
|
|
|
|
|
parameters will eventually be enforced. For example, right now this |
244
|
|
|
|
|
|
|
will work: |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
func add($this, $that) { return $this + $that } |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
say add(1,2,3); # says 3 |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
The extra argument is ignored. In future versions of perl5i this will |
251
|
|
|
|
|
|
|
be a runtime error. |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=head3 Signature Introspection |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
The signature of a subroutine defined with C<func> or C<method> can be |
257
|
|
|
|
|
|
|
queried by calling the C<signature> method on the code reference. |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
func hello($greeting, $place) { say "$greeting, $place" } |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
my $code = \&hello; |
262
|
|
|
|
|
|
|
say $code->signature->num_positional_params; # prints 2 |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
Functions defined with C<sub> will not have a signature. |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
See L<perl5i::Signature> for more details. |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=head2 Autoboxing |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
L<autobox> allows methods to be defined for and called on most |
272
|
|
|
|
|
|
|
unblessed variables. This means you can call methods on ordinary |
273
|
|
|
|
|
|
|
strings, lists and hashes! It also means perl5i can add a lot of |
274
|
|
|
|
|
|
|
functionality without polluting the global namespace. |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
L<autobox::Core> wraps a lot of Perl's built in functions so they can |
277
|
|
|
|
|
|
|
be called as methods on unblessed variables. C<< @a->pop >> for example. |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=head3 alias |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
$scalar_reference->alias( @identifiers ); |
282
|
|
|
|
|
|
|
@alias->alias( @identifiers ); |
283
|
|
|
|
|
|
|
%hash->alias( @identifiers ); |
284
|
|
|
|
|
|
|
(\&code)->alias( @identifiers ); |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
Aliases a variable to a new global name. |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
my $code = sub { 42 }; |
289
|
|
|
|
|
|
|
$code->alias( "foo" ); |
290
|
|
|
|
|
|
|
say foo(); # prints 42 |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
It will work on everything except scalar references. |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
our %stuff; |
295
|
|
|
|
|
|
|
%other_hash->alias( "stuff" ); # %stuff now aliased to %other_hash |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
It is not a copy, changes to one will change the other. |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
my %things = (foo => 23); |
300
|
|
|
|
|
|
|
our %stuff; |
301
|
|
|
|
|
|
|
%things->alias( "stuff" ); # alias %things to %stuff |
302
|
|
|
|
|
|
|
$stuff{foo} = 42; # change %stuff |
303
|
|
|
|
|
|
|
say $things{foo}; # and it will show up in %things |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
Multiple @identifiers will be joined with '::' and used as the fully |
306
|
|
|
|
|
|
|
qualified name for the alias. |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
my $class = "Some::Class"; |
309
|
|
|
|
|
|
|
my $name = "foo"; |
310
|
|
|
|
|
|
|
sub { 99 }->alias( $class, $name ); |
311
|
|
|
|
|
|
|
say Some::Class->foo; # prints 99 |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
If there is just one @identifier and it has no "::" in it, the current |
314
|
|
|
|
|
|
|
caller will be prepended. C<< $thing->alias("name") >> is shorthand for |
315
|
|
|
|
|
|
|
C<< $thing->alias(CLASS, "name") >> |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
Due to limitations in autobox, non-reference scalars cannot be |
318
|
|
|
|
|
|
|
aliased. Alias a scalar ref instead. |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
my $thing = 23; |
321
|
|
|
|
|
|
|
$thing->alias("foo"); # error |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
my $thing = \23; |
324
|
|
|
|
|
|
|
$thing->alias("foo"); # $foo is now aliased to $thing |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
This is basically a nicer way to say: |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
no strict 'refs'; |
329
|
|
|
|
|
|
|
*{$package . '::'. $name} = $reference; |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
=head2 Scalar Autoboxing |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
All of the methods provided by L<autobox::Core> are available from perl5i. |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
in addition, perl5i adds some methods of its own. |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
=head3 path |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
my $object = $path->path; |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
Creates a L<Path::Tiny> $object for the given file or directory $path. |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
my $path = "/foo/bar/baz.txt"->path; |
345
|
|
|
|
|
|
|
my $content = $path->slurp; |
346
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
=head3 center |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
my $centered_string = $string->center($length); |
351
|
|
|
|
|
|
|
my $centered_string = $string->center($length, $character); |
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
Centers $string between $character. $centered_string will be of |
354
|
|
|
|
|
|
|
length $length. |
355
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
C<$character> defaults to " ". |
357
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
say "Hello"->center(10); # " Hello "; |
359
|
|
|
|
|
|
|
say "Hello"->center(10, '-'); # "---Hello--"; |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
C<center()> will never truncate C<$string>. If $length is less |
362
|
|
|
|
|
|
|
than C<< $string->length >> it will just return C<$string>. |
363
|
|
|
|
|
|
|
|
364
|
|
|
|
|
|
|
say "Hello"->center(4); # "Hello"; |
365
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
=head3 round |
367
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
my $rounded_number = $number->round; |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
Round to the nearest integer. |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
=head3 round_up |
373
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
=head3 ceil |
375
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
my $new_number = $number->round_up; |
377
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
Rounds the $number towards infinity. |
379
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
2.45->round_up; # 3 |
381
|
|
|
|
|
|
|
(-2.45)->round_up; # -2 |
382
|
|
|
|
|
|
|
|
383
|
|
|
|
|
|
|
ceil() is a synonym for round_up(). |
384
|
|
|
|
|
|
|
|
385
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
=head3 round_down |
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
=head3 floor |
389
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
my $new_number = $number->round_down; |
391
|
|
|
|
|
|
|
|
392
|
|
|
|
|
|
|
Rounds the $number towards negative infinity. |
393
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
2.45->round_down; # 2 |
395
|
|
|
|
|
|
|
(-2.45)->round_down; # -3 |
396
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
floor() is a synonyn for round_down(). |
398
|
|
|
|
|
|
|
|
399
|
|
|
|
|
|
|
|
400
|
|
|
|
|
|
|
=head3 is_number |
401
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
$is_a_number = $thing->is_number; |
403
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
Returns true if $thing is a number understood by Perl. |
405
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
12.34->is_number; # true |
407
|
|
|
|
|
|
|
"12.34"->is_number; # also true |
408
|
|
|
|
|
|
|
"eleven"->is_number; # false |
409
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
=head3 is_positive |
411
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
$is_positive = $thing->is_positive; |
413
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
Returns true if $thing is a positive number. |
415
|
|
|
|
|
|
|
|
416
|
|
|
|
|
|
|
0 is not positive. |
417
|
|
|
|
|
|
|
|
418
|
|
|
|
|
|
|
=head3 is_negative |
419
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
$is_negative = $thing->is_negative; |
421
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
Returns true if $thing is a negative number. |
423
|
|
|
|
|
|
|
|
424
|
|
|
|
|
|
|
0 is not negative. |
425
|
|
|
|
|
|
|
|
426
|
|
|
|
|
|
|
=head3 is_even |
427
|
|
|
|
|
|
|
|
428
|
|
|
|
|
|
|
$is_even = $thing->is_even; |
429
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
Returns true if $thing is an even integer. |
431
|
|
|
|
|
|
|
|
432
|
|
|
|
|
|
|
=head3 is_odd |
433
|
|
|
|
|
|
|
|
434
|
|
|
|
|
|
|
$is_odd = $thing->is_odd; |
435
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
Returns true if $thing is an odd integer. |
437
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
=head3 is_integer |
439
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
$is_an_integer = $thing->is_integer; |
441
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
Returns true if $thing is an integer. |
443
|
|
|
|
|
|
|
|
444
|
|
|
|
|
|
|
12->is_integer; # true |
445
|
|
|
|
|
|
|
12.34->is_integer; # false |
446
|
|
|
|
|
|
|
"eleven"->is_integer; # false |
447
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
=head3 is_int |
449
|
|
|
|
|
|
|
|
450
|
|
|
|
|
|
|
A synonym for is_integer |
451
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
=head3 is_decimal |
453
|
|
|
|
|
|
|
|
454
|
|
|
|
|
|
|
$is_a_decimal_number = $thing->is_decimal; |
455
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
Returns true if $thing is a decimal number. |
457
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
12->is_decimal; # false |
459
|
|
|
|
|
|
|
12.34->is_decimal; # true |
460
|
|
|
|
|
|
|
".34"->is_decimal; # true |
461
|
|
|
|
|
|
|
"point five"->is_decimal; # false |
462
|
|
|
|
|
|
|
|
463
|
|
|
|
|
|
|
=head3 require |
464
|
|
|
|
|
|
|
|
465
|
|
|
|
|
|
|
my $module = $module->require; |
466
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
Will C<require> the given $module. This avoids funny things like |
468
|
|
|
|
|
|
|
C<eval qq[require $module] or die $@>. It accepts only module names. |
469
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
On failure it will throw an exception, just like C<require>. On a |
471
|
|
|
|
|
|
|
success it returns the $module. This is mostly useful so that you can |
472
|
|
|
|
|
|
|
immediately call $module's C<import> method to emulate a C<use>. |
473
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
# like "use $module qw(foo bar);" if that worked |
475
|
|
|
|
|
|
|
$module->require->import(qw(foo bar)); |
476
|
|
|
|
|
|
|
|
477
|
|
|
|
|
|
|
# like "use $module;" if that worked |
478
|
|
|
|
|
|
|
$module->require->import; |
479
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
=head3 wrap |
481
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
my $wrapped = $string->wrap( width => $cols, separator => $sep ); |
483
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
Wraps $string to width $cols, breaking lines at word boundries using |
485
|
|
|
|
|
|
|
separator $sep. |
486
|
|
|
|
|
|
|
|
487
|
|
|
|
|
|
|
If no width is given, $cols defaults to 76. Default line separator is |
488
|
|
|
|
|
|
|
the newline character "\n". |
489
|
|
|
|
|
|
|
|
490
|
|
|
|
|
|
|
See L<Text::Wrap> for details. |
491
|
|
|
|
|
|
|
|
492
|
|
|
|
|
|
|
=head3 ltrim |
493
|
|
|
|
|
|
|
|
494
|
|
|
|
|
|
|
=head3 rtrim |
495
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
=head3 trim |
497
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
my $trimmed = $string->trim; |
499
|
|
|
|
|
|
|
my $trimmed = $string->trim($character_set); |
500
|
|
|
|
|
|
|
|
501
|
|
|
|
|
|
|
Trim whitespace. ltrim() trims off the start of the string (left), |
502
|
|
|
|
|
|
|
rtrim() off the end (right) and trim() off both the start and end. |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
my $string = ' testme'->ltrim; # 'testme' |
505
|
|
|
|
|
|
|
my $string = 'testme '->rtrim; # 'testme' |
506
|
|
|
|
|
|
|
my $string = ' testme '->trim; # 'testme' |
507
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
They all take an optional $character_set which will determine what |
509
|
|
|
|
|
|
|
characters should be trimmed. It follows regex character set syntax |
510
|
|
|
|
|
|
|
so C<A-Z> will trim everything from A to Z. Defaults to C<\s>, |
511
|
|
|
|
|
|
|
whitespace. |
512
|
|
|
|
|
|
|
|
513
|
|
|
|
|
|
|
my $string = '-> test <-'->trim('-><'); # ' test ' |
514
|
|
|
|
|
|
|
|
515
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
=head3 title_case |
517
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
my $name = 'joe smith'->title_case; # Joe Smith |
519
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
Will uppercase every word character that follows a wordbreak character. |
521
|
|
|
|
|
|
|
|
522
|
|
|
|
|
|
|
|
523
|
|
|
|
|
|
|
=head3 path2module |
524
|
|
|
|
|
|
|
|
525
|
|
|
|
|
|
|
my $module = $path->path2module; |
526
|
|
|
|
|
|
|
|
527
|
|
|
|
|
|
|
Given a relative $path it will return the Perl module this represents. |
528
|
|
|
|
|
|
|
For example, |
529
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
"Foo/Bar.pm"->path2module; # "Foo::Bar" |
531
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
It will throw an exception if given something which could not be a |
533
|
|
|
|
|
|
|
path to a Perl module. |
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
=head3 module2path |
536
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
my $path = $module->module2path; |
538
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
Will return the relative $path in which the Perl $module can be found. |
540
|
|
|
|
|
|
|
For example, |
541
|
|
|
|
|
|
|
|
542
|
|
|
|
|
|
|
"Foo::Bar"->module2path; # "Foo/Bar.pm" |
543
|
|
|
|
|
|
|
|
544
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
=head3 is_module_name |
546
|
|
|
|
|
|
|
|
547
|
|
|
|
|
|
|
my $is_valid = $string->is_module_name; |
548
|
|
|
|
|
|
|
|
549
|
|
|
|
|
|
|
Will return true if the $string is a valid module name. |
550
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
"Foo::Bar"->is_module_name; # true |
552
|
|
|
|
|
|
|
"Foo/Bar"->is_module_name; # false |
553
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
=head3 group_digits |
556
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
my $number_grouped = $number->group_digits; |
558
|
|
|
|
|
|
|
my $number_grouped = $number->group_digits(\%options); |
559
|
|
|
|
|
|
|
|
560
|
|
|
|
|
|
|
Turns a number like 1234567 into a string like 1,234,567 known as "digit grouping". |
561
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
It honors your current locale to determine the separator and grouping. |
563
|
|
|
|
|
|
|
This can be overridden using C<%options>. |
564
|
|
|
|
|
|
|
|
565
|
|
|
|
|
|
|
NOTE: many systems do not have their numeric locales set properly |
566
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
=over 4 |
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
=item separator |
570
|
|
|
|
|
|
|
|
571
|
|
|
|
|
|
|
The character used to separate groups. Defaults to "thousands_sep" in |
572
|
|
|
|
|
|
|
your locale or "," if your locale doesn't specify. |
573
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
=item decimal_point |
575
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
The decimal point character. Defaults to "decimal_point" in your |
577
|
|
|
|
|
|
|
locale or "." if your locale does not specify. |
578
|
|
|
|
|
|
|
|
579
|
|
|
|
|
|
|
=item grouping |
580
|
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
How many numbers in a group? Defaults to "grouping" in your locale or |
582
|
|
|
|
|
|
|
3 if your locale doesn't specify. |
583
|
|
|
|
|
|
|
|
584
|
|
|
|
|
|
|
Note: we don't honor the full grouping locale, its a wee bit too complicated. |
585
|
|
|
|
|
|
|
|
586
|
|
|
|
|
|
|
=item currency |
587
|
|
|
|
|
|
|
|
588
|
|
|
|
|
|
|
If true, it will treat the number as currency and use the monetary |
589
|
|
|
|
|
|
|
locale settings. "mon_thousands_sep" instead of "thousands_sep" and |
590
|
|
|
|
|
|
|
"mon_grouping" instead of "grouping". |
591
|
|
|
|
|
|
|
|
592
|
|
|
|
|
|
|
|
593
|
|
|
|
|
|
|
=back |
594
|
|
|
|
|
|
|
|
595
|
|
|
|
|
|
|
1234->group_digits; # 1,234 (assuming US locale) |
596
|
|
|
|
|
|
|
1234->group_digits( separator => "." ); # 1.234 |
597
|
|
|
|
|
|
|
|
598
|
|
|
|
|
|
|
|
599
|
|
|
|
|
|
|
=head3 commify |
600
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
my $number_grouped = $number->commify; |
602
|
|
|
|
|
|
|
my $number_grouped = $number->commify(\%options); |
603
|
|
|
|
|
|
|
|
604
|
|
|
|
|
|
|
commify() is just like group_digits() but it is not locale aware. It |
605
|
|
|
|
|
|
|
is useful when you want a predictable result regardless of the user's |
606
|
|
|
|
|
|
|
locale settings. |
607
|
|
|
|
|
|
|
|
608
|
|
|
|
|
|
|
C<%options> defaults to C<< ( separator => ",", grouping => 3, decimal_point => "." ) >>. |
609
|
|
|
|
|
|
|
Each key will be overridden individually. |
610
|
|
|
|
|
|
|
|
611
|
|
|
|
|
|
|
1234->commify; # 1,234 |
612
|
|
|
|
|
|
|
1234->commify({ separator => "." }); # 1.234 |
613
|
|
|
|
|
|
|
|
614
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
=head3 reverse |
616
|
|
|
|
|
|
|
|
617
|
|
|
|
|
|
|
my $reverse = $string->reverse; |
618
|
|
|
|
|
|
|
|
619
|
|
|
|
|
|
|
Reverses a $string. |
620
|
|
|
|
|
|
|
|
621
|
|
|
|
|
|
|
Unlike Perl's reverse(), this always reverses the string regardless of context. |
622
|
|
|
|
|
|
|
|
623
|
|
|
|
|
|
|
|
624
|
|
|
|
|
|
|
=head2 Array Autoboxing |
625
|
|
|
|
|
|
|
|
626
|
|
|
|
|
|
|
The methods provided by L<autobox::Core/Array Methods> are available |
627
|
|
|
|
|
|
|
from perl5i. |
628
|
|
|
|
|
|
|
|
629
|
|
|
|
|
|
|
All the functions from L<List::Util> and select ones from |
630
|
|
|
|
|
|
|
L<List::MoreUtils> are all available as methods on unblessed arrays |
631
|
|
|
|
|
|
|
and array refs: first, max, maxstr, min, minstr, minmax, shuffle, |
632
|
|
|
|
|
|
|
reduce, sum, any, all, none, true, false, uniq and mesh. |
633
|
|
|
|
|
|
|
|
634
|
|
|
|
|
|
|
They have all been altered to return array refs where applicable in |
635
|
|
|
|
|
|
|
order to allow chaining. |
636
|
|
|
|
|
|
|
|
637
|
|
|
|
|
|
|
@array->grep(sub{ $_->is_number })->sum->say; |
638
|
|
|
|
|
|
|
|
639
|
|
|
|
|
|
|
=head3 foreach |
640
|
|
|
|
|
|
|
|
641
|
|
|
|
|
|
|
@array->foreach( func($item) { ... } ); |
642
|
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
Works like the built in C<foreach>, calls the code block for each |
644
|
|
|
|
|
|
|
element of @array passing it into the block. |
645
|
|
|
|
|
|
|
|
646
|
|
|
|
|
|
|
@array->foreach( func($item) { say $item } ); # print each item |
647
|
|
|
|
|
|
|
|
648
|
|
|
|
|
|
|
It will pass in as many elements as the code block accepts. This |
649
|
|
|
|
|
|
|
allows you to iterate through an array 2 at a time, or 3 or 4 or |
650
|
|
|
|
|
|
|
whatever. |
651
|
|
|
|
|
|
|
|
652
|
|
|
|
|
|
|
my @names = ("Joe", "Smith", "Jim", "Dandy", "Jane", "Lane"); |
653
|
|
|
|
|
|
|
@names->foreach( func($fname, $lname) { |
654
|
|
|
|
|
|
|
say "Person: $fname $lname"; |
655
|
|
|
|
|
|
|
}); |
656
|
|
|
|
|
|
|
|
657
|
|
|
|
|
|
|
A normal subroutine with no signature will get one at a time. |
658
|
|
|
|
|
|
|
|
659
|
|
|
|
|
|
|
If @array is not a multiple of the iteration (for example, @array has |
660
|
|
|
|
|
|
|
5 elements and you ask 2 at a time) the behavior is currently undefined. |
661
|
|
|
|
|
|
|
|
662
|
|
|
|
|
|
|
=head3 as_hash |
663
|
|
|
|
|
|
|
|
664
|
|
|
|
|
|
|
my %hash = @array->as_hash; |
665
|
|
|
|
|
|
|
|
666
|
|
|
|
|
|
|
This method returns a %hash where each element of @array is a key. |
667
|
|
|
|
|
|
|
The values are all true. Its functionality is similar to: |
668
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
my %hash = map { $_ => 1 } @array; |
670
|
|
|
|
|
|
|
|
671
|
|
|
|
|
|
|
Example usage: |
672
|
|
|
|
|
|
|
|
673
|
|
|
|
|
|
|
my @array = ("a", "b", "c"); |
674
|
|
|
|
|
|
|
my %hash = @array->as_hash; |
675
|
|
|
|
|
|
|
say q[@array contains 'a'] if $hash{"a"}; |
676
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
=head3 pick |
678
|
|
|
|
|
|
|
|
679
|
|
|
|
|
|
|
my @rand = @array->pick($number); |
680
|
|
|
|
|
|
|
|
681
|
|
|
|
|
|
|
The pick() method returns a list of $number elements in @array. |
682
|
|
|
|
|
|
|
If $number is larger than the size of the list, it returns the entire list shuffled. |
683
|
|
|
|
|
|
|
|
684
|
|
|
|
|
|
|
Example usage: |
685
|
|
|
|
|
|
|
|
686
|
|
|
|
|
|
|
my @array = (1, 2, 3, 4); |
687
|
|
|
|
|
|
|
my @rand = @array->pick(2); |
688
|
|
|
|
|
|
|
|
689
|
|
|
|
|
|
|
=head3 pick_one |
690
|
|
|
|
|
|
|
|
691
|
|
|
|
|
|
|
my $rand = @array->pick_one; |
692
|
|
|
|
|
|
|
|
693
|
|
|
|
|
|
|
The pick_one() method returns a random element in @array. |
694
|
|
|
|
|
|
|
It is similar to @array->pick(1), except that it does not return a list. |
695
|
|
|
|
|
|
|
|
696
|
|
|
|
|
|
|
Example usage: |
697
|
|
|
|
|
|
|
|
698
|
|
|
|
|
|
|
my @array = (1,2,3,4); |
699
|
|
|
|
|
|
|
my $rand = @array->pick_one; |
700
|
|
|
|
|
|
|
|
701
|
|
|
|
|
|
|
=head3 diff |
702
|
|
|
|
|
|
|
|
703
|
|
|
|
|
|
|
Calculate the difference between two (or more) arrays: |
704
|
|
|
|
|
|
|
|
705
|
|
|
|
|
|
|
my @a = ( 1, 2, 3 ); |
706
|
|
|
|
|
|
|
my @b = ( 3, 4, 5 ); |
707
|
|
|
|
|
|
|
|
708
|
|
|
|
|
|
|
my @diff_a = @a->diff(\@b) # [ 1, 2 ] |
709
|
|
|
|
|
|
|
my @diff_b = @b->diff(\@a) # [ 4, 5 ] |
710
|
|
|
|
|
|
|
|
711
|
|
|
|
|
|
|
Diff returns all elements in array C<@a> that are not present in array |
712
|
|
|
|
|
|
|
C<@b>. Item order is not considered: two identical elements in both |
713
|
|
|
|
|
|
|
arrays will be recognized as such disregarding their index. |
714
|
|
|
|
|
|
|
|
715
|
|
|
|
|
|
|
[ qw( foo bar ) ]->diff( [ qw( bar foo ) ] ) # empty, they are equal |
716
|
|
|
|
|
|
|
|
717
|
|
|
|
|
|
|
For comparing more than two arrays: |
718
|
|
|
|
|
|
|
|
719
|
|
|
|
|
|
|
@a->diff(\@b, \@c, ... ) |
720
|
|
|
|
|
|
|
|
721
|
|
|
|
|
|
|
All comparisons are against the base array (C<@a> in this example). The |
722
|
|
|
|
|
|
|
result will be composed of all those elements that were present in C<@a> |
723
|
|
|
|
|
|
|
and in none other. |
724
|
|
|
|
|
|
|
|
725
|
|
|
|
|
|
|
It also works with nested data structures; it will traverse them |
726
|
|
|
|
|
|
|
depth-first to assess whether they are identical or not. For instance: |
727
|
|
|
|
|
|
|
|
728
|
|
|
|
|
|
|
[ [ 'foo ' ], { bar => 1 } ]->diff([ 'foo' ]) # [ { bar => 1 } ] |
729
|
|
|
|
|
|
|
|
730
|
|
|
|
|
|
|
In the case of overloaded objects (i.e., L<DateTime>, L<URI>, |
731
|
|
|
|
|
|
|
L<Path::Class>, etc.), it tries its best to treat them as strings or numbers. |
732
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
my $uri = URI->new("http://www.perl.com"); |
734
|
|
|
|
|
|
|
my $uri2 = URI->new("http://www.perl.com"); |
735
|
|
|
|
|
|
|
|
736
|
|
|
|
|
|
|
[ $uri ]->diff( [ "http://www.perl.com" ] ); # empty, they are equal |
737
|
|
|
|
|
|
|
[ $uri ]->diff( [ $uri2 ] ); # empty, they are equal |
738
|
|
|
|
|
|
|
|
739
|
|
|
|
|
|
|
|
740
|
|
|
|
|
|
|
=head3 popn |
741
|
|
|
|
|
|
|
|
742
|
|
|
|
|
|
|
my @newarray = @array->popn($n); |
743
|
|
|
|
|
|
|
|
744
|
|
|
|
|
|
|
L<Pops|perlfunc/pop> C<$n> values from the C<@array>. |
745
|
|
|
|
|
|
|
|
746
|
|
|
|
|
|
|
If C<$n> is greater than the length of C<@array>, it will return the |
747
|
|
|
|
|
|
|
whole C<@array>. If C<$n> is 0, it will return an empty array. |
748
|
|
|
|
|
|
|
|
749
|
|
|
|
|
|
|
A negative C<$n> or non-integer is an error. |
750
|
|
|
|
|
|
|
|
751
|
|
|
|
|
|
|
my @array = (1, 2, 3, 4, 5); |
752
|
|
|
|
|
|
|
my @newarray = @array->popn(3); # (3, 4, 5) |
753
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
|
755
|
|
|
|
|
|
|
=head3 shiftn |
756
|
|
|
|
|
|
|
|
757
|
|
|
|
|
|
|
my @newarray = @array->shiftn($n); |
758
|
|
|
|
|
|
|
|
759
|
|
|
|
|
|
|
Works like L<popn>, but it L<shifts|perlfunc/shift> off the front of |
760
|
|
|
|
|
|
|
the array instead of popping off the end. |
761
|
|
|
|
|
|
|
|
762
|
|
|
|
|
|
|
my @array = (1, 2, 3, 4, 5); |
763
|
|
|
|
|
|
|
my @newarray = @array->shiftn(3); # (1, 2, 3) |
764
|
|
|
|
|
|
|
|
765
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
=head3 intersect |
767
|
|
|
|
|
|
|
|
768
|
|
|
|
|
|
|
my @a = (1 .. 10); |
769
|
|
|
|
|
|
|
my @b = (5 .. 15); |
770
|
|
|
|
|
|
|
|
771
|
|
|
|
|
|
|
my @intersection = @a->intersect(\@b) # [ 5 .. 10 ]; |
772
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
Performs intersection between arrays, returning those elements that are |
774
|
|
|
|
|
|
|
present in all of the argument arrays simultaneously. |
775
|
|
|
|
|
|
|
|
776
|
|
|
|
|
|
|
As with C<diff()>, it works with any number of arrays, nested data |
777
|
|
|
|
|
|
|
structures of arbitrary depth, and handles overloaded objects |
778
|
|
|
|
|
|
|
graciously. |
779
|
|
|
|
|
|
|
|
780
|
|
|
|
|
|
|
=head3 ltrim |
781
|
|
|
|
|
|
|
|
782
|
|
|
|
|
|
|
=head3 rtrim |
783
|
|
|
|
|
|
|
|
784
|
|
|
|
|
|
|
=head3 trim |
785
|
|
|
|
|
|
|
|
786
|
|
|
|
|
|
|
my @trimmed = @list->trim; |
787
|
|
|
|
|
|
|
my @trimmed = @list->trim($character_set); |
788
|
|
|
|
|
|
|
|
789
|
|
|
|
|
|
|
Trim whitespace from each element of an array. Each works just like |
790
|
|
|
|
|
|
|
their scalar counterpart. |
791
|
|
|
|
|
|
|
|
792
|
|
|
|
|
|
|
my @trimmed = [ ' foo', 'bar ' ]->ltrim; # [ 'foo', 'bar ' ] |
793
|
|
|
|
|
|
|
my @trimmed = [ ' foo', 'bar ' ]->rtrim; # [ ' foo', 'bar' ] |
794
|
|
|
|
|
|
|
my @trimmed = [ ' foo', 'bar ' ]->trim; # [ 'foo', 'bar' ] |
795
|
|
|
|
|
|
|
|
796
|
|
|
|
|
|
|
As with the scalar trim() methods, they all take an optional $character_set |
797
|
|
|
|
|
|
|
which will determine what characters should be trimmed. |
798
|
|
|
|
|
|
|
|
799
|
|
|
|
|
|
|
my @trimmed = ['-> foo <-', '-> bar <-']->trim('-><'); # [' foo ', ' bar '] |
800
|
|
|
|
|
|
|
|
801
|
|
|
|
|
|
|
|
802
|
|
|
|
|
|
|
=head2 Hash Autoboxing |
803
|
|
|
|
|
|
|
|
804
|
|
|
|
|
|
|
All of the methods provided by L<autobox::Core/Hash Methods> are |
805
|
|
|
|
|
|
|
available from perl5i. |
806
|
|
|
|
|
|
|
|
807
|
|
|
|
|
|
|
In addition... |
808
|
|
|
|
|
|
|
|
809
|
|
|
|
|
|
|
=head3 each |
810
|
|
|
|
|
|
|
|
811
|
|
|
|
|
|
|
Iterate through each key/value pair in a hash using a callback. |
812
|
|
|
|
|
|
|
|
813
|
|
|
|
|
|
|
my %things = ( foo => 23, bar => 42 ); |
814
|
|
|
|
|
|
|
%things->each( func($k, $v) { |
815
|
|
|
|
|
|
|
say "Key: $k, Value: $v" |
816
|
|
|
|
|
|
|
}); |
817
|
|
|
|
|
|
|
|
818
|
|
|
|
|
|
|
Unlike the C<each> function, individual calls to each are guaranteed |
819
|
|
|
|
|
|
|
to iterate through the entirety of the hash. |
820
|
|
|
|
|
|
|
|
821
|
|
|
|
|
|
|
=head3 flip |
822
|
|
|
|
|
|
|
|
823
|
|
|
|
|
|
|
Exchanges values for keys in a hash. |
824
|
|
|
|
|
|
|
|
825
|
|
|
|
|
|
|
my %things = ( foo => 1, bar => 2, baz => 5 ); |
826
|
|
|
|
|
|
|
my %flipped = %things->flip; # { 1 => foo, 2 => bar, 5 => baz } |
827
|
|
|
|
|
|
|
|
828
|
|
|
|
|
|
|
If there is more than one occurence of a certain value, any one of the |
829
|
|
|
|
|
|
|
keys may end up as the value. This is because of the random ordering |
830
|
|
|
|
|
|
|
of hash keys. |
831
|
|
|
|
|
|
|
|
832
|
|
|
|
|
|
|
# Could be { 1 => foo }, { 1 => bar }, or { 1 => baz } |
833
|
|
|
|
|
|
|
{ foo => 1, bar => 1, baz => 1 }->flip; |
834
|
|
|
|
|
|
|
|
835
|
|
|
|
|
|
|
Because hash references cannot usefully be keys, it will not work on |
836
|
|
|
|
|
|
|
nested hashes. |
837
|
|
|
|
|
|
|
|
838
|
|
|
|
|
|
|
{ foo => [ 'bar', 'baz' ] }->flip; # dies |
839
|
|
|
|
|
|
|
|
840
|
|
|
|
|
|
|
|
841
|
|
|
|
|
|
|
=head3 merge |
842
|
|
|
|
|
|
|
|
843
|
|
|
|
|
|
|
Recursively merge two or more hashes together using L<Hash::Merge::Simple>. |
844
|
|
|
|
|
|
|
|
845
|
|
|
|
|
|
|
my $a = { a => 1 }; |
846
|
|
|
|
|
|
|
my $b = { b => 2, c => 3 }; |
847
|
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
$a->merge($b); # { a => 1, b => 2, c => 3 } |
849
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
For conflicting keys, rightmost precedence is used: |
851
|
|
|
|
|
|
|
|
852
|
|
|
|
|
|
|
my $a = { a => 1 }; |
853
|
|
|
|
|
|
|
my $b = { a => 100, b => 2}; |
854
|
|
|
|
|
|
|
|
855
|
|
|
|
|
|
|
$a->merge($b); # { a => 100, b => 2 } |
856
|
|
|
|
|
|
|
$b->merge($a); # { a => 1, b => 2 } |
857
|
|
|
|
|
|
|
|
858
|
|
|
|
|
|
|
It also works with nested hashes, although it won't attempt to merge |
859
|
|
|
|
|
|
|
array references or objects. For more information, look at the |
860
|
|
|
|
|
|
|
L<Hash::Merge::Simple> docs. |
861
|
|
|
|
|
|
|
|
862
|
|
|
|
|
|
|
=head3 diff |
863
|
|
|
|
|
|
|
|
864
|
|
|
|
|
|
|
my %staff = ( bob => 42, martha => 35, timmy => 23 ); |
865
|
|
|
|
|
|
|
my %promoted = ( timmy => 23 ); |
866
|
|
|
|
|
|
|
|
867
|
|
|
|
|
|
|
%staff->diff(\%promoted); # { bob => 42, martha => 35 } |
868
|
|
|
|
|
|
|
|
869
|
|
|
|
|
|
|
Returns the key/value pairs present in the first hash that are not |
870
|
|
|
|
|
|
|
present in the subsequent hash arguments. Otherwise works as |
871
|
|
|
|
|
|
|
C<< @array->diff >>. |
872
|
|
|
|
|
|
|
|
873
|
|
|
|
|
|
|
=head3 intersect |
874
|
|
|
|
|
|
|
|
875
|
|
|
|
|
|
|
%staff->intersect(\%promoted); # { timmy => 23 } |
876
|
|
|
|
|
|
|
|
877
|
|
|
|
|
|
|
Returns the key/value pairs that are present simultaneously in all the |
878
|
|
|
|
|
|
|
hash arguments. Otherwise works as C<< @array->intersect >>. |
879
|
|
|
|
|
|
|
|
880
|
|
|
|
|
|
|
|
881
|
|
|
|
|
|
|
=head2 Code autoboxing |
882
|
|
|
|
|
|
|
|
883
|
|
|
|
|
|
|
=head3 signature |
884
|
|
|
|
|
|
|
|
885
|
|
|
|
|
|
|
my $sig = $code->signature; |
886
|
|
|
|
|
|
|
|
887
|
|
|
|
|
|
|
You can query the signature of any code reference defined with C<func> |
888
|
|
|
|
|
|
|
or C<method>. See L<Signature Introspection> for details. |
889
|
|
|
|
|
|
|
|
890
|
|
|
|
|
|
|
If C<$code> has a signature, returns an object representing C<$code>'s |
891
|
|
|
|
|
|
|
signature. See L<perl5i::Signature> for details. Otherwise it |
892
|
|
|
|
|
|
|
returns nothing. |
893
|
|
|
|
|
|
|
|
894
|
|
|
|
|
|
|
|
895
|
|
|
|
|
|
|
=head3 caller |
896
|
|
|
|
|
|
|
|
897
|
|
|
|
|
|
|
L<Perl6::Caller> causes C<caller> to return an object in scalar |
898
|
|
|
|
|
|
|
context. |
899
|
|
|
|
|
|
|
|
900
|
|
|
|
|
|
|
=head3 die |
901
|
|
|
|
|
|
|
|
902
|
|
|
|
|
|
|
C<die> now always returns an exit code of 255 instead of trying to use |
903
|
|
|
|
|
|
|
C<$!> or C<$?> which makes the exit code unpredictable. If you want |
904
|
|
|
|
|
|
|
to exit with a message and a special exit code, use C<warn> then |
905
|
|
|
|
|
|
|
C<exit>. |
906
|
|
|
|
|
|
|
|
907
|
|
|
|
|
|
|
=head3 list |
908
|
|
|
|
|
|
|
|
909
|
|
|
|
|
|
|
C<list> will force list context similar to how |
910
|
|
|
|
|
|
|
L<scalar|perlfunc/scalar> will force scalar context. |
911
|
|
|
|
|
|
|
|
912
|
|
|
|
|
|
|
|
913
|
|
|
|
|
|
|
=head2 utf8::all |
914
|
|
|
|
|
|
|
|
915
|
|
|
|
|
|
|
perl5i turns on L<utf8::all> which turns on all the Unicode features |
916
|
|
|
|
|
|
|
of Perl it can. |
917
|
|
|
|
|
|
|
|
918
|
|
|
|
|
|
|
Here is the current list, more may be turned on later. |
919
|
|
|
|
|
|
|
|
920
|
|
|
|
|
|
|
Bare strings in your source code are now UTF8. This means UTF8 |
921
|
|
|
|
|
|
|
variable and method names, strings and regexes. |
922
|
|
|
|
|
|
|
|
923
|
|
|
|
|
|
|
my $message = "اÙا Ùا اتÙÙÙ
اÙعربÙÙ"; |
924
|
|
|
|
|
|
|
my $Ïάδε = "It's all Greek to me!"; |
925
|
|
|
|
|
|
|
sub fünkÃhüñ { ... } |
926
|
|
|
|
|
|
|
|
927
|
|
|
|
|
|
|
Strings will be treated as a set of characters rather than a set of |
928
|
|
|
|
|
|
|
bytes. For example, C<length> will return the number of characters, |
929
|
|
|
|
|
|
|
not the number of bytes. |
930
|
|
|
|
|
|
|
|
931
|
|
|
|
|
|
|
length("perl5i is MÃTÃÅ"); # 15, not 18 |
932
|
|
|
|
|
|
|
|
933
|
|
|
|
|
|
|
C<@ARGV> will be read as UTF8. |
934
|
|
|
|
|
|
|
|
935
|
|
|
|
|
|
|
STDOUT, STDIN, STDERR and all newly opened filehandles will have UTF8 |
936
|
|
|
|
|
|
|
encoding turned on. Consequently, if you want to output raw bytes to |
937
|
|
|
|
|
|
|
a file, such as outputting an image, you must set C<< binmode $fh >>. |
938
|
|
|
|
|
|
|
|
939
|
|
|
|
|
|
|
=head3 capture |
940
|
|
|
|
|
|
|
|
941
|
|
|
|
|
|
|
my($stdout, $stderr) = capture { ... } %options; |
942
|
|
|
|
|
|
|
my $stdout = capture { ... } %options; |
943
|
|
|
|
|
|
|
|
944
|
|
|
|
|
|
|
C<capture()> lets you capture all output to C<STDOUT> and C<STDERR> in |
945
|
|
|
|
|
|
|
any block of code. |
946
|
|
|
|
|
|
|
|
947
|
|
|
|
|
|
|
# $out = "Hello" |
948
|
|
|
|
|
|
|
# $err = "Bye" |
949
|
|
|
|
|
|
|
my($out, $err) = capture { |
950
|
|
|
|
|
|
|
print "Hello"; |
951
|
|
|
|
|
|
|
print STDERR "Bye"; |
952
|
|
|
|
|
|
|
}; |
953
|
|
|
|
|
|
|
|
954
|
|
|
|
|
|
|
If called in scalar context, it will only return C<STDOUT> and silence C<STDERR>. |
955
|
|
|
|
|
|
|
|
956
|
|
|
|
|
|
|
# $out = "Hello" |
957
|
|
|
|
|
|
|
my $out = capture { |
958
|
|
|
|
|
|
|
print "Hello"; |
959
|
|
|
|
|
|
|
warn "oh god"; |
960
|
|
|
|
|
|
|
}; |
961
|
|
|
|
|
|
|
|
962
|
|
|
|
|
|
|
C<capture> takes some options. |
963
|
|
|
|
|
|
|
|
964
|
|
|
|
|
|
|
=over 4 |
965
|
|
|
|
|
|
|
|
966
|
|
|
|
|
|
|
=item B<tee> |
967
|
|
|
|
|
|
|
|
968
|
|
|
|
|
|
|
tee will cause output to be captured yet still printed. |
969
|
|
|
|
|
|
|
|
970
|
|
|
|
|
|
|
my $out = capture { print "Hi" } tee => 1; |
971
|
|
|
|
|
|
|
|
972
|
|
|
|
|
|
|
=item B<merge> |
973
|
|
|
|
|
|
|
|
974
|
|
|
|
|
|
|
merge will merge C<STDOUT> and C<STDERR> into one variable. |
975
|
|
|
|
|
|
|
|
976
|
|
|
|
|
|
|
# $out = "HiBye" |
977
|
|
|
|
|
|
|
my $out = capture { |
978
|
|
|
|
|
|
|
print "Hi"; |
979
|
|
|
|
|
|
|
print STDERR "Bye"; |
980
|
|
|
|
|
|
|
} merge => 1; |
981
|
|
|
|
|
|
|
|
982
|
|
|
|
|
|
|
=back |
983
|
|
|
|
|
|
|
|
984
|
|
|
|
|
|
|
=head2 Carp |
985
|
|
|
|
|
|
|
|
986
|
|
|
|
|
|
|
C<croak> and C<carp> from L<Carp> are always available. |
987
|
|
|
|
|
|
|
|
988
|
|
|
|
|
|
|
The Carp message will always format consistently, smoothing over the |
989
|
|
|
|
|
|
|
backwards incompatible change in Carp 1.25. |
990
|
|
|
|
|
|
|
|
991
|
|
|
|
|
|
|
=head2 Child |
992
|
|
|
|
|
|
|
|
993
|
|
|
|
|
|
|
L<Child> provides the C<child> function which is a better way to do forking. |
994
|
|
|
|
|
|
|
|
995
|
|
|
|
|
|
|
C<child> creates and starts a child process, and returns an |
996
|
|
|
|
|
|
|
L<Child::Link::Proc> object which is a better interface for managing the child |
997
|
|
|
|
|
|
|
process. The only required argument is a codeblock, which is called in the new |
998
|
|
|
|
|
|
|
process. exit() is automatically called for you after the codeblock returns. |
999
|
|
|
|
|
|
|
|
1000
|
|
|
|
|
|
|
my $proc = child { |
1001
|
|
|
|
|
|
|
my $parent = shift; |
1002
|
|
|
|
|
|
|
... |
1003
|
|
|
|
|
|
|
}; |
1004
|
|
|
|
|
|
|
|
1005
|
|
|
|
|
|
|
You can also request a pipe for IPC: |
1006
|
|
|
|
|
|
|
|
1007
|
|
|
|
|
|
|
my $proc = child { |
1008
|
|
|
|
|
|
|
my $parent = shift; |
1009
|
|
|
|
|
|
|
|
1010
|
|
|
|
|
|
|
$parent->say("Message"); |
1011
|
|
|
|
|
|
|
my $reply = $parent->read(); |
1012
|
|
|
|
|
|
|
|
1013
|
|
|
|
|
|
|
... |
1014
|
|
|
|
|
|
|
} pipe => 1; |
1015
|
|
|
|
|
|
|
|
1016
|
|
|
|
|
|
|
my $message = $proc->read(); |
1017
|
|
|
|
|
|
|
$proc->say("reply"); |
1018
|
|
|
|
|
|
|
|
1019
|
|
|
|
|
|
|
See L<Child> for more information. |
1020
|
|
|
|
|
|
|
|
1021
|
|
|
|
|
|
|
|
1022
|
|
|
|
|
|
|
=head2 English |
1023
|
|
|
|
|
|
|
|
1024
|
|
|
|
|
|
|
L<English> gives English names to the punctuation variables; for |
1025
|
|
|
|
|
|
|
instance, C<<$@>> is also C<<$EVAL_ERROR>>. See L<perlvar> for |
1026
|
|
|
|
|
|
|
details. |
1027
|
|
|
|
|
|
|
|
1028
|
|
|
|
|
|
|
It does B<not> load the regex variables which affect performance. |
1029
|
|
|
|
|
|
|
C<$PREMATCH>, C<$MATCH>, and C<$POSTMATCH> will not exist. See |
1030
|
|
|
|
|
|
|
the C<p> modifier in L<perlre> for a better alternative. |
1031
|
|
|
|
|
|
|
|
1032
|
|
|
|
|
|
|
=head2 Modern::Perl |
1033
|
|
|
|
|
|
|
|
1034
|
|
|
|
|
|
|
L<Modern::Perl> turns on strict and warnings, enables all the 5.10 |
1035
|
|
|
|
|
|
|
features like C<given/when>, C<say> and C<state>, and enables C3 |
1036
|
|
|
|
|
|
|
method resolution order. |
1037
|
|
|
|
|
|
|
|
1038
|
|
|
|
|
|
|
=head2 CLASS |
1039
|
|
|
|
|
|
|
|
1040
|
|
|
|
|
|
|
Provides C<CLASS> and C<$CLASS> alternatives to C<__PACKAGE__>. |
1041
|
|
|
|
|
|
|
|
1042
|
|
|
|
|
|
|
=head2 File::chdir |
1043
|
|
|
|
|
|
|
|
1044
|
|
|
|
|
|
|
L<File::chdir> gives you C<$CWD> representing the current working |
1045
|
|
|
|
|
|
|
directory and it's assignable to C<chdir>. You can also localize it |
1046
|
|
|
|
|
|
|
to safely chdir inside a scope. |
1047
|
|
|
|
|
|
|
|
1048
|
|
|
|
|
|
|
=head2 File::stat |
1049
|
|
|
|
|
|
|
|
1050
|
|
|
|
|
|
|
L<File::stat> causes C<stat> to return an object in scalar context. |
1051
|
|
|
|
|
|
|
|
1052
|
|
|
|
|
|
|
=head2 DateTime |
1053
|
|
|
|
|
|
|
|
1054
|
|
|
|
|
|
|
C<time>, C<localtime>, and C<gmtime> are replaced with DateTime |
1055
|
|
|
|
|
|
|
objects. They will all act like the core functions. |
1056
|
|
|
|
|
|
|
|
1057
|
|
|
|
|
|
|
# Sat Jan 10 13:37:04 2004 |
1058
|
|
|
|
|
|
|
say scalar gmtime(2**30); |
1059
|
|
|
|
|
|
|
|
1060
|
|
|
|
|
|
|
# 2004 |
1061
|
|
|
|
|
|
|
say gmtime(2**30)->year; |
1062
|
|
|
|
|
|
|
|
1063
|
|
|
|
|
|
|
# 2009 (when this was written) |
1064
|
|
|
|
|
|
|
say time->year; |
1065
|
|
|
|
|
|
|
|
1066
|
|
|
|
|
|
|
|
1067
|
|
|
|
|
|
|
=head2 Time::y2038 |
1068
|
|
|
|
|
|
|
|
1069
|
|
|
|
|
|
|
C<gmtime()> and C<localtime()> will now safely work with dates beyond |
1070
|
|
|
|
|
|
|
the year 2038 and before 1901. The exact range is not defined, but we |
1071
|
|
|
|
|
|
|
guarantee at least up to 2**47 and back to year 1. |
1072
|
|
|
|
|
|
|
|
1073
|
|
|
|
|
|
|
|
1074
|
|
|
|
|
|
|
=head2 IO::Handle |
1075
|
|
|
|
|
|
|
|
1076
|
|
|
|
|
|
|
Turns filehandles into objects so you can call methods on them. The |
1077
|
|
|
|
|
|
|
biggest one is C<autoflush> rather than mucking around with C<$|> and |
1078
|
|
|
|
|
|
|
C<select>. |
1079
|
|
|
|
|
|
|
|
1080
|
|
|
|
|
|
|
$fh->autoflush(1); |
1081
|
|
|
|
|
|
|
|
1082
|
|
|
|
|
|
|
|
1083
|
|
|
|
|
|
|
=head2 autodie |
1084
|
|
|
|
|
|
|
|
1085
|
|
|
|
|
|
|
L<autodie> causes system and file calls which can fail |
1086
|
|
|
|
|
|
|
(C<open>, C<system>, and C<chdir>, for example) to die when they fail. |
1087
|
|
|
|
|
|
|
This means you don't have to put C<or die> at the end of every system |
1088
|
|
|
|
|
|
|
call, but you do have to wrap it in an C<eval> block if you want to |
1089
|
|
|
|
|
|
|
trap the failure. |
1090
|
|
|
|
|
|
|
|
1091
|
|
|
|
|
|
|
autodie's default error messages are pretty smart. |
1092
|
|
|
|
|
|
|
|
1093
|
|
|
|
|
|
|
All of autodie will be turned on. |
1094
|
|
|
|
|
|
|
|
1095
|
|
|
|
|
|
|
|
1096
|
|
|
|
|
|
|
=head2 autovivification |
1097
|
|
|
|
|
|
|
|
1098
|
|
|
|
|
|
|
L<autovivification> fixes the bug/feature where this: |
1099
|
|
|
|
|
|
|
|
1100
|
|
|
|
|
|
|
$hash = {}; |
1101
|
|
|
|
|
|
|
$hash->{key1}{key2}; |
1102
|
|
|
|
|
|
|
|
1103
|
|
|
|
|
|
|
Results in C<< $hash->{key1} >> coming into existence. That will no longer |
1104
|
|
|
|
|
|
|
happen. |
1105
|
|
|
|
|
|
|
|
1106
|
|
|
|
|
|
|
=head2 No indirect object syntax |
1107
|
|
|
|
|
|
|
|
1108
|
|
|
|
|
|
|
perl5i turns indirect object syntax, ie. C<new $obj>, into a compile |
1109
|
|
|
|
|
|
|
time error. Indirect object syntax is largely unnecessary and |
1110
|
|
|
|
|
|
|
removing it avoids a number of ambiguous cases where Perl will |
1111
|
|
|
|
|
|
|
mistakenly try to turn a function call into an indirect method call. |
1112
|
|
|
|
|
|
|
|
1113
|
|
|
|
|
|
|
See L<indirect> for details. |
1114
|
|
|
|
|
|
|
|
1115
|
|
|
|
|
|
|
=head3 want |
1116
|
|
|
|
|
|
|
|
1117
|
|
|
|
|
|
|
C<want()> generalizes the mechanism of the wantarray function, allowing a |
1118
|
|
|
|
|
|
|
function to determine the context it's being called in. Want distinguishes |
1119
|
|
|
|
|
|
|
not just scalar v. array context, but void, lvalue, rvalue, boolean, reference |
1120
|
|
|
|
|
|
|
context, and more. See perldoc L<Want> for full details. |
1121
|
|
|
|
|
|
|
|
1122
|
|
|
|
|
|
|
=head2 Try::Tiny |
1123
|
|
|
|
|
|
|
|
1124
|
|
|
|
|
|
|
L<Try::Tiny> gives support for try/catch blocks as an alternative to |
1125
|
|
|
|
|
|
|
C<eval BLOCK>. This allows correct error handling with proper localization |
1126
|
|
|
|
|
|
|
of $@ and a nice syntax layer: |
1127
|
|
|
|
|
|
|
|
1128
|
|
|
|
|
|
|
# handle errors with a catch handler |
1129
|
|
|
|
|
|
|
try { |
1130
|
|
|
|
|
|
|
die "foo"; |
1131
|
|
|
|
|
|
|
} catch { |
1132
|
|
|
|
|
|
|
warn "caught error: $_"; |
1133
|
|
|
|
|
|
|
}; |
1134
|
|
|
|
|
|
|
|
1135
|
|
|
|
|
|
|
# just silence errors |
1136
|
|
|
|
|
|
|
try { |
1137
|
|
|
|
|
|
|
die "foo"; |
1138
|
|
|
|
|
|
|
}; |
1139
|
|
|
|
|
|
|
|
1140
|
|
|
|
|
|
|
See perldoc L<Try::Tiny> for details. |
1141
|
|
|
|
|
|
|
|
1142
|
|
|
|
|
|
|
|
1143
|
|
|
|
|
|
|
=head2 true |
1144
|
|
|
|
|
|
|
|
1145
|
|
|
|
|
|
|
You no longer have to put a true value at the end of a module which uses perl5i. |
1146
|
|
|
|
|
|
|
|
1147
|
|
|
|
|
|
|
|
1148
|
|
|
|
|
|
|
=head2 Better load errors |
1149
|
|
|
|
|
|
|
|
1150
|
|
|
|
|
|
|
Most of us have learned the meaning of the dreaded "Can't locate Foo.pm in |
1151
|
|
|
|
|
|
|
@INC". Admittedly though, it's not the most helpful of the error messages. In |
1152
|
|
|
|
|
|
|
perl5i we provide a much friendlier error message. |
1153
|
|
|
|
|
|
|
|
1154
|
|
|
|
|
|
|
Example: |
1155
|
|
|
|
|
|
|
|
1156
|
|
|
|
|
|
|
Can't locate My/Module.pm in your Perl library. You may need to install it |
1157
|
|
|
|
|
|
|
from CPAN or another repository. Your library paths are: |
1158
|
|
|
|
|
|
|
Indented list of paths, 1 per line... |
1159
|
|
|
|
|
|
|
|
1160
|
|
|
|
|
|
|
|
1161
|
|
|
|
|
|
|
=head1 Turning off features |
1162
|
|
|
|
|
|
|
|
1163
|
|
|
|
|
|
|
use perl5i::2 -skip => \@features_to_skip; |
1164
|
|
|
|
|
|
|
|
1165
|
|
|
|
|
|
|
While perl5i is intended as a curated collection of modules, its |
1166
|
|
|
|
|
|
|
possible you might not want certain features. Features can be |
1167
|
|
|
|
|
|
|
turned off in your scope by using C<-skip>. |
1168
|
|
|
|
|
|
|
|
1169
|
|
|
|
|
|
|
For example, this will skip loading Try::Tiny. |
1170
|
|
|
|
|
|
|
|
1171
|
|
|
|
|
|
|
use perl5i::latest -skip => [qw(Try::Tiny)]; |
1172
|
|
|
|
|
|
|
|
1173
|
|
|
|
|
|
|
Why would you do this? You might want to use a different try/catch |
1174
|
|
|
|
|
|
|
module such as L<TryCatch> which provides its own C<try> and C<catch>. |
1175
|
|
|
|
|
|
|
|
1176
|
|
|
|
|
|
|
The feature strings are: C<autobox>, C<autodie>, C<autovivification>, |
1177
|
|
|
|
|
|
|
C<capture>, C<Carp::Fix::1_25>, C<Child>, C<CLASS>, C<die>, C<English>, |
1178
|
|
|
|
|
|
|
C<File::chdir>, C<indirect>, C<list>, C<Meta>, C<Modern::Perl>, |
1179
|
|
|
|
|
|
|
C<Perl6::Caller>, C<Signatures>, C<stat>, C<time>, C<true>, C<Try::Tiny>, |
1180
|
|
|
|
|
|
|
C<utf8::all>, C<Want>. |
1181
|
|
|
|
|
|
|
|
1182
|
|
|
|
|
|
|
|
1183
|
|
|
|
|
|
|
=head1 Command line program |
1184
|
|
|
|
|
|
|
|
1185
|
|
|
|
|
|
|
There is a perl5i command line program installed with perl5i (Windows |
1186
|
|
|
|
|
|
|
users get perl5i.bat). This is handy for writing one liners. |
1187
|
|
|
|
|
|
|
|
1188
|
|
|
|
|
|
|
perl5i -e 'gmtime->year->say' |
1189
|
|
|
|
|
|
|
|
1190
|
|
|
|
|
|
|
And you can use it on the C<#!> line. |
1191
|
|
|
|
|
|
|
|
1192
|
|
|
|
|
|
|
#!/usr/bin/perl5i |
1193
|
|
|
|
|
|
|
|
1194
|
|
|
|
|
|
|
gmtime->year->say; |
1195
|
|
|
|
|
|
|
|
1196
|
|
|
|
|
|
|
If you write a one-liner without using this program, saying C<-Mperl5i> means |
1197
|
|
|
|
|
|
|
C<-Mperl5i::latest>. Please see L</"Using perl5i"> and L</VERSIONING> for |
1198
|
|
|
|
|
|
|
details. |
1199
|
|
|
|
|
|
|
|
1200
|
|
|
|
|
|
|
|
1201
|
|
|
|
|
|
|
=head1 BUGS |
1202
|
|
|
|
|
|
|
|
1203
|
|
|
|
|
|
|
Some parts are not lexical. Some parts are package scoped. |
1204
|
|
|
|
|
|
|
|
1205
|
|
|
|
|
|
|
If you're going to use two versions of perl5i together, we do not |
1206
|
|
|
|
|
|
|
currently recommend having them in the same package. |
1207
|
|
|
|
|
|
|
|
1208
|
|
|
|
|
|
|
See L<http://github.com/evalEmpire/perl5i/issues/labels/bug> for a complete list. |
1209
|
|
|
|
|
|
|
|
1210
|
|
|
|
|
|
|
Please report bugs at L<http://github.com/evalEmpire/perl5i/issues/>. |
1211
|
|
|
|
|
|
|
|
1212
|
|
|
|
|
|
|
|
1213
|
|
|
|
|
|
|
=head1 VERSIONING |
1214
|
|
|
|
|
|
|
|
1215
|
|
|
|
|
|
|
perl5i follows the Semantic Versioning policy, L<http://semver.org>. |
1216
|
|
|
|
|
|
|
In short... |
1217
|
|
|
|
|
|
|
|
1218
|
|
|
|
|
|
|
Versions will be of the form X.Y.Z. |
1219
|
|
|
|
|
|
|
|
1220
|
|
|
|
|
|
|
0.Y.Z may change anything at any time. |
1221
|
|
|
|
|
|
|
|
1222
|
|
|
|
|
|
|
Incrementing X (ie. 1.2.3 -> 2.0.0) indicates a backwards incompatible change. |
1223
|
|
|
|
|
|
|
|
1224
|
|
|
|
|
|
|
Incrementing Y (ie. 1.2.3 -> 1.3.0) indicates a new feature. |
1225
|
|
|
|
|
|
|
|
1226
|
|
|
|
|
|
|
Incrementing Z (ie. 1.2.3 -> 1.2.4) indicates a bug fix or other internal change. |
1227
|
|
|
|
|
|
|
|
1228
|
|
|
|
|
|
|
|
1229
|
|
|
|
|
|
|
=head1 NOTES |
1230
|
|
|
|
|
|
|
|
1231
|
|
|
|
|
|
|
Inspired by chromatic's L<Modern::Perl> and in particular |
1232
|
|
|
|
|
|
|
F<http://www.modernperlbooks.com/mt/2009/04/ugly-perl-a-lesson-in-the-importance-of-language-design.html>. |
1233
|
|
|
|
|
|
|
|
1234
|
|
|
|
|
|
|
I totally didn't come up with the "Perl 5 + i" joke. I think it was |
1235
|
|
|
|
|
|
|
Damian Conway. |
1236
|
|
|
|
|
|
|
|
1237
|
|
|
|
|
|
|
|
1238
|
|
|
|
|
|
|
=head1 THANKS |
1239
|
|
|
|
|
|
|
|
1240
|
|
|
|
|
|
|
Thanks to our contributors: Chas Owens, Darian Patrick, rjbs, |
1241
|
|
|
|
|
|
|
chromatic, Ben Hengst, Bruno Vecchi and anyone else I've forgotten. |
1242
|
|
|
|
|
|
|
|
1243
|
|
|
|
|
|
|
Thanks to Flavian and Matt Trout for their signature and |
1244
|
|
|
|
|
|
|
L<Devel::Declare> work. |
1245
|
|
|
|
|
|
|
|
1246
|
|
|
|
|
|
|
Thanks to all the CPAN authors upon whom this builds. |
1247
|
|
|
|
|
|
|
|
1248
|
|
|
|
|
|
|
=head1 LICENSE |
1249
|
|
|
|
|
|
|
|
1250
|
|
|
|
|
|
|
Copyright 2009-2010, Michael G Schwern <schwern@pobox.com> |
1251
|
|
|
|
|
|
|
|
1252
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
1253
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
1254
|
|
|
|
|
|
|
|
1255
|
|
|
|
|
|
|
See L<http://dev.perl.org/licenses/artistic.html> |
1256
|
|
|
|
|
|
|
|
1257
|
|
|
|
|
|
|
|
1258
|
|
|
|
|
|
|
=head1 SEE ALSO |
1259
|
|
|
|
|
|
|
|
1260
|
|
|
|
|
|
|
Repository: L<http://github.com/evalEmpire/perl5i/> |
1261
|
|
|
|
|
|
|
Issues/Bugs: L<http://github.com/evalEmpire/perl5i/issues> |
1262
|
|
|
|
|
|
|
IRC: L<irc://irc.perl.org> on the #perl5i channel |
1263
|
|
|
|
|
|
|
Wiki: L<http://github.com/evalEmpire/perl5i/wiki> |
1264
|
|
|
|
|
|
|
Twitter: L<http://twitter.com/perl5i> |
1265
|
|
|
|
|
|
|
|
1266
|
|
|
|
|
|
|
Frequently Asked Questions about perl5i: L<perl5ifaq> |
1267
|
|
|
|
|
|
|
|
1268
|
|
|
|
|
|
|
Some modules with similar purposes include: |
1269
|
|
|
|
|
|
|
L<Modern::Perl>, L<Common::Sense> |
1270
|
|
|
|
|
|
|
|
1271
|
|
|
|
|
|
|
For a complete object declaration system, see L<Moose> and |
1272
|
|
|
|
|
|
|
L<MooseX::Declare>. |