Changeset 32629
- Timestamp:
- 04/20/09 00:23:40 (4 years ago)
- Location:
- lang/perl/Util-Any/trunk
- Files:
-
- 1 added
- 5 modified
-
lib/Util/Any.pm (modified) (8 diffs)
-
t/01-util.t (added)
-
t/12-sub-exporter-like-export.t (modified) (2 diffs)
-
t/lib/UtilExporter.pm (modified) (1 diff)
-
t/lib/UtilSubExporter2.pm (modified) (1 diff)
-
t/lib/exampleHello.pm (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Util-Any/trunk/lib/Util/Any.pm
r32282 r32629 36 36 foreach my $class (@{$config->{$kind}}) { # $class is class name or array ref 37 37 my @funcs = @{$funcs->{$kind} || []}; 38 ($class, $module_prefix, $options) = ref $class ? @$class : ($class, '', []);38 ($class, $module_prefix, $options) = @$class if ref $class; 39 39 $prefix = $kind_prefix ? $kind_prefix : 40 40 ($opt{module_prefix} and $module_prefix) ? $module_prefix : … … 57 57 } 58 58 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}) { 60 68 push @funcs , $o; 61 69 $rename{$o} = $options->{$o}; … … 66 74 } 67 75 $pkg->_do_export($caller, $class, \@funcs, $local_definition, \%rename, $prefix); 76 } 68 77 } 69 78 } … … 82 91 if (%$local_definition) { 83 92 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) { 86 96 ExportTo::export_to 87 97 ($caller => 88 98 { 89 99 ($local_rename ? $local_rename : $prefix ? $prefix . $func : $func) 90 => %$def ? sub { no strict 'refs'; &{$class . '::' . $func}(%$def, @_) } 91 : $class . '::' . $func 100 => $class . '::' . $func 92 101 }); 93 102 } 94 103 } 95 @export_funcs = grep !exists $local_definition->{$_}, @export_funcs;96 104 } 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; 100 106 } 107 ExportTo::export_to($caller => ($prefix or %$rename) 108 ? {map {($prefix . ($rename->{$_} || $_)) => $class . '::' . $_} @export_funcs} 109 : [map $class . '::' . $_, uniq @export_funcs]); 101 110 } 102 111 103 112 sub _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 : (); 109 118 } 110 119 … … 501 510 =head3 C<all> cannot be used for key. 502 511 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/]}; 504 515 505 516 =head3 Value is array ref which contained scalar or array ref. … … 589 600 =head2 EXPORTING LIKE Sub::Exporter 590 601 591 It's experimental featrue. not enough tested. and only support '-prefix' , '-as' and pass default args.602 It's experimental featrue. not enough tested. and only support '-prefix' and '-as'. 592 603 593 604 use UtilSubExporter list => {-prefix => 'list__', min => {-as => "list___min"}}, … … 598 609 Check t/lib/UtilSubExporter.pm, t/10-sub-exporter-like-epxort.t and t/12-sub-exporter-like-export.t 599 610 611 =head3 Sub::Exporter's generator way 612 613 It's experimental feature, not enough tested. 600 614 601 615 =head1 WORKING WITH EXPORTER-LIKE MODULES … … 606 620 607 621 If 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 625 If your utility module which inherited Util::Any has utility functions and export them by Exporter-like module, 626 behavior 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. 608 630 609 631 =head2 ALTERNATIVE INHERITING -
lang/perl/Util-Any/trunk/t/12-sub-exporter-like-export.t
r32282 r32629 5 5 my $err; 6 6 use UtilExporter 7 -hello => [hello_name => {-as => 'hello_ ktat', name => 'ktat'},8 he llo_where => {-as => 'hello_japan', where=> 'japan'},9 he llo_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'}], 10 10 'askme'; 11 11 … … 14 14 use Test::More qw/no_plan/; 15 15 16 ok(defined &hello_ ktat, 'defined hello_ktat');17 ok(defined &he llo_japan,'defined hello_japan');16 ok(defined &hello_rename, 'defined hello_rename'); 17 ok(defined &hey_japan, 'defined hello_japan'); 18 18 ok(defined &askme, 'defined askme'); 19 19 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'); 20 is(hello_rename() , 'hello, ', 'hello_rename'); 21 is(hey_japan() , 'hey, japan', 'hey_japan'); 22 is(hey_japan("Osaka"), 'hey, Osaka in japan', 'hey_japan("Osaka")'); 23 is(hey_osaka() , 'hey, osaka in japan', 'hey_osaka'); 24 is(hey_osaka("Osaka"), 'hey, Osaka in japan', 'hey_osaka("Osaka")'); -
lang/perl/Util-Any/trunk/t/lib/UtilExporter.pm
r32282 r32629 19 19 ['List::Util', '', [qw(first min minstr max maxstr sum)]], 20 20 ]; 21 $Utils->{-hello} = ['exampleHello']; 21 $Utils->{-hello} = [ 22 ['exampleHello' => '', {'hey' => \&hey_generator}], 23 ]; 22 24 23 25 sub hello { "hello there" } 24 26 sub askme { "what you will" } 25 27 sub hi { "hi there" } 28 sub hello_where { "hello where" } 29 30 sub 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 } 26 40 27 41 1; -
lang/perl/Util-Any/trunk/t/lib/UtilSubExporter2.pm
r32282 r32629 31 31 our $Utils = clone $Util::Any::Utils; 32 32 $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 } 34 38 ]; 35 39 -
lang/perl/Util-Any/trunk/t/lib/exampleHello.pm
r32282 r32629 6 6 our @EXPORT_OK = qw/hello_name hello_where/; 7 7 8 sub hello_name {my %arg = @_; "hello, $arg{name}"}8 sub hello_name {my %arg = @_; "hello, " . ($arg{name} || '')} 9 9 sub 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 14 sub hey { 15 my ($at, $in) = @_; 16 unless ($at) { 17 return "hey, $in"; 18 } else { 19 return "hey, $at in $in"; 20 } 12 21 } 13 22
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)