Chinaunix首页 | 论坛 | 博客
  • 博客访问: 225714
  • 博文数量: 36
  • 博客积分: 1188
  • 博客等级: 军士长
  • 技术积分: 802
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 21:45
文章分类

全部博文(36)

文章存档

2020年(1)

2017年(2)

2015年(1)

2014年(1)

2013年(1)

2012年(3)

2011年(27)

分类: WINDOWS

2011-03-22 12:11:54

001Moose Person
  1. package Person;
  2.  use Moose;

  3.  has id => ( is => 'rw' , isa => 'Int' , required => 1 ,default => 0 );
  4.  has name => ( is => 'rw' , isa => 'Str' , );

  5.  use Moose::Util::TypeConstraints;
  6.  subtype 'Telephone'
  7.       => as 'Int'
  8.       => where { $_ =~ /^\d+$/};

  9.  no Moose::Util::TypeConstraints;
  10.  has telephone => (
  11.       is => 'rw',
  12.       isa => 'Telephone' ,
  13. );


  14. package User;
  15.  use Moose;
  16.  extends 'Person';
  17.  has login => ( is => 'rw', isa => 'Str', );

  18. sub do_login {
  19. my ($self, $password) = @_;
  20. print '用户:'.$self->login.'; 密码:'.$password;

  21. }

  22. before do_login => sub {
  23.  my ( $self, $pass ) = @_;
  24.  warn "Called login() with $pass \n";
  25. };

  26. after do_login => sub {
  27.  my $self=shift;
  28.  print '; Telephone:'.$self->telephone,"\n";
  29. };

  30.  around name => sub {
  31.  my ($orig, $self, @args) = @_;
  32.  return ucfirst lc $self->$orig( @args ) ;

  33. };


  34. package main;
  35. my $user = new User(id => 123, name =>'john');
  36. $user->login($user->name);
  37. print $user->name,"\n";
  38. print $user->id,"\n";
  39. print $user->telephone(311223234),"\n";
  40. $user->do_login(123456);

002Moose::Role Person

  1. package Person;
  2.  use Moose::Role;

  3.  has id => ( is => 'rw' , isa => 'Int' , required => 1 ,default => 0 );
  4.  has name => ( is => 'rw' , isa => 'Str' , );

  5.  use Moose::Util::TypeConstraints;
  6.  subtype 'Telephone'
  7.       => as 'Int'
  8.       => where { $_ =~ /^\d+$/};

  9.  no Moose::Util::TypeConstraints;
  10.  has telephone => (
  11.       is => 'rw',
  12.       isa => 'Telephone' ,
  13. );


  14. package User;
  15.  use Moose;
  16.  with 'Person';
  17.  has login => ( is => 'rw', isa => 'Str', );

  18. sub do_login {
  19. my ($self, $password) = @_;
  20. print '用户:'.$self->login.'; 密码:'.$password;

  21. }

  22. before do_login => sub {
  23.  my ( $self, $pass ) = @_;
  24.  warn "Called login() with $pass \n";
  25. };

  26. after do_login => sub {
  27.  my $self=shift;
  28.  print '; Telephone:'.$self->telephone,"\n";
  29. };

  30.  around name => sub {
  31.  my ($orig, $self, @args) = @_;
  32.  return ucfirst lc $self->$orig( @args ) ;

  33. };


  34. package main;
  35. my $user = new User(id => 123, name =>'john');
  36. $user->login($user->name);
  37. print $user->name,"\n";
  38. print $user->id,"\n";
  39. print $user->telephone(311223234),"\n";
  40. $user->do_login(123456);
003Moo***::Declare Person
  1. use Moo***::Declare;

  2. class Person {

  3.     has id => ( is => 'rw', isa => 'Int', required => 1, default => 0 );
  4.     has name => ( is => 'rw', isa => 'Str', );

  5.     use Moose::Util::TypeConstraints;
  6.     subtype 'Telephone' => as 'Int' => where { $_ =~ /^\d+$/ };

  7.     no Moose::Util::TypeConstraints;
  8.     has telephone => (
  9.         is => 'rw',
  10.         isa => 'Telephone',
  11.     );
  12. }

  13. class User extends Person {

  14.     has login => ( is => 'rw', isa => 'Str', );

  15.     method do_login( Num $password) {
  16.         print '用户:' . $self->login . '; 密码:' . $password;

  17.       }

  18.       before do_login( Num $password) {
  19.         warn "Called login() with $password\n";

  20.       };

  21.     after do_login {
  22.         print '; Telephone:' . $self->telephone, "\n";

  23.     };

  24.     around name( Str @args ) {
  25.         return ucfirst lc $self->$orig(@args);

  26.     };
  27. }

  28. # Main program

  29. my $user = new User( id => 123, name => 'john' );
  30. $user->login( $user->name );
  31. print $user->name, "\n";

  32. print $user->telephone(311223234), "\n";
  33. $user->do_login(123456);

 

004Moo Person

  1. package Person;
  2. use Moo;
  3. use Sub::Quote;

  4. has id => (
  5.     is => 'rw',
  6.     isa => quote_sub q{ die unless $_[0] == int($_[0]) }
  7. );
  8. has name => (
  9.     is => 'rw',
  10.     isa => quote_sub q{ die unless $_[0] =~ /^\D+$/}
  11. );
  12.  has telephone => (
  13.       is => 'rw',
  14.       isa => quote_sub q{ die unless $_[0] =~ /^\d+$/}
  15. );
  16. package User;
  17. use Moo;
  18. use Sub::Quote;
  19. extends 'Person';
  20. has login => (
  21.     is => 'rw',
  22.     isa => quote_sub q{ die unless $_[0] =~ m/^\D+$/}
  23. );

  24. sub do_login {
  25.     my ( $self, $password ) = @_;
  26.     print '用户:' . $self->login . '; 密码:' . $password;

  27. }

  28. before do_login => sub {
  29.     my ( $self, $pass ) = @_;
  30.     warn "Called login() with $pass \n";
  31. };

  32. after do_login => sub {
  33.     my $self = shift;
  34.     print '; Telephone:' . $self->telephone, "\n";
  35. };

  36. around name => sub {
  37.     my ( $orig, $self, @args ) = @_;
  38.     return ucfirst lc $self->$orig(@args);

  39. };

  40. package main;
  41. my $user = new User( id => 123, name => 'john' );
  42. $user->login( $user->name );
  43. print $user->name, "\n";
  44. print $user->id, "\n";
  45. print $user->telephone(311223234), "\n";
  46. $user->do_login(123456);
阅读(1325) | 评论(3) | 转发(0) |
0

上一篇:没有了

下一篇:Moose handles的一个例子

给主人留下些什么吧!~~

rubyish2011-09-01 20:11:45

应该

aef25u2011-03-30 13:14:22

kingwmj: 那个dancer的代码,你也应该放到博客里来。.....
可以考虑,到时将内容丰富一下再放进来吧

kingwmj2011-03-30 08:52:05

那个dancer的代码,你也应该放到博客里来。