Changeset 35510
- Timestamp:
- 10/06/09 03:39:33 (4 years ago)
- Location:
- lang/perl/Util-Any/trunk
- Files:
-
- 6 added
- 9 modified
-
Changes (modified) (2 diffs)
-
MANIFEST (modified) (4 diffs)
-
lib/Util/Any.pm (modified) (12 diffs)
-
t/01-synopsis.t (modified) (1 diff)
-
t/03-priority.t (added)
-
t/05-smart-rename.t (modified) (2 diffs)
-
t/06-perl6-export-attrs-all.t (added)
-
t/08-exporter-simple-all.t (added)
-
t/09-exporter-all.t (added)
-
t/10-sub-exporter-all.t (added)
-
t/11-sub-exporter-all.t (modified) (2 diffs)
-
t/12-sub-exporter-like-export-with-args.t (added)
-
t/lib/SubExporterGenerator.pm (modified) (2 diffs)
-
t/lib/UtilExporter.pm (modified) (1 diff)
-
t/lib/UtilPerl6ExportAttrs.pm (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Util-Any/trunk/Changes
r35485 r35510 1 1 Revision history for Util-Any 2 3 0.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. 2 10 3 11 0.16 2009/09/30 03:16 … … 59 67 0.07 2009/04/12 03:58 60 68 Fix bug when inheriting 61 Thanks to Rechard Jelinek69 Thanks to Richard Jelinek 62 70 63 71 0.06 2009/03/22 14:55 -
lang/perl/Util-Any/trunk/MANIFEST
r35485 r35510 29 29 t/01-util-kind-all-prefix.t 30 30 t/03-debug.t 31 t/03- debug.t31 t/03-priority.t 32 32 t/04-option.t 33 33 t/04-option-select.t … … 39 39 t/06-perl6-export-attrs2.t 40 40 t/06-perl6-export-attrs-no-args.t 41 t/06-perl6-export-attrs-all.t 41 42 t/07-perl6-export-attrs.t 42 43 t/07-perl6-export-attrs2.t … … 45 46 t/08-exporter-simple2.t 46 47 t/08-exporter-simple-no-args.t 48 t/08-exporter-simple-all.t 47 49 t/09-exporter.t 48 50 t/09-exporter2.t 51 t/09-exporter-all.t 49 52 t/09-exporter-no-args.t 53 t/10-sub-exporter-all.t 50 54 t/10-sub-exporter.t 51 55 t/10-sub-exporter2.t … … 57 61 t/11-sub-exporter-full-all.t 58 62 t/12-sub-exporter-like-export.t 63 t/12-sub-exporter-like-export-with-args.t 59 64 t/13-inherit.t 60 65 t/14-pluggable.t -
lang/perl/Util-Any/trunk/lib/Util/Any.pm
r35485 r35510 57 57 # Carp::croak "$pkg doesn't have such kind of functions : $kind" 58 58 # 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); 60 60 } 61 61 } 62 62 63 63 sub _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 70 69 my ($class, $module_prefix, $config_options) = ref $class_config ? @$class_config : ($class_config, '', ''); 71 70 … … 97 96 @funcs = @{_all_funcs_in_class($class)}; 98 97 } 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) { 100 99 if (ref(my $gen = $config_options->{$function}) eq 'CODE') { 101 100 # Like Sub::Exporter generator … … 105 104 $arg{$_} = $def->{$_} for grep !/^-/, keys %$def; 106 105 ExportTo::export_to($caller => {($def->{-as} || $function) 107 => $gen->($pkg, $class, $function, \%arg)}); 108 109 106 => $gen->($pkg, $class, $function, \%arg, $kind_args)}); 110 107 } 111 108 } 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)}); 113 110 } 114 111 $exported{$function} = undef; … … 123 120 $class_func{$class} = [\@funcs, \%rename]; 124 121 } 122 my %want_funcs; 123 @want_funcs{@$wanted_funcs} = (); 125 124 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); 128 127 } 129 128 } 130 129 131 130 sub _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) = @_; 134 132 my %reverse_rename = reverse %$rename; 135 133 if (%$local_definition) { … … 207 205 my (@arg, %want_kind); 208 206 my $import_module = $pkg->_use_import_module; 207 my $all_improt = 0; 209 208 if (@$org_args) { 210 209 @$org_args = %{$org_args->[0]} if ref $org_args->[0] eq 'HASH'; 211 210 if (lc($org_args->[0]) =~ /^([:-])?all/) { 211 my $all_import = shift @$org_args; 212 212 my $inherit_all = $1; 213 213 $pkg->_lazy_load_plugins_all($config) if $opt->{'plugin'} eq 'lazy' and $pkg->can('_plugins'); … … 215 215 @want_kind{keys %$config} = (); 216 216 if ($inherit_all and $import_module) { 217 if ($import_module eq 'Expo ter' or $import_module eq 'Exporter::Simple') {217 if ($import_module eq 'Exporter' or $import_module eq 'Exporter::Simple') { 218 218 no strict 'refs'; no warnings; 219 push @arg, ':all' if ${$pkg . '::EXPORT_TAGS'}{":all"}; 219 push @arg, ':all' if ${$pkg . '::EXPORT_TAGS'}{"all"}; 220 220 221 } elsif ($import_module eq 'Sub::Exporter') { 221 222 push @arg, '-all'; 222 } elsif ($import_module eq 'Perl6::Export er::Attrs') {223 } elsif ($import_module eq 'Perl6::Export::Attrs') { 223 224 push @arg, ':ALL'; 224 225 } 225 226 } 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 } 226 236 } 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); 239 240 } 240 241 } … … 307 308 sub _func_definitions { 308 309 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); 311 311 if (ref $want_func_definition eq 'HASH') { 312 312 # list => {func => {-as => 'rename'}}; list => {-prefix => 'hoge_' } 313 313 $kind_prefix = $want_func_definition->{-prefix} 314 314 if exists $want_func_definition->{-prefix}; 315 $kind_args = $want_func_definition->{-args} 316 if exists $want_func_definition->{-args}; 315 317 foreach my $f (grep !/^-/, keys %$want_func_definition) { 316 318 $local_definition{$f} = [$want_func_definition->{$f}]; … … 319 321 foreach (my $i = 0; $i < @$want_func_definition; $i++) { 320 322 my ($k, $v) = @{$want_func_definition}[$i, $i + 1]; 321 if (ref $v) { 323 if ($k eq '-prefix') { 324 $kind_prefix = $v; 322 325 $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; 329 333 } else { 330 push @ funcs, $k;334 push @wanted_funcs, $k; 331 335 } 332 336 } 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 || {}; 336 340 } 337 341 … … 406 410 =cut 407 411 408 our $VERSION = '0.1 6';412 our $VERSION = '0.17'; 409 413 410 414 =head1 SYNOPSIS … … 686 690 functions in -list, are exported with prefix "list__" except 'min' and 'min' is exported as 'lmin'. 687 691 692 =head1 PRIORITY OF THE WAYS TO CHANGE FUNCTION NAME 693 694 There are some ways to chnage function name. 695 Their 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 709 Only 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 723 I 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 729 if module's prefix is defined in class(not defined in Util::Any), use 3, or use 4. 730 731 =item 3 + 5 732 733 3 or 5. reason is as same as the above. 734 735 =item 3 + 4 + 5 736 737 5 is ignored. 738 739 =item 4 + 5 740 741 5 is ignored. 742 743 =back 744 688 745 =head1 NOTE ABOUT all KEYWORD 689 746 … … 926 983 Util::Any try to export duplicate function C<min>, one of both should fail. 927 984 985 =head4 GIVE DEFAULT ARGUMENTS TO CODE GENERATOR 986 987 You may want to give default arguments to all code generators in same kind. 988 For example, if you create shortcut to use Number::Format, 989 you 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 1006 And 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 1013 thousands_sep and int_curr_symbol are given to all of -number kind of function. 1014 928 1015 =head2 USE PLUGGABLE FEATURE FOR YOUR MODULE 929 1016 … … 970 1057 971 1058 NOTE 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. 1059 some of import options are for Util::Any and others are for exporter-like module 1060 (especially, ussing with Sub::Exporter is confusing). 973 1061 974 1062 CPAN has some modules to export functions. -
lang/perl/Util-Any/trunk/t/01-synopsis.t
r35387 r35510 83 83 ok(!defined &l_uniq); 84 84 85 package EE; 86 use Test::More; 87 88 use Util::Any -list => {uniq => {-as => 'li_uniq'}, -prefix => "ll_"}, {smart_rename => 1}; 89 is_deeply([li_uniq qw/1 0 1 2 3 3/], [1,0,2,3]); 90 is(ll_min(qw/10 9 8 4 5 7/), 4); 91 ok(!defined &ll_uniq); 92 -
lang/perl/Util-Any/trunk/t/05-smart-rename.t
r35365 r35510 1 1 use Test::More 'no_plan'; 2 2 use lib qw(./lib t/lib); 3 4 package AA; 5 use Test::More; 6 3 7 use SmartRename -utf8, {smart_rename => 1}; 4 8 … … 9 13 ok(defined(&utf8_downgrade)); 10 14 15 package BB; 16 use Test::More; 17 18 use SmartRename -utf8 => {is_utf8 => {-as => 'utf_flagged'}, -prefix => 'xx_', smart_rename => 1}; 19 20 ok(!defined(&xx_is_utf8)); 21 ok(defined(&utf_flagged)); 22 ok(defined(&xx_utf8_upgrade)); 23 ok(defined(&xx_downgrade)); 24 -
lang/perl/Util-Any/trunk/t/11-sub-exporter-all.t
r32282 r35510 5 5 my $err; 6 6 BEGIN { 7 eval "use UtilSubExporter ' all'";7 eval "use UtilSubExporter '-all'"; 8 8 $err = $@; 9 9 } … … 18 18 ok(defined &min, 'defined min'); 19 19 ok(defined &minstr, 'defined minstr'); 20 ok( !defined &hello, 'notdefined hello');21 ok( !defined &hi, 'notdefined hi');22 ok( !defined &askme, 'notdefined askme');20 ok(defined &hello, 'defined hello'); 21 ok(defined &hi, 'defined hi'); 22 ok(defined &askme, 'defined askme'); 23 23 24 24 is((first {$_ >= 4} (2,10,4,3,5)), 10, 'list first'); -
lang/perl/Util-Any/trunk/t/lib/SubExporterGenerator.pm
r35485 r35510 21 21 max => \&build_max_reformatter, 22 22 hoge => sub { sub () {"hogehoge"}}, 23 check_default => \&check_default, 23 24 } 24 25 ], … … 45 46 } 46 47 48 sub check_default { 49 my ($pkg, $class, $name, $args, $default_args) = @_; 50 sub { 51 return $default_args; 52 } 53 } 54 47 55 1; -
lang/perl/Util-Any/trunk/t/lib/UtilExporter.pm
r32629 r35510 13 13 'us' => [qw/hi/], 14 14 'hello' => [qw/hello_name hello_where/], 15 'all' => [qw/hello hi askme/], 15 16 ); 16 17 -
lang/perl/Util-Any/trunk/t/lib/UtilPerl6ExportAttrs.pm
r35474 r35510 34 34 ]; 35 35 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 ## } 50 50 51 51 1;
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)