我遇到的问题是正则表达式的行为对我来说没有意义.$ line是对标量的引用(在本例中,字符串是'print'hello world \n"')但是,执行正则表达式匹配的尝试似乎成功,但也更改了$$ line的值.除此之外,我在尝试修改第65行的$$行时遇到错误
这是代码:
my $line = $_[0];
$$line =~ s/^(\s+\(?)//;
my @functions = ('print');
# Check if the expression is a function
for my $funcName (@functions) {
print $$line . "\n";
if ($$line =~ m/^($funcName\(?\s*)/) {
print $$line . "\n";
$$line =~ s/$1//; # THIS IS LINE 65
my $args = [];
while (scalar(@{$args}) == 0 || ${$line} =~ /\s*,/) {
push (@{$args}, parseExpression($line))
}
my $function = {
type => 'function',
name => $funcName,
args => $args
};
return $function;
}
}
Run Code Online (Sandbox Code Playgroud)
输出如下:
print "hello world\n"
print
Modification of a read-only value attempted at ./perl2python.pl line 65, <> line 3.
Run Code Online (Sandbox Code Playgroud)
此代码是函数的摘录,但它应足以说明出现了什么问题.
输出的第二行应该与第一行相同,但是看起来$$行是由if子句在两个print语句之间改变的.
任何建议?
如果消除了属于不属于问题的代码的所有混淆,消除子程序调用及其参数传递等等,您可以将问题归结为代码看起来像这样:
my $line = \"Hello world!\n"; # $line contains a reference to a string literal.
$$line =~ s/Hello/Goodbye/;
Run Code Online (Sandbox Code Playgroud)
当你运行它时,你将得到Modification of a read-only value attempted...消息,因为你无法修改字符串文字.
添加my某个地方修复它的事实可能只是意味着你的新词法$line掩盖了一些名为的其他标量变量$line,它是一个持有对字符串文字的引用的变量.
| 归档时间: |
|
| 查看次数: |
683 次 |
| 最近记录: |