Changeset 31434

Show
Ignore:
Timestamp:
03/22/09 04:24:59 (4 years ago)
Author:
ktat
Message:

to use Perl6::Export::Attrs

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

Legend:

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

    r26846 r31434  
    11Revision history for Util-Any 
     2 
     30.05    2009/03/22 02:35 
     4        solve the problem using Util::Any with Perl6::Export::Atttrs 
     5        new feature to inherit Util::Any(-Base and -Perl6ExportAttrs) 
    26 
    370.04    2008/12/14 17:34 
  • lang/perl/Util-Any/trunk/MANIFEST

    r26846 r31434  
    2424t/04-option-select.t 
    2525t/04-option-except.t 
     26t/05-util_any_base-prefix.t 
     27t/05-util_any_base.t 
     28t/06-perl6-export-attrs.t 
     29t/06-perl6-export-attrs2.t 
     30t/06-perl6-export-attrs-no-args.t 
     31t/07-perl6-export-attrs.t 
     32t/07-perl6-export-attrs2.t 
     33t/07-perl6-export-attrs-no-args.t 
    2634t/lib/MyUtil.pm 
     35t/lib/MyUtilBase.pm 
     36t/lib/UtilPerl6ExportAttrs.pm 
     37t/lib/UtilPerl6ExportAttrsBase.pm 
  • lang/perl/Util-Any/trunk/lib/Util/Any.pm

    r26846 r31434  
    1717sub import { 
    1818  my $pkg = shift; 
     19  my $caller = (caller)[0]; 
     20 
     21  return $pkg->_base_import($caller, @_) if @_ and $_[0] =~/^-\w+$/; 
    1922 
    2023  no strict 'refs'; 
    2124 
    2225  my $config = ${$pkg . '::Utils'}; 
    23   my $caller = (caller)[0]; 
    2426  my %want; 
    2527  my %opt = (prefix => 0, module_prefix => 0, debug => 0); 
     
    9496    } 
    9597  } 
     98  if ($pkg->_use_perl6_export_attrs) { 
     99    no strict 'refs'; 
     100    no warnings; 
     101    my $pkg_utils = ${$pkg . '::Utils'}; 
     102    my @arg = defined $pkg_utils ? (grep !exists $pkg_utils->{$_}, @_) 
     103                                 : (grep !exists $Utils->{$_}, @_); 
     104    if (@_ and @arg) { 
     105      eval "package $caller; $pkg" . '->Perl6::Export::Attrs::_generic_import(@arg);'; 
     106    } elsif (!@_) { 
     107      eval "package $caller; $pkg" . '->Perl6::Export::Attrs::_generic_import;'; 
     108    } 
     109  } 
    96110} 
    97111 
     112sub _base_import { 
     113  my ($pkg, $caller, @flgs) = @_; 
     114  { 
     115    no strict 'refs'; 
     116    push @{"${caller}::ISA"}, __PACKAGE__; 
     117  } 
     118 
     119  while (my $flg = shift @flgs) { 
     120    if (lc($flg) eq '-perl6exportattrs') { 
     121      eval "use Perl6::Export::Attrs ();"; 
     122      no strict 'refs'; 
     123      *{$caller . '::MODIFY_CODE_ATTRIBUTES'} = \&Perl6::Export::Attrs::_generic_MCA; 
     124      *{$caller . '::_use_perl6_export_attrs'} = sub { 1 }; 
     125    } 
     126  } 
     127} 
     128 
     129sub _use_perl6_export_attrs { 0 } 
     130 
    98131=head1 NAME 
    99132 
     
    106139=cut 
    107140 
    108 our $VERSION = '0.04'; 
     141our $VERSION = '0.05'; 
    109142 
    110143=head1 SYNOPSIS 
     
    325358  
    326359 use Clone qw/clone/; 
    327  use base qw/Util::Any/; 
     360 use Util::Any -Base; # or use base qw/Util::Any/; 
    328361 our $Utils = clone $Util::Any::Utils; 
    329362 push @{$Utils->{list}}, qw/Your::Favorite::List::Utils/; 
     
    334367 
    335368 use Util::Yours qw/list/; 
     369 
     370=head1 USE Perl6::Export::Attrs in YOUR OWN UTIL MODULE 
     371 
     372Perl6::Export::Attrs overrides caller package's import method. 
     373So, when your module use Perl6::Export::Attrs, Util::Any cannot work. 
     374 
     375Util::Any provides option to solve this prolblem. 
     376Write the follwoing instead of "use Util::Any -Base" or "use base qw/Util::Any/". 
     377 
     378 use Util::Any -Perl6ExportAttrs; 
     379 
     380example; 
     381 
     382 package Util::Yours; 
     383  
     384 use Clone qw/clone/; 
     385 use Util::Any -Perl6ExportAttrs; 
     386 our $Utils = clone $Util::Any::Utils; 
     387 push @{$Utils->{list}}, qw/Your::Favorite::List::Utils/; 
     388  
     389 sub foo :Export(:DEFAULT) { 
     390   return "foo!"; 
     391 } 
     392  
     393 sub bar :Export(:bar) { 
     394   return "bar!"; 
     395 } 
     396  
     397 1; 
     398 
     399Or you can write your own import method and BEGIN block like the follwoing. 
     400 
     401 package UtilPerl6ExportAttr; 
     402  
     403 use strict; 
     404 use base qw/Util::Any/; 
     405 use Clone qw/clone/; 
     406  
     407 BEGIN { 
     408   use Perl6::Export::Attrs (); 
     409   no strict 'refs'; 
     410   *{__PACKAGE__ . '::MODIFY_CODE_ATTRIBUTES'} = \&Perl6::Export::Attrs::_generic_MCA; 
     411 } 
     412  
     413 our $Utils = clone $Util::Any::Utils; 
     414 $Utils->{your_list} = [ 
     415                  ['List::Util', '', [qw(first min sum)]], 
     416                 ]; 
     417  
     418 sub import { 
     419   my $pkg = shift; 
     420   my $caller = (caller)[0]; 
     421  
     422   no strict 'refs'; 
     423   eval "package $caller; $pkg" . '->Util::Any::import(@_);'; 
     424   my @arg = grep !exists $Utils->{$_}, @_; 
     425   if (@_ and @arg) { 
     426     eval "package $caller; $pkg" . '->Perl6::Export::Attrs::_generic_import(@arg)'; 
     427   } elsif (!@_) { 
     428     eval "package $caller; $pkg" . '->Perl6::Export::Attrs::_generic_import'; 
     429   } 
     430   return; 
     431 } 
     432  
     433 sub foo :Export(:DEFAULT) { 
     434   return "foo!"; 
     435 } 
     436  
     437 1; 
    336438 
    337439=head2 $Utils STRUCTURE