« List/Objects/WithUtils/Role/Array/Immutable.pm
↑ Summary
List/Objects/WithUtils/Role/Hash/TiedRO.pm »
| 1 | package List::Objects::WithUtils::Autobox; | ||||||||||||||||||||||
| 2 | 93 | $List::Objects::WithUtils::Autobox::VERSION = '2.028002'; | |||||||||||||||||||||
| 3 | 93 | ▶ | use strictures 2; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 93 | require Carp; | |||||||||||||||||||||
| 5 | 93 | require Module::Runtime; | |||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | 93 | ▶ | use parent 'autobox'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | sub ARRAY_TYPE () { 'List::Objects::WithUtils::Array' } | ||||||||||||||||||||||
| 10 | sub HASH_TYPE () { 'List::Objects::WithUtils::Hash' } | ||||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | sub import { | ||||||||||||||||||||||
| 13 | 94 | ▶ | my ($class, %params) = @_; | ||||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=11.0
| |||||||||||||||||||||||
| 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 | %params = map {; lc($_) => $params{$_} } keys %params; | |||||||||||||||||||||
| 26 | $class->SUPER::import( | ||||||||||||||||||||||
| 27 | ARRAY => | ||||||||||||||||||||||
| 28 | Module::Runtime::use_package_optimistically($params{array} || ARRAY_TYPE), | ||||||||||||||||||||||
| 29 | HASH => | ||||||||||||||||||||||
| 30 | 94 | ▶ | Module::Runtime::use_package_optimistically($params{hash} || HASH_TYPE) | ||||||||||||||||||||
|
Condition100%4/4
$params{'array'} || 'List::Objects::WithUtils::Array'
$params{'hash'} || 'List::Objects::WithUtils::Hash'
MC/DC100%1/1
$params{'array'} || 'List::Objects::WithUtils::Array'
$params{'array'}
MC/DC100%1/1
$params{'hash'} || 'List::Objects::WithUtils::Hash'
$params{'hash'}
Truth table100%2/2
A
A= $params{'array'}
Truth table100%2/2
A
A= $params{'hash'}
| |||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||
| 33 | |||||||||||||||||||||||
| 34 | 93 | ▶ | |||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 35 | qq[<dngor> 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 | 93 | 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<autobox> that provides | ||||||||||||||||||||||
| 63 | L<List::Objects::WithUtils> methods for native ARRAY and HASH types; you can | ||||||||||||||||||||||
| 64 | treat native Perl list references as if they were | ||||||||||||||||||||||
| 65 | L<List::Objects::WithUtils::Array> or L<List::Objects::WithUtils::Hash> | ||||||||||||||||||||||
| 66 | instances. | ||||||||||||||||||||||
| 67 | |||||||||||||||||||||||
| 68 | Like L<autobox>, 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<new> 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 <avenj@cobaltirc.org> | ||||||||||||||||||||||
| 92 | |||||||||||||||||||||||
| 93 | =cut | ||||||||||||||||||||||