001Moose Person
- package Person;
- use Moose;
- has id => ( is => 'rw' , isa => 'Int' , required => 1 ,default => 0 );
- has name => ( is => 'rw' , isa => 'Str' , );
- use Moose::Util::TypeConstraints;
- subtype 'Telephone'
- => as 'Int'
- => where { $_ =~ /^\d+$/};
- no Moose::Util::TypeConstraints;
- has telephone => (
- is => 'rw',
- isa => 'Telephone' ,
- );
- package User;
- use Moose;
- extends 'Person';
- has login => ( is => 'rw', isa => 'Str', );
- sub do_login {
- my ($self, $password) = @_;
- print '用户:'.$self->login.'; 密码:'.$password;
- }
- before do_login => sub {
- my ( $self, $pass ) = @_;
- warn "Called login() with $pass \n";
- };
- after do_login => sub {
- my $self=shift;
- print '; Telephone:'.$self->telephone,"\n";
- };
- around name => sub {
- my ($orig, $self, @args) = @_;
- return ucfirst lc $self->$orig( @args ) ;
- };
- package main;
- my $user = new User(id => 123, name =>'john');
- $user->login($user->name);
- print $user->name,"\n";
- print $user->id,"\n";
- print $user->telephone(311223234),"\n";
- $user->do_login(123456);
002Moose::Role Person
- package Person;
- use Moose::Role;
- has id => ( is => 'rw' , isa => 'Int' , required => 1 ,default => 0 );
- has name => ( is => 'rw' , isa => 'Str' , );
- use Moose::Util::TypeConstraints;
- subtype 'Telephone'
- => as 'Int'
- => where { $_ =~ /^\d+$/};
- no Moose::Util::TypeConstraints;
- has telephone => (
- is => 'rw',
- isa => 'Telephone' ,
- );
- package User;
- use Moose;
- with 'Person';
- has login => ( is => 'rw', isa => 'Str', );
- sub do_login {
- my ($self, $password) = @_;
- print '用户:'.$self->login.'; 密码:'.$password;
- }
- before do_login => sub {
- my ( $self, $pass ) = @_;
- warn "Called login() with $pass \n";
- };
- after do_login => sub {
- my $self=shift;
- print '; Telephone:'.$self->telephone,"\n";
- };
- around name => sub {
- my ($orig, $self, @args) = @_;
- return ucfirst lc $self->$orig( @args ) ;
- };
- package main;
- my $user = new User(id => 123, name =>'john');
- $user->login($user->name);
- print $user->name,"\n";
- print $user->id,"\n";
- print $user->telephone(311223234),"\n";
- $user->do_login(123456);
003Moo***::Declare Person
- use Moo***::Declare;
- class Person {
- has id => ( is => 'rw', isa => 'Int', required => 1, default => 0 );
- has name => ( is => 'rw', isa => 'Str', );
- use Moose::Util::TypeConstraints;
- subtype 'Telephone' => as 'Int' => where { $_ =~ /^\d+$/ };
- no Moose::Util::TypeConstraints;
- has telephone => (
- is => 'rw',
- isa => 'Telephone',
- );
- }
- class User extends Person {
- has login => ( is => 'rw', isa => 'Str', );
- method do_login( Num $password) {
- print '用户:' . $self->login . '; 密码:' . $password;
- }
- before do_login( Num $password) {
- warn "Called login() with $password\n";
- };
- after do_login {
- print '; Telephone:' . $self->telephone, "\n";
- };
- around name( Str @args ) {
- return ucfirst lc $self->$orig(@args);
- };
- }
- # Main program
- my $user = new User( id => 123, name => 'john' );
- $user->login( $user->name );
- print $user->name, "\n";
- print $user->telephone(311223234), "\n";
- $user->do_login(123456);
004Moo Person
- package Person;
- use Moo;
- use Sub::Quote;
- has id => (
- is => 'rw',
- isa => quote_sub q{ die unless $_[0] == int($_[0]) }
- );
- has name => (
- is => 'rw',
- isa => quote_sub q{ die unless $_[0] =~ /^\D+$/}
- );
- has telephone => (
- is => 'rw',
- isa => quote_sub q{ die unless $_[0] =~ /^\d+$/}
- );
- package User;
- use Moo;
- use Sub::Quote;
- extends 'Person';
- has login => (
- is => 'rw',
- isa => quote_sub q{ die unless $_[0] =~ m/^\D+$/}
- );
- sub do_login {
- my ( $self, $password ) = @_;
- print '用户:' . $self->login . '; 密码:' . $password;
- }
- before do_login => sub {
- my ( $self, $pass ) = @_;
- warn "Called login() with $pass \n";
- };
- after do_login => sub {
- my $self = shift;
- print '; Telephone:' . $self->telephone, "\n";
- };
- around name => sub {
- my ( $orig, $self, @args ) = @_;
- return ucfirst lc $self->$orig(@args);
- };
- package main;
- my $user = new User( id => 123, name => 'john' );
- $user->login( $user->name );
- print $user->name, "\n";
- print $user->id, "\n";
- print $user->telephone(311223234), "\n";
- $user->do_login(123456);
阅读(1330) | 评论(3) | 转发(0) |