Changeset 35510

Show
Ignore:
Timestamp:
10/06/09 03:39:33 (4 years ago)
Author:
ktat
Message:

Checking in changes prior to tagging of version 0.17. Changelog diff is:

=== Changes
==================================================================
--- Changes (revision 39944)
+++ Changes (local)
@@ -1,5 +1,13 @@

Revision history for Util-Any


+0.17 2009/10/06 03:32
+ add -args option to give common arguments to all functions in same kind.
+ enable to work -all with kind setting. for example
+ -all, -list => {...}
+ fix typo: inherit Perl6::Exprot::Attr and ':ALL' keyword didn't work correctly.
+ fix typo: inherit Exproer and ':all' keyword didn't work correctly.
+ Thanks to Richard Jelinek, again.
+

0.16 2009/09/30 03:16

fix bug: using SubExportor?'s generator way and select functions with rename function,

didn't working correctly

@@ -58,7 +66,7 @@


0.07 2009/04/12 03:58

Fix bug when inheriting

- Thanks to Rechard Jelinek
+ Thanks to Richard Jelinek


0.06 2009/03/22 14:55

support Exporter and Exporter::Simple

Location:
lang/perl/Util-Any/trunk
Files:
6 added
9 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Util-Any/trunk/Changes

    r35485 r35510  
    11Revision history for Util-Any 
     2 
     30.17    2009/10/06 03:32 
     4        add -args option to give common arguments to all functions in same kind. 
     5        enable to work -all with kind setting. for example 
     6               -all, -list => {...} 
     7        fix typo: inherit Perl6::Exprot::Attr and ':ALL' keyword didn't work correctly. 
     8        fix typo: inherit Exproer and ':all' keyword didn't work correctly. 
     9        Thanks to  Richard Jelinek, again. 
    210 
    3110.16    2009/09/30 03:16 
     
    59670.07    2009/04/12 03:58 
    6068        Fix bug when inheriting 
    61         Thanks to Rechard Jelinek 
     69        Thanks to  Richard Jelinek 
    6270 
    63710.06    2009/03/22 14:55 
  • lang/perl/Util-Any/trunk/MANIFEST

    r35485 r35510  
    2929t/01-util-kind-all-prefix.t 
    3030t/03-debug.t 
    31 t/03-debug.t 
     31t/03-priority.t 
    3232t/04-option.t 
    3333t/04-option-select.t 
     
    3939t/06-perl6-export-attrs2.t 
    4040t/06-perl6-export-attrs-no-args.t 
     41t/06-perl6-export-attrs-all.t 
    4142t/07-perl6-export-attrs.t 
    4243t/07-perl6-export-attrs2.t 
     
    4546t/08-exporter-simple2.t 
    4647t/08-exporter-simple-no-args.t 
     48t/08-exporter-simple-all.t 
    4749t/09-exporter.t 
    4850t/09-exporter2.t 
     51t/09-exporter-all.t 
    4952t/09-exporter-no-args.t 
     53t/10-sub-exporter-all.t 
    5054t/10-sub-exporter.t 
    5155t/10-sub-exporter2.t 
     
    5761t/11-sub-exporter-full-all.t 
    5862t/12-sub-exporter-like-export.t 
     63t/12-sub-exporter-like-export-with-args.t 
    5964t/13-inherit.t 
    6065t/14-pluggable.t 
  • lang/perl/Util-Any/trunk/lib/Util/Any.pm

    r35485 r35510  
    5757    # Carp::croak "$pkg doesn't have such kind of functions : $kind" 
    5858    # unless exists $config->{$kind}; 
    59     $pkg->_kind_exporter($caller, $config->{$kind}, $kind, $want_kind->{$kind}, \%opt); 
     59    $pkg->_kind_exporter($caller, $config->{$kind}, (lc(join "", $kind =~m{(\w+)}go)), $want_kind->{$kind}, \%opt); 
    6060  } 
    6161} 
    6262 
    6363sub _kind_exporter { 
    64   my ($pkg, $caller, $config, $kind, $import_setting, $opt) = @_; 
    65   my ($funcs, $local_definition, $kind_prefix) = $pkg->_func_definitions($import_setting); 
    66   my ($kind_word, $prefix, %want_funcs, %exported, %class_func) = (lc(join "", $kind =~m{(\w+)}go), ''); 
    67   @want_funcs{@$funcs} = (); 
    68  
    69   foreach my $class_config (@$config) { # $class_config is class name or array ref 
     64  my ($pkg, $caller, $kind_config, $kind_word, $import_setting, $opt) = @_; 
     65  my ($wanted_funcs, $local_definition, $kind_prefix, $kind_args) = $pkg->_func_definitions($import_setting); 
     66  my ($prefix, %exported, %class_func); 
     67 
     68  foreach my $class_config (@$kind_config) { # $class_config is class name or array ref 
    7069    my ($class, $module_prefix, $config_options) = ref $class_config ? @$class_config : ($class_config, '', ''); 
    7170 
     
    9796        @funcs = @{_all_funcs_in_class($class)}; 
    9897      } 
    99       foreach my $function (%want_funcs ? (grep {defined $config_options->{$_}} keys %want_funcs) : grep !/^-/, keys %$config_options) { 
     98      foreach my $function (@$wanted_funcs ? (grep {defined $config_options->{$_}} @$wanted_funcs) : grep !/^-/, keys %$config_options) { 
    10099        if (ref(my $gen = $config_options->{$function}) eq 'CODE') { 
    101100          # Like Sub::Exporter generator 
     
    105104              $arg{$_} = $def->{$_}  for grep !/^-/, keys %$def; 
    106105              ExportTo::export_to($caller => {($def->{-as} || $function) 
    107                                               => $gen->($pkg, $class, $function, \%arg)}); 
    108  
    109  
     106                                              => $gen->($pkg, $class, $function, \%arg, $kind_args)}); 
    110107            } 
    111108          } else { 
    112             ExportTo::export_to($caller => {$prefix . $function => $gen->($pkg, $class, $function, {})}); 
     109            ExportTo::export_to($caller => {$prefix . $function => $gen->($pkg, $class, $function, {}, $kind_args)}); 
    113110          } 
    114111          $exported{$function} = undef; 
     
    123120    $class_func{$class} = [\@funcs, \%rename]; 
    124121  } 
     122  my %want_funcs; 
     123  @want_funcs{@$wanted_funcs} = (); 
    125124  foreach my $class (keys %class_func) { 
    126     $pkg->_do_export($caller, $class, $class_func{$class}->[0], \%want_funcs, \%exported, 
    127                      $local_definition, $class_func{$class}->[1], $prefix, $kind_prefix); 
     125    _do_export($caller, $class, $class_func{$class}->[0], \%want_funcs, \%exported, 
     126               $local_definition, $class_func{$class}->[1], $prefix, $kind_prefix); 
    128127  } 
    129128} 
    130129 
    131130sub _do_export { 
    132   my ($pkg, $caller, $class, $funcs, $want_funcs, $exported, $local_definition, $rename, $prefix, $kind_prefix) = @_; 
    133  
     131  my ($caller, $class, $funcs, $want_funcs, $exported, $local_definition, $rename, $prefix, $kind_prefix) = @_; 
    134132  my %reverse_rename = reverse %$rename; 
    135133  if (%$local_definition) { 
     
    207205  my (@arg, %want_kind); 
    208206  my $import_module = $pkg->_use_import_module; 
     207  my $all_improt = 0; 
    209208  if (@$org_args) { 
    210209    @$org_args = %{$org_args->[0]} if ref $org_args->[0] eq 'HASH'; 
    211210    if (lc($org_args->[0]) =~ /^([:-])?all/) { 
     211      my $all_import = shift @$org_args; 
    212212      my $inherit_all = $1; 
    213213      $pkg->_lazy_load_plugins_all($config) if $opt->{'plugin'} eq 'lazy' and $pkg->can('_plugins'); 
     
    215215      @want_kind{keys %$config} = (); 
    216216      if ($inherit_all and $import_module) { 
    217         if ($import_module eq 'Expoter' or $import_module eq 'Exporter::Simple') { 
     217        if ($import_module eq 'Exporter' or $import_module eq 'Exporter::Simple') { 
    218218          no strict 'refs'; no warnings; 
    219           push @arg, ':all' if ${$pkg . '::EXPORT_TAGS'}{":all"}; 
     219          push @arg, ':all' if ${$pkg . '::EXPORT_TAGS'}{"all"}; 
     220 
    220221        } elsif ($import_module eq 'Sub::Exporter') { 
    221222          push @arg, '-all'; 
    222         } elsif ($import_module eq 'Perl6::Exporter::Attrs') { 
     223        } elsif ($import_module eq 'Perl6::Export::Attrs') { 
    223224          push @arg, ':ALL'; 
    224225        } 
    225226      } 
     227    } elsif ($opt->{'plugin'} eq 'lazy' and $pkg->can('_plugins')) { 
     228      $pkg->_lazy_load_plugins($config, $org_args); 
     229    } 
     230    if (List::MoreUtils::any {ref $_} @$org_args) { 
     231      for (my $i = 0; $i < @$org_args; $i++) { 
     232        my $kind = $org_args->[$i]; 
     233        my $import_setting = $org_args->[$i + 1] ? $org_args->[++$i] : undef; 
     234        _insert_want_arg($config, $kind, $import_setting, \%want_kind, \@arg); 
     235      } 
    226236    } else { 
    227       $pkg->_lazy_load_plugins($config, $org_args) if $opt->{'plugin'} eq 'lazy' and $pkg->can('_plugins'); 
    228       if (List::MoreUtils::any {ref $_} @$org_args) { 
    229         for (my $i = 0; $i < @$org_args; $i++) { 
    230           my $kind = $org_args->[$i]; 
    231           my $import_setting = $org_args->[$i + 1] ? $org_args->[++$i] : undef; 
    232           _insert_want_arg($config, $kind, $import_setting, \%want_kind, \@arg); 
    233         } 
    234       } else { 
    235         # export specified kinds 
    236         foreach my $kind (@$org_args) { 
    237           _insert_want_arg($config, $kind, undef, \%want_kind, \@arg); 
    238         } 
     237      # export specified kinds 
     238      foreach my $kind (@$org_args) { 
     239        _insert_want_arg($config, $kind, undef, \%want_kind, \@arg); 
    239240      } 
    240241    } 
     
    307308sub _func_definitions { 
    308309  my ($pkg, $want_func_definition) = @_; 
    309   my $kind_prefix; 
    310   my (@funcs, %funcs, %local_definition); 
     310  my ($kind_prefix, $kind_args, @wanted_funcs, %funcs, %local_definition); 
    311311  if (ref $want_func_definition eq 'HASH') { 
    312312    # list => {func => {-as => 'rename'}};  list => {-prefix => 'hoge_' } 
    313313    $kind_prefix = $want_func_definition->{-prefix} 
    314314      if exists $want_func_definition->{-prefix}; 
     315    $kind_args = $want_func_definition->{-args} 
     316      if exists $want_func_definition->{-args}; 
    315317    foreach my $f (grep !/^-/, keys %$want_func_definition) { 
    316318      $local_definition{$f} = [$want_func_definition->{$f}]; 
     
    319321    foreach (my $i = 0; $i < @$want_func_definition; $i++) { 
    320322      my ($k, $v) = @{$want_func_definition}[$i, $i + 1]; 
    321       if (ref $v) { 
     323      if ($k eq '-prefix') { 
     324        $kind_prefix = $v; 
    322325        $i++; 
    323         if ($k eq '-prefix') { 
    324           $kind_prefix = $v; 
    325         } else { 
    326           push @funcs, $k; 
    327           push @{$local_definition{$k} ||= []}, $v; 
    328         } 
     326      } elsif ($k eq '-args') { 
     327        $kind_args = $v; 
     328        $i++; 
     329      }elsif (ref $v) { 
     330        $i++; 
     331        push @wanted_funcs, $k; 
     332        push @{$local_definition{$k} ||= []}, $v; 
    329333      } else { 
    330         push @funcs, $k; 
     334        push @wanted_funcs, $k; 
    331335      } 
    332336    } 
    333     @funcs = List::MoreUtils::uniq @funcs; 
    334   } 
    335   return \@funcs, \%local_definition, $kind_prefix || ''; 
     337    @wanted_funcs = List::MoreUtils::uniq @wanted_funcs; 
     338  } 
     339  return \@wanted_funcs, \%local_definition, $kind_prefix || '', $kind_args || {}; 
    336340} 
    337341 
     
    406410=cut 
    407411 
    408 our $VERSION = '0.16'; 
     412our $VERSION = '0.17'; 
    409413 
    410414=head1 SYNOPSIS 
     
    686690functions in -list, are exported with prefix "list__" except 'min' and 'min' is exported as 'lmin'. 
    687691 
     692=head1 PRIORITY OF THE WAYS TO CHANGE FUNCTION NAME 
     693 
     694There are some ways to chnage function name. 
     695Their priority is the following. 
     696 
     697=over 4 
     698 
     699=item 1 rename 
     700 
     701 -list => {uniq => {-as => 'luniq'}} 
     702 
     703=item 2 kind_prefix 
     704 
     705 -list => {-prefix => list} 
     706 
     707=item 3 module_prefix 
     708 
     709Only if module's prefix is defined 
     710 
     711 ..., {module_prefix => 1} 
     712 
     713=item 4 prefix 
     714 
     715 ..., {prefix => 1} 
     716 
     717=item 5 smart_rename 
     718 
     719 ..., {smart_rename => 1} 
     720 
     721=back 
     722 
     723I don't recommend to use 3, 4, 5 in same time, because it may confuse you. 
     724 
     725=over 4 
     726 
     727=item 3 + 4 
     728 
     729if module's prefix is defined in class(not defined in Util::Any), use 3, or use 4. 
     730 
     731=item 3 + 5 
     732 
     7333 or 5. reason is as same as the above. 
     734 
     735=item 3 + 4 + 5 
     736 
     7375 is ignored. 
     738 
     739=item 4 + 5 
     740 
     7415 is ignored. 
     742 
     743=back 
     744 
    688745=head1 NOTE ABOUT all KEYWORD 
    689746 
     
    926983Util::Any try to export duplicate function C<min>, one of both should fail. 
    927984 
     985=head4 GIVE DEFAULT ARGUMENTS TO CODE GENERATOR 
     986 
     987You may want to give default arguments to all code generators in same kind. 
     988For example, if you create shortcut to use Number::Format, 
     989you may want to give common arguments with creating instance. 
     990 
     991 -number => [ 
     992    [ 'Number::Format' => { 
     993        'round' => sub { 
     994            my($pkg, $class, $func, $args, $default_args) = @_; 
     995            my $n = 'Number::Format'->new(%$default_args); 
     996            sub { $n->round(@_); } 
     997        }, 
     998        'number_format' => sub { 
     999            my($pkg, $class, $func, $args, $default_args) = @_; 
     1000            my $n = 'Number::Format'->new(%$default_args, %$args); 
     1001            sub { $n->format_number(@_); } 
     1002        } 
     1003      } 
     1004    ]; 
     1005 
     1006And write as the following: 
     1007 
     1008 use Util::Yours -number => [-args => {thousands_sep => "_", int_curr_symbol => '\'} ]; 
     1009  
     1010 print number_format(100000); # 100_000 
     1011 print number_price(100000);  # \100_000 
     1012 
     1013thousands_sep and int_curr_symbol are given to all of -number kind of function. 
     1014 
    9281015=head2 USE PLUGGABLE FEATURE FOR YOUR MODULE 
    9291016 
     
    9701057 
    9711058NOTE THAT: I don't recommend this usage, because using this may confuse user; 
    972 some of import options are for Util::Any and others are for exporter-like module. 
     1059some of import options are for Util::Any and others are for exporter-like module 
     1060(especially, ussing with Sub::Exporter is confusing). 
    9731061 
    9741062CPAN has some modules to export functions. 
  • lang/perl/Util-Any/trunk/t/01-synopsis.t

    r35387 r35510  
    8383ok(!defined &l_uniq); 
    8484 
     85package EE; 
     86use Test::More; 
     87 
     88use Util::Any -list => {uniq => {-as => 'li_uniq'}, -prefix => "ll_"}, {smart_rename => 1}; 
     89is_deeply([li_uniq qw/1 0 1 2 3 3/], [1,0,2,3]); 
     90is(ll_min(qw/10 9 8 4 5 7/), 4); 
     91ok(!defined &ll_uniq); 
     92 
  • lang/perl/Util-Any/trunk/t/05-smart-rename.t

    r35365 r35510  
    11use Test::More 'no_plan'; 
    22use lib qw(./lib t/lib); 
     3 
     4package AA; 
     5use Test::More; 
     6 
    37use SmartRename -utf8, {smart_rename => 1}; 
    48 
     
    913ok(defined(&utf8_downgrade)); 
    1014 
     15package BB; 
     16use Test::More; 
     17 
     18use SmartRename -utf8 => {is_utf8 => {-as => 'utf_flagged'}, -prefix => 'xx_', smart_rename => 1}; 
     19 
     20ok(!defined(&xx_is_utf8)); 
     21ok(defined(&utf_flagged)); 
     22ok(defined(&xx_utf8_upgrade)); 
     23ok(defined(&xx_downgrade)); 
     24 
  • lang/perl/Util-Any/trunk/t/11-sub-exporter-all.t

    r32282 r35510  
    55my $err; 
    66BEGIN { 
    7   eval "use UtilSubExporter 'all'"; 
     7  eval "use UtilSubExporter '-all'"; 
    88  $err = $@; 
    99} 
     
    1818ok(defined &min,    'defined min'); 
    1919ok(defined &minstr, 'defined minstr'); 
    20 ok(!defined &hello,       'not defined hello'); 
    21 ok(!defined &hi,          'not defined hi'); 
    22 ok(!defined &askme,       'not defined askme'); 
     20ok(defined &hello,       'defined hello'); 
     21ok(defined &hi,          'defined hi'); 
     22ok(defined &askme,       'defined askme'); 
    2323 
    2424is((first {$_ >= 4} (2,10,4,3,5)), 10, 'list first'); 
  • lang/perl/Util-Any/trunk/t/lib/SubExporterGenerator.pm

    r35485 r35510  
    2121              max => \&build_max_reformatter, 
    2222              hoge => sub { sub () {"hogehoge"}}, 
     23              check_default => \&check_default, 
    2324             } 
    2425            ], 
     
    4546} 
    4647 
     48sub check_default { 
     49  my ($pkg, $class, $name, $args, $default_args) = @_; 
     50  sub { 
     51    return $default_args; 
     52  } 
     53} 
     54 
    47551; 
  • lang/perl/Util-Any/trunk/t/lib/UtilExporter.pm

    r32629 r35510  
    1313                    'us'    => [qw/hi/], 
    1414                    'hello' => [qw/hello_name hello_where/], 
     15                    'all'   => [qw/hello hi askme/], 
    1516                   ); 
    1617 
  • lang/perl/Util-Any/trunk/t/lib/UtilPerl6ExportAttrs.pm

    r35474 r35510  
    3434                ]; 
    3535 
    36 sub import { 
    37   my $pkg = shift; 
    38   my $caller = (caller)[0]; 
    39  
    40   no strict 'refs'; 
    41   eval "package $caller; $pkg" . '->Util::Any::import(@_);'; 
    42   my @arg = grep !exists $Utils->{$_}, @_; 
    43   if (@_ and @arg) { 
    44     eval "package $caller; $pkg" . '->Perl6::Export::Attrs::_generic_import(@arg)'; 
    45   } elsif (!@_) { 
    46     eval "package $caller; $pkg" . '->Perl6::Export::Attrs::_generic_import'; 
    47   } 
    48   return; 
    49 } 
     36## sub import { 
     37##   my $pkg = shift; 
     38##   my $caller = (caller)[0]; 
     39##  
     40##   no strict 'refs'; 
     41##   eval "package $caller; $pkg" . '->Util::Any::import(@_);'; 
     42##   my @arg = grep !exists $Utils->{$_}, @_; 
     43##   if (@_ and @arg) { 
     44##     eval "package $caller; $pkg" . '->Perl6::Export::Attrs::_generic_import(@arg)'; 
     45##   } elsif (!@_) { 
     46##     eval "package $caller; $pkg" . '->Perl6::Export::Attrs::_generic_import'; 
     47##   } 
     48##   return; 
     49## } 
    5050 
    51511;