substitute:替换评价
Substitute: replacement evaluation
如果在变量中传递替换,那么第一个和第二个替换是等价的吗?
#!/usr/bin/env perl6
use v6;
my $foo = 'switch';
my $t1 = my $t2 = my $t3 = my $t4 = 'this has a $foo in it';
my $replace = prompt( ':' ); # [=10=]
$t1.=subst( / ( $ \w+ ) /, $replace );
$t2.=subst( / ( $ \w+ ) /, { $replace } );
$t3.=subst( / ( $ \w+ ) /, { $replace.EVAL } );
$t4.=subst( / ( $ \w+ ) /, { ( $replace.EVAL ).EVAL } );
say "T1 : $t1";
say "T2 : $t2";
say "T3 : $t3";
say "T4 : $t4";
# T1 : this has a [=10=] in it
# T2 : this has a [=10=] in it
# T3 : this has a $foo in it
# T4 : this has a switch in it
$replace
和 {$replace}
之间的唯一区别是第二个是 returns 变量值的块。它只是增加了一个间接级别,但结果是一样的。
更新:根据@raiph的评论编辑。
如果在变量中传递替换,那么第一个和第二个替换是等价的吗?
#!/usr/bin/env perl6
use v6;
my $foo = 'switch';
my $t1 = my $t2 = my $t3 = my $t4 = 'this has a $foo in it';
my $replace = prompt( ':' ); # [=10=]
$t1.=subst( / ( $ \w+ ) /, $replace );
$t2.=subst( / ( $ \w+ ) /, { $replace } );
$t3.=subst( / ( $ \w+ ) /, { $replace.EVAL } );
$t4.=subst( / ( $ \w+ ) /, { ( $replace.EVAL ).EVAL } );
say "T1 : $t1";
say "T2 : $t2";
say "T3 : $t3";
say "T4 : $t4";
# T1 : this has a [=10=] in it
# T2 : this has a [=10=] in it
# T3 : this has a $foo in it
# T4 : this has a switch in it
$replace
和 {$replace}
之间的唯一区别是第二个是 returns 变量值的块。它只是增加了一个间接级别,但结果是一样的。
更新:根据@raiph的评论编辑。