$ dir =〜s / __ math_eqn __ / $ fun-> {'VAR'} {'math_eqn'} / g;在Perl中做

时间:2019-07-03 09:12:10

标签: perl

此命令的作用是什么? ,我知道这是要更换的,但是由于我不了解操作员,因此感到困惑。

$dir =~ s/__math_eqn__/$fun->{'VAR'}{'math_eqn'}/g;

1 个答案:

答案 0 :(得分:1)

它正在对字符串$dir进行global substitution。 子字符串__math_eqn__替换为哈希值$fun->{VAR}{math_eqn}(其中$fun是哈希引用),例如:

my $fun = {
    VAR => { math_eqn => "2 + 2 = 4" }
};
my $dir = "This is the example equation: __math_eqn__,  and "
  . " here is the equation once more: __math_eqn__";
$dir =~ s/__math_eqn__/$fun->{'VAR'}{'math_eqn'}/g;
say $dir;

输出

This is the example equation: 2 + 2 = 4,  and  here is the equation once more: 2 + 2 = 4

有关更多信息,请参见perldoc perlrefperldoc perldsc