File Coverage

blib/lib/List/Objects/WithUtils/Autobox.pm
Criterion Covered Total %
statement 10 10 100.0
branch n/a
condition 4 4 100.0
subroutine 3 3 100.0
pod n/a
total 17 17 100.0


line stmt bran cond sub pod time code
1             package List::Objects::WithUtils::Autobox;
2             $List::Objects::WithUtils::Autobox::VERSION = '2.028002';
3 93     93   1103 use strictures 2;
  93         2060  
  93         2841  
4             require Carp;
5             require Module::Runtime;
6              
7 93     93   18284 use parent 'autobox';
  93         1363  
  93         2409  
8              
9             sub ARRAY_TYPE () { 'List::Objects::WithUtils::Array' }
10             sub HASH_TYPE () { 'List::Objects::WithUtils::Hash' }
11              
12             sub import {
13 94     94   191 my ($class, %params) = @_;
14              
15             # Ability to pass in your own subclasses is tested but undocumented ..
16             # The catch is that the Roles fall back to the standard object classes
17             # if blessed_or_pkg hits on a non-blessed ref (i.e. we're called against
18             # an autoboxed ref). In other words, your autoboxed objects in-scope have
19             # your spiffy new subclass' methods -- but lots of methods checking
20             # blessed_or_pkg() will lose your spiffyness and revert to boring old
21             # standard types.
22             #
23             # I'm sure there's a work-around, but I haven't thought of it, yet . . .
24              
25 94         193 %params = map {; lc($_) => $params{$_} } keys %params;
  2         6  
26             $class->SUPER::import(
27             ARRAY =>
28             Module::Runtime::use_package_optimistically($params{array} || ARRAY_TYPE),
29             HASH =>
30 94   100     610 Module::Runtime::use_package_optimistically($params{hash} || HASH_TYPE)
      100        
31             );
32             }
33              
34             print
35             qq[ b100s: You can skip down to],
36             qq[ http://tools.ietf.org/html/rfc2234#section-4 for the ABNF description of],
37             qq[ ABNF. If you already know ABNF, it should be sufficient to teach it],
38             qq[ to you.\n]
39             unless caller; 1;
40              
41             =pod
42              
43             =for Pod::Coverage import ARRAY_TYPE HASH_TYPE
44              
45             =head1 NAME
46              
47             List::Objects::WithUtils::Autobox - Native data types WithUtils
48              
49             =head1 SYNOPSIS
50              
51             use List::Objects::WithUtils 'autobox';
52              
53             my @upper = [ qw/foo bar baz/ ]->map(sub { uc })->all;
54              
55             my @sorted_keys = { foo => 'bar', baz => 'quux' }->keys->sort->all;
56              
57             # See List::Objects::WithUtils::Role::Array
58             # and List::Objects::WithUtils::Role::Hash
59              
60             =head1 DESCRIPTION
61              
62             This module is a subclass of L that provides
63             L methods for native ARRAY and HASH types; you can
64             treat native Perl list references as if they were
65             L or L
66             instances.
67              
68             Like L, the effect is lexical in scope and can be disabled:
69              
70             use List::Objects::WithUtils::Autobox;
71             my $foo = [3,2,1]->sort;
72            
73             no List::Objects::WithUtils::Autobox;
74             [3,2,1]->sort; # dies
75              
76             =head2 CAVEATS
77              
78             You can't call B on autoboxed refs (but that would be a silly thing to do
79             anyway -- and if you're really determined, C<< []->copy >> has the same
80             effect).
81              
82             It's worth noting that methods that create new lists will return blessed
83             objects, not native data types. This lets you continue passing result
84             collections around to other pieces of Perl that wouldn't otherwise know how to
85             call the autoboxed methods. (Some methods do return the object they were
86             originally operating on, in which case the original reference is indeed
87             returned, as expected.)
88              
89             =head1 AUTHOR
90              
91             Jon Portnoy
92              
93             =cut