替代:替代评估

时间:2019-04-20 06:46:14

标签: replace eval perl6 subst

如果替换是通过变量传递的,那么第一个和第二个替换是否相等?

#!/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( ':' ); # $0

$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 $0 in it
# T2 : this has a $0 in it
# T3 : this has a $foo in it
# T4 : this has a switch in it

1 个答案:

答案 0 :(得分:9)

$replace{$replace}之间的唯一区别在于,第二个是返回变量值的块。它只是添加了一个间接级别,但是结果是相同的。 更新:根据@raiph的评论进行编辑。

相关问题