File Coverage

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
total 17 17 100.0


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         # Ability to pass in your own subclasses is tested but undocumented ..
15         # The catch is that the Roles fall back to the standard object classes
16         # if blessed_or_pkg hits on a non-blessed ref (i.e. we're called against
17         # an autoboxed ref). In other words, your autoboxed objects in-scope have
18         # your spiffy new subclass' methods -- but lots of methods checking
19         # blessed_or_pkg() will lose your spiffyness and revert to boring old
20         # standard types.
21         #
22         # I'm sure there's a work-around, but I haven't thought of it, yet . . .
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