Changeset 32629

Show
Ignore:
Timestamp:
04/20/09 00:23:40 (4 years ago)
Author:
ktat
Message:

fix misunderstanding feature of Sub::Exporter

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

Legend:

Unmodified
Added
Removed
  • lang/perl/Util-Any/trunk/lib/Util/Any.pm

    r32282 r32629  
    3636    foreach my $class (@{$config->{$kind}}) { # $class is class name or array ref 
    3737      my @funcs = @{$funcs->{$kind} || []}; 
    38       ($class, $module_prefix, $options) = ref $class ? @$class : ($class, '', []); 
     38      ($class, $module_prefix, $options) = @$class if ref $class; 
    3939      $prefix = $kind_prefix                             ? $kind_prefix   : 
    4040                ($opt{module_prefix} and $module_prefix) ? $module_prefix : 
     
    5757        } 
    5858        foreach my $o (grep !/^-/, keys %$options) { 
    59           if (defined &{$class . '::' . $o}) { 
     59          if (ref $options->{$o} eq 'CODE') { 
     60            my $gen = $options->{$o}; 
     61            foreach my $def (@{$local_definition->{$o}}) { 
     62              my %arg; 
     63              $arg{$_} = $def->{$_}  for grep !/^-/, keys %$def; 
     64              ExportTo::export_to($caller => {delete $def->{-as} => $gen->($pkg, $class, $o, \%arg)}); 
     65            } 
     66 
     67          } elsif (defined &{$class . '::' . $o}) { 
    6068            push @funcs , $o; 
    6169            $rename{$o} = $options->{$o}; 
     
    6674      } 
    6775      $pkg->_do_export($caller, $class, \@funcs, $local_definition, \%rename, $prefix); 
     76    } 
    6877  } 
    6978} 
     
    8291  if (%$local_definition) { 
    8392    foreach my $func (keys %$local_definition) { 
    84         foreach my $def (@{$local_definition->{$func}}) { 
    85           my $local_rename = delete $def->{-as} || ''; 
     93      foreach my $def (@{$local_definition->{$func}}) { 
     94        my $local_rename = delete $def->{-as} || ''; 
     95        unless (%$def) { 
    8696          ExportTo::export_to 
    8797              ($caller => 
    8898               { 
    8999                ($local_rename ? $local_rename : $prefix ? $prefix . $func : $func) 
    90                   => %$def ? sub { no strict 'refs'; &{$class . '::' . $func}(%$def, @_) } 
    91                          : $class . '::' . $func 
     100                              => $class . '::' . $func 
    92101               }); 
    93102        } 
    94103      } 
    95       @export_funcs = grep !exists $local_definition->{$_}, @export_funcs; 
    96104    } 
    97     ExportTo::export_to($caller => ($prefix or %$rename) 
    98                         ? {map {($prefix . ($rename->{$_} || $_)) => $class . '::' . $_} @export_funcs} 
    99                         : [map $class . '::' . $_, uniq @export_funcs]); 
     105    @export_funcs = grep !exists $local_definition->{$_}, @export_funcs; 
    100106  } 
     107  ExportTo::export_to($caller => ($prefix or %$rename) 
     108                      ? {map {($prefix . ($rename->{$_} || $_)) => $class . '::' . $_} @export_funcs} 
     109                      : [map $class . '::' . $_, uniq @export_funcs]); 
    101110} 
    102111 
    103112sub _insert_want_arg { 
    104   my ($config, $f, $setting, $want, $arg) = @_; 
    105   my $lf = lc $f; 
    106   exists $config->{$lf} ? 
    107     $want->{$lf} = $setting : 
    108     push @$arg, $f, defined $setting ? $setting : (); 
     113  my ($config, $kind, $setting, $want, $arg) = @_; 
     114  my $kind = lc $f; 
     115  exists $config->{$kind} ? 
     116    $want->{$kind} = $setting: 
     117    push @$arg, $kind, defined $setting ? $setting : (); 
    109118} 
    110119 
     
    501510=head3 C<all> cannot be used for key. 
    502511 
    503  NG $Utils = { all => [qw/List::Util/]}; 
     512 NG $Utils = { all    => [qw/List::Util/]}; 
     513 NG $Utils = { -all   => [qw/List::Util/]}; 
     514 NG $Utils = { ':all' => [qw/List::Util/]}; 
    504515 
    505516=head3 Value is array ref which contained scalar or array ref. 
     
    589600=head2 EXPORTING LIKE Sub::Exporter 
    590601 
    591 It's experimental featrue. not enough tested. and only support '-prefix', '-as' and pass default args. 
     602It's experimental featrue. not enough tested. and only support '-prefix' and '-as'. 
    592603 
    593604 use UtilSubExporter list => {-prefix => 'list__', min => {-as => "list___min"}}, 
     
    598609Check t/lib/UtilSubExporter.pm, t/10-sub-exporter-like-epxort.t and  t/12-sub-exporter-like-export.t 
    599610 
     611=head3 Sub::Exporter's generator way 
     612 
     613It's experimental feature, not enough tested. 
    600614 
    601615=head1 WORKING WITH EXPORTER-LIKE MODULES 
     
    606620 
    607621If you want to use module mentioned above, you have to change the way to inherit these modules. 
     622 
     623=head2 DIFFERENCE between 'all' and '-all' or ':all' 
     624 
     625If your utility module which inherited Util::Any has utility functions and export them by Exporter-like module, 
     626behavior of 'all' and '-all' or ':all' is a bit different. 
     627 
     628 'all' ... export all utilities defined in your package's $Utils variables. 
     629 '-all' or ':all' ... export all utilities including functions in your util module itself. 
    608630 
    609631=head2 ALTERNATIVE INHERITING 
  • lang/perl/Util-Any/trunk/t/12-sub-exporter-like-export.t

    r32282 r32629  
    55my $err; 
    66use UtilExporter 
    7   -hello => [hello_name  => {-as => 'hello_ktat', name => 'ktat'}, 
    8              hello_where => {-as => 'hello_japan', where => 'japan'}, 
    9              hello_where => {-as => 'hello_japan', in => 'japan', where => 'osaka'}], 
     7  -hello => [hello_name  => {-as => 'hello_rename'}, 
     8             hey => {-as => 'hey_japan', in => 'japan'}, 
     9             hey => {-as => 'hey_osaka', in => 'japan', at => 'osaka'}], 
    1010 'askme'; 
    1111 
     
    1414use Test::More qw/no_plan/; 
    1515 
    16 ok(defined &hello_ktat,  'defined hello_ktat'); 
    17 ok(defined &hello_japan, 'defined hello_japan'); 
     16ok(defined &hello_rename, 'defined hello_rename'); 
     17ok(defined &hey_japan, 'defined hello_japan'); 
    1818ok(defined &askme,  'defined askme'); 
    1919 
    20 is(hello_ktat(), 'hello, ktat'  , 'hello, ktat'); 
    21 is(hello_japan(), 'hello, japan', 'hello, japan'); 
    22 is(hello_japan(where => "Osaka"), 'hello, Osaka', 'override where'); 
     20is(hello_rename()    , 'hello, ', 'hello_rename'); 
     21is(hey_japan()       , 'hey, japan', 'hey_japan'); 
     22is(hey_japan("Osaka"), 'hey, Osaka in japan', 'hey_japan("Osaka")'); 
     23is(hey_osaka()       , 'hey, osaka in japan', 'hey_osaka'); 
     24is(hey_osaka("Osaka"), 'hey, Osaka in japan', 'hey_osaka("Osaka")'); 
  • lang/perl/Util-Any/trunk/t/lib/UtilExporter.pm

    r32282 r32629  
    1919                 ['List::Util', '', [qw(first min minstr max maxstr sum)]], 
    2020                ]; 
    21 $Utils->{-hello} = ['exampleHello']; 
     21$Utils->{-hello} = [ 
     22                    ['exampleHello' => '', {'hey'   => \&hey_generator}], 
     23                   ]; 
    2224 
    2325sub hello { "hello there" } 
    2426sub askme { "what you will" } 
    2527sub hi    { "hi there" } 
     28sub hello_where { "hello where" } 
     29 
     30sub hey_generator { 
     31  my ($self, $class, $func, $given) = @_; 
     32  my $at = $given->{at}; 
     33  my $in = $given->{in}; 
     34  return sub { 
     35    no strict 'refs'; 
     36    my ($_at, $_in) = @_; 
     37    &{$class . '::' . $func}($_at || $at, $_in || $in); 
     38  } 
     39} 
    2640 
    27411; 
  • lang/perl/Util-Any/trunk/t/lib/UtilSubExporter2.pm

    r32282 r32629  
    3131our $Utils = clone $Util::Any::Utils; 
    3232$Utils->{-l2s} = [ 
    33                  ['List::Util', '', [qw(first min minstr max maxstr sum)]], 
     33                  ['List::Util', '', [qw(first min minstr max maxstr sum)]], 
     34                  { 
     35                   'hoge' => \&hoge_generator, # generator 
     36                   'hogehoge' => 'hello', # &hello 
     37                  } 
    3438                ]; 
    3539 
  • lang/perl/Util-Any/trunk/t/lib/exampleHello.pm

    r32282 r32629  
    66our @EXPORT_OK = qw/hello_name hello_where/; 
    77 
    8 sub hello_name {my %arg = @_; "hello, $arg{name}"} 
     8sub hello_name {my %arg = @_; "hello, " . ($arg{name} || '')} 
    99sub hello_where { 
    10   my %arg = @_; 
    11   return "hello, $arg{where}"; 
     10  my ($at, $in) = @_; 
     11  return @_ > 1 ? "hello, $at in $in" : "hello, $at"; 
     12} 
     13 
     14sub hey { 
     15  my ($at, $in) = @_; 
     16  unless ($at) { 
     17    return "hey, $in"; 
     18  } else { 
     19    return "hey, $at in $in"; 
     20  } 
    1221} 
    1322