line |
stmt |
bran |
cond |
sub |
code |
1
|
|
|
|
|
package List::Objects::WithUtils::Autobox; |
2
|
8
|
|
|
8
|
use strictures 1; |
|
8
|
|
|
|
|
|
8
|
|
|
|
|
3
|
|
|
|
|
require Carp; |
4
|
|
|
|
|
require Module::Runtime; |
5
|
|
|
|
|
|
6
|
8
|
|
|
8
|
use parent 'autobox'; |
|
8
|
|
|
|
|
|
8
|
|
|
|
|
7
|
|
|
|
|
|
8
|
|
|
|
|
sub ARRAY_TYPE () { 'List::Objects::WithUtils::Array' } |
9
|
|
|
|
|
sub HASH_TYPE () { 'List::Objects::WithUtils::Hash' } |
10
|
|
|
|
|
|
11
|
|
|
|
|
sub import { |
12
|
9
|
|
|
9
|
my ($class, %params) = @_; |
13
|
|
|
|
|
|
14
|
|
|
|
|
|
15
|
|
|
|
|
|
16
|
|
|
|
|
|
17
|
|
|
|
|
|
18
|
|
|
|
|
|
19
|
|
|
|
|
|
20
|
|
|
|
|
|
21
|
|
|
|
|
|
22
|
|
|
|
|
|
23
|
|
|
|
|
|
24
|
9
|
|
|
|
%params = map {; lc($_) => $params{$_} } keys %params; |
|
2
|
|
|
|
|
25
|
9
|
|
100
|
|
$class->SUPER::import( |
|
|
|
100
|
|
|
26
|
|
|
|
|
ARRAY => |
27
|
|
|
|
|
Module::Runtime::use_package_optimistically($params{array} || ARRAY_TYPE), |
28
|
|
|
|
|
HASH => |
29
|
|
|
|
|
Module::Runtime::use_package_optimistically($params{hash} || HASH_TYPE) |
30
|
|
|
|
|
); |
31
|
|
|
|
|
} |
32
|
|
|
|
|
|
33
|
|
|
|
|
1; |
34
|
|
|
|
|
|
35
|
|
|
|
|
=pod |
36
|
|
|
|
|
|
37
|
|
|
|
|
=for Pod::Coverage import ARRAY_TYPE HASH_TYPE |
38
|
|
|
|
|
|
39
|
|
|
|
|
=head1 NAME |
40
|
|
|
|
|
|
41
|
|
|
|
|
List::Objects::WithUtils::Autobox - Native data types WithUtils |
42
|
|
|
|
|
|
43
|
|
|
|
|
=head1 SYNOPSIS |
44
|
|
|
|
|
|
45
|
|
|
|
|
use List::Objects::WithUtils 'autobox'; |
46
|
|
|
|
|
|
47
|
|
|
|
|
my @upper = [ qw/foo bar baz/ ]->map(sub { uc $_[0] })->all; |
48
|
|
|
|
|
|
49
|
|
|
|
|
my @sorted_keys = { foo => 'bar', baz => 'quux' }->keys->sort->all; |
50
|
|
|
|
|
|
51
|
|
|
|
|
# See List::Objects::WithUtils::Role::Array |
52
|
|
|
|
|
# and List::Objects::WithUtils::Role::Hash |
53
|
|
|
|
|
|
54
|
|
|
|
|
=head1 DESCRIPTION |
55
|
|
|
|
|
|
56
|
|
|
|
|
This module is a subclass of L<autobox> that provides |
57
|
|
|
|
|
L<List::Objects::WithUtils> methods for native ARRAY and HASH types; you can |
58
|
|
|
|
|
treat native Perl list references as if they were |
59
|
|
|
|
|
L<List::Objects::WithUtils::Array> or L<List::Objects::WithUtils::Hash> |
60
|
|
|
|
|
instances. |
61
|
|
|
|
|
|
62
|
|
|
|
|
Like L<autobox>, the effect is lexical in scope and can be disabled: |
63
|
|
|
|
|
|
64
|
|
|
|
|
use List::Objects::WithUtils::Autobox; |
65
|
|
|
|
|
my $foo = [3,2,1]->sort; |
66
|
|
|
|
|
|
67
|
|
|
|
|
no List::Objects::WithUtils::Autobox; |
68
|
|
|
|
|
[3,2,1]->sort; # dies |
69
|
|
|
|
|
|
70
|
|
|
|
|
It's worth noting that methods that create new lists will return blessed |
71
|
|
|
|
|
objects, not native data types. This lets you continue passing result |
72
|
|
|
|
|
collections around to other pieces of Perl that wouldn't otherwise know how to |
73
|
|
|
|
|
call the autoboxed methods. (Some methods do return the object they were |
74
|
|
|
|
|
originally operating on, in which case the original reference is indeed |
75
|
|
|
|
|
returned, as expected.) |
76
|
|
|
|
|
|
77
|
|
|
|
|
=head1 AUTHOR |
78
|
|
|
|
|
|
79
|
|
|
|
|
Jon Portnoy <avenj@cobaltirc.org> |
80
|
|
|
|
|
|
81
|
|
|
|
|
=cut |
82
|
|
|
|
|
|