Skip to content

Instantly share code, notes, and snippets.

@terrencehan
Last active December 11, 2015 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save terrencehan/4621165 to your computer and use it in GitHub Desktop.
Save terrencehan/4621165 to your computer and use it in GitHub Desktop.
#这个问题解决了:
#Most of the power comes from the implicit smartmatching
#that can sometimes apply. Most of the time, when(EXPR) is
#treated as an implicit smartmatch of $_ , that is, $_ ~~ EXPR .
#(See Smartmatch Operator in perlop for more information on smartmatching.)
#But when EXPR is one of the 10 exceptional cases
#(or things like them) listed below, it is used directly as a boolean.
#1. A user-defined subroutine call or a method invocation.
#其他省略
use MooseX::Declare;
class Class {
has 'num' => (
is => 'rw',
isa => 'Num',
default => 123,
);
}
my $e = Class->new;
sub return_123 { return 123; }
use 5.010;
given (124) {
when ( $e->num ) { say "123"; } #实际执行这里
when (124) { say "124"; } #应该执行这个
}
given (124) {
when ( return_123() ) { say "123"; } #实际执行这里
when (124) { say "124"; } #应该执行这个
}
given (124) {
when ( return_123() . "" ) { say "123"; }
when (124) { say "124"; } #正确执行到这里
}
given (124) {
when ( my $x = $e->num ) { say "123"; }
when (124) { say "124"; } #这次就执行了这个
}
#这个问题定位在smart matching上么;
if ( 124 ~~ return_123() ) {
print "get in if"; #X不能执行到这里
}
given(123){
when(1 == 1) {say "1 == 1";}
when(123) {say "123";}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment