Changeset 25726

Show
Ignore:
Timestamp:
12/03/08 05:23:06 (4 years ago)
Author:
ktat
Message:

add module_prefix option; remove duplicated code

Location:
lang/perl/Util-Any/trunk
Files:
1 added
3 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Util-Any/trunk/Makefile.PL

    r25725 r25726  
    1010    PL_FILES            => {}, 
    1111    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, 
    1318    }, 
    1419    dist                => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, 
  • lang/perl/Util-Any/trunk/lib/Util/Any.pm

    r25725 r25726  
    22 
    33use ExportTo (); 
     4use Carp (); 
    45use warnings; 
    56use strict; 
    67 
    78our %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/ ], 
    1112             ); 
    1213 
     
    1516  my $caller = (caller)[0]; 
    1617  my %want; 
    17   my %opt = (prefix => 0); 
     18  my %opt = (prefix => 0, module_prefix => 0); 
    1819 
    1920  if (@_ > 1 and ref $_[-1] eq 'HASH') { 
     
    2324  if (@_) { 
    2425    if (ref $_[0] eq 'HASH') { 
    25       %want = %{shift()}; 
     26      my %_want = %{shift()}; 
     27      %want = map {lc($_) => $_want{$_}} keys %_want; 
    2628    } elsif (lc($_[0]) eq 'all') { 
    2729      @want{keys %Utils} = (); 
    2830    } else { 
    29       @want{@_} = (); 
     31      @want{map lc $_, @_} = (); 
    3032    } 
    3133  } 
     
    3436 
    3537  foreach my $kind (keys %Utils) { 
    36     my $prefix = lc($kind) . '_'; 
     38    my ($prefix, $module_prefix) = ('',''); 
     39 
    3740    if (exists $want{$kind}) { 
    3841      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        } 
    3948        eval "require $class"; 
    4049        unless ($@) { 
    4150          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}) { 
    5152            my %w; 
    5253            @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; 
    6055          } 
     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 $@; 
    6165        } 
    6266      } 
     
    8084=head1 SYNOPSIS 
    8185 
    82     use Util::Any qw/List/; 
     86    use Util::Any qw/list/; 
    8387    # you can import any functions of List::Util and List::MoreUtils 
    8488 
     
    8791If you want to choose functions 
    8892 
    89     use Util::Any {List => qw/uniq/}; 
     93    use Util::Any {list => qw/uniq/}; 
    9094    # you can import uniq function, not import other functions 
    9195 
     
    9498If you want to import All kind of utility functions 
    9599 
    96     use Util::Any qw/All/; 
     100    use Util::Any qw/all/; 
    97101 
    98102If you want to import functions with prefix(ex. list_, scalar_, hash_) 
    99103 
    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}; 
    102106    use Util::Any {List => qw/uniq/}, {prefix => 1}; 
     107     
     108    print list_uniq qw/1, 0, 1, 2, 3, 3/; 
    103109 
    104110=head1 DESCRIPTION 
     
    118124 package Util::Yours; 
    119125  
    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/; 
    124128  
    125129 1; 
    126130 
    127 In your code. 
    128  
    129  use Util::Yours qw/List/; 
     131In your code; 
     132 
     133 use Util::Yours qw/list/; 
     134 
     135=head1 PREFIX FOR EACH MODULE 
     136 
     137If you want to import many modules and they have same function name. 
     138You 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 
     147In your code; 
     148 
     149 use Util::Yours qw/list/, {module_prefix => 1}; 
    130150 
    131151=head1 AUTHOR 
  • lang/perl/Util-Any/trunk/t/lib/MyUtil.pm

    r25725 r25726  
    11package MyUtil; 
    22 
    3 BEGIN { 
    4   use base qw/Util::Any/; 
    5   %Util::Any::Utils = 
    6     ( 
    7      List => [qw/List::Util/], 
    8     ); 
    9 } 
     3use base qw/Util::Any/; 
     4%Util::Any::Utils = 
     5  ( 
     6   list => [['List::Util' => 'lu_']], 
     7  ); 
    108 
    1191;