Changeset 25726
- Timestamp:
- 12/03/08 05:23:06 (4 years ago)
- Location:
- lang/perl/Util-Any/trunk
- Files:
-
- 1 added
- 3 modified
-
Makefile.PL (modified) (1 diff)
-
lib/Util/Any.pm (modified) (8 diffs)
-
t/01-util-kind-my-prefix.t (added)
-
t/lib/MyUtil.pm (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Util-Any/trunk/Makefile.PL
r25725 r25726 10 10 PL_FILES => {}, 11 11 PREREQ_PM => { 12 'Test::More' => 0, 12 'Test::More' => 0, 13 'ExportTo' => 0, 14 'List::Util' => 0, 15 'List::MoreUtils' => 0, 16 'Hash::Util' => 0, 17 'Scalar::Util' => 0, 13 18 }, 14 19 dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, -
lang/perl/Util-Any/trunk/lib/Util/Any.pm
r25725 r25726 2 2 3 3 use ExportTo (); 4 use Carp (); 4 5 use warnings; 5 6 use strict; 6 7 7 8 our %Utils = ( 8 List => [ qw/List::Util List::MoreUtils/ ],9 Scalar => [ qw/Scalar::Util/ ],10 Hash => [ qw/Hash::Util/ ],9 list => [ qw/List::Util List::MoreUtils/ ], 10 scalar => [ qw/Scalar::Util/ ], 11 hash => [ qw/Hash::Util/ ], 11 12 ); 12 13 … … 15 16 my $caller = (caller)[0]; 16 17 my %want; 17 my %opt = (prefix => 0 );18 my %opt = (prefix => 0, module_prefix => 0); 18 19 19 20 if (@_ > 1 and ref $_[-1] eq 'HASH') { … … 23 24 if (@_) { 24 25 if (ref $_[0] eq 'HASH') { 25 %want = %{shift()}; 26 my %_want = %{shift()}; 27 %want = map {lc($_) => $_want{$_}} keys %_want; 26 28 } elsif (lc($_[0]) eq 'all') { 27 29 @want{keys %Utils} = (); 28 30 } else { 29 @want{ @_} = ();31 @want{map lc $_, @_} = (); 30 32 } 31 33 } … … 34 36 35 37 foreach my $kind (keys %Utils) { 36 my $prefix = lc($kind) . '_'; 38 my ($prefix, $module_prefix) = ('',''); 39 37 40 if (exists $want{$kind}) { 38 41 foreach my $class (@{$Utils{$kind}}) { 42 ($class, $module_prefix) = ref $class ? @$class : ($class, $prefix); 43 if ($opt{module_prefix} and $module_prefix) { 44 $prefix = $module_prefix; 45 } elsif ($opt{prefix}) { 46 $prefix = lc($kind) . '_'; 47 } 39 48 eval "require $class"; 40 49 unless ($@) { 41 50 my @funcs = @{${class} . '::EXPORT_OK'}; 42 unless (my $want_func = $want{$kind}) { 43 if ($opt{prefix}) { 44 ExportTo::export_to 45 ($caller => {map {$prefix . $_ => $class . '::' . $_} @funcs}); 46 } else { 47 ExportTo::export_to 48 ($caller => [map $class . '::' . $_, @funcs]); 49 } 50 } else { 51 if (my $want_func = $want{$kind}) { 51 52 my %w; 52 53 @w{@$want_func} = (); 53 if ($opt{prefix}) { 54 ExportTo::export_to 55 ($caller => {map {$prefix . $_ => $class . '::' . $_} grep exists $w{$_}, @funcs}); 56 } else { 57 ExportTo::export_to 58 ($caller => [map $class . '::' . $_, grep exists $w{$_}, @funcs]); 59 } 54 @funcs = grep exists $w{$_}, @funcs; 60 55 } 56 if ($prefix) { 57 ExportTo::export_to 58 ($caller => {map {$prefix . $_ => $class . '::' . $_} @funcs}); 59 } else { 60 ExportTo::export_to 61 ($caller => [map $class . '::' . $_, @funcs]); 62 } 63 } else { 64 Carp::carp $@; 61 65 } 62 66 } … … 80 84 =head1 SYNOPSIS 81 85 82 use Util::Any qw/ List/;86 use Util::Any qw/list/; 83 87 # you can import any functions of List::Util and List::MoreUtils 84 88 … … 87 91 If you want to choose functions 88 92 89 use Util::Any { List => qw/uniq/};93 use Util::Any {list => qw/uniq/}; 90 94 # you can import uniq function, not import other functions 91 95 … … 94 98 If you want to import All kind of utility functions 95 99 96 use Util::Any qw/ All/;100 use Util::Any qw/all/; 97 101 98 102 If you want to import functions with prefix(ex. list_, scalar_, hash_) 99 103 100 use Util::Any qw/ All/, {prefix => 1};101 use Util::Any qw/ List/, {prefix => 1};104 use Util::Any qw/all/, {prefix => 1}; 105 use Util::Any qw/list/, {prefix => 1}; 102 106 use Util::Any {List => qw/uniq/}, {prefix => 1}; 107 108 print list_uniq qw/1, 0, 1, 2, 3, 3/; 103 109 104 110 =head1 DESCRIPTION … … 118 124 package Util::Yours; 119 125 120 BEGIN { 121 use base qw/Util::Any/; 122 push @{$Util::Any::Utils{List}}, qw/Your::Favolite::List::Utils/; 123 } 126 use base qw/Util::Any/; 127 push @{$Util::Any::Utils{list}}, qw/Your::Favorite::List::Utils/; 124 128 125 129 1; 126 130 127 In your code. 128 129 use Util::Yours qw/List/; 131 In your code; 132 133 use Util::Yours qw/list/; 134 135 =head1 PREFIX FOR EACH MODULE 136 137 If you want to import many modules and they have same function name. 138 You can specify prefix for each module like the following. 139 140 use base qw/Util::Any/; 141 142 %Util::Any::Utils = ( 143 list => [['List::Util' => 'lu_'], ['List::MoreUtils' => 'lmu_']] 144 ); 145 146 147 In your code; 148 149 use Util::Yours qw/list/, {module_prefix => 1}; 130 150 131 151 =head1 AUTHOR -
lang/perl/Util-Any/trunk/t/lib/MyUtil.pm
r25725 r25726 1 1 package MyUtil; 2 2 3 BEGIN { 4 use base qw/Util::Any/; 5 %Util::Any::Utils = 6 ( 7 List => [qw/List::Util/], 8 ); 9 } 3 use base qw/Util::Any/; 4 %Util::Any::Utils = 5 ( 6 list => [['List::Util' => 'lu_']], 7 ); 10 8 11 9 1;
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)