[英]Strange behavior of '\' under mod_perl
I have the following in a script that runs under mod_perl 我在mod_perl下运行的脚本中具有以下内容
Logger::log("\$1 = '$1'");
Logger::log("\$ 1 = '$1'");
Logger::log("\\$1 = '$1'");
Which outputs the following into my log file: 它将以下内容输出到我的日志文件中:
logger: = ''
logger: $ 1 = ''
logger: \ = ''
$1 is known to be null. 已知$ 1为空。 Is this a bug in mod_perl2 or is it something else I'm missing? 这是mod_perl2中的错误,还是我想念的其他东西?
Did you try: 你试过了吗:
Logger::log(q!$1 = '! . $1 . q!'!);
or, to avoid warnings: 或者,为避免警告:
Logger::log(q!$1 = '! . ( defined $1 ? $1 : '' ) . q!'!);
The idea here is that q!...! 这里的想法是q!...! doesn't interpolate its contents, so you know for sure the first part of the string will be $1 = ". If it's still not appearing in the output, then you know Logger::log() or something it calls is interpolating its arguments, which probably shouldn't happen. 不会插值其内容,因此您可以确定字符串的第一部分将是$ 1 =“。如果仍未出现在输出中,则说明Logger :: log()或它调用的内容正在插值其参数,这可能不应该发生。
Oh, and if you're using a more modern Perl, the second example can use ( $1 // '' ) instead. 哦,如果您使用的是更现代的Perl,则第二个示例可以使用($ 1 //'')。
If you're worried about catching and accidently printing nulls, there's a quick and easy way that almost everyone will recommend you do first: add the following to your program: 如果您担心捕获和意外打印空值,几乎每个人都会推荐一种快速简便的方法:在程序中添加以下内容:
use strict;
use warnings;
The problem in particular seems odd; 特别是这个问题看起来很奇怪。 when I do 当我做
my $foo = 'zip';
$foo =~ /(bal)/;
print "\$1: '$1'";
I get 我懂了
$1: ''
(and with use strict and warnings, the additional error (以及使用严格和警告的附加错误
Use of uninitialized value in concatenation (.) or string at - line 5.
Of course, you can prevent $1 from ever being null if you test your regex: 当然,如果测试正则表达式,可以防止$ 1变为null:
if ($foo =~ /(pattern)/) {
# $1 is guaranteed to be ok here, if it matched
}
So yeah, it might be your logger re-interpreting $1 as something else. 是的,可能是您的记录器将$ 1重新解释为其他内容。 Try adding two more \\\\
; 尝试添加两个\\\\
; one for escaping the $, and another for escaping an extra backslash. 一个用于转义$,另一个用于转义额外的反斜杠。 Thus it'd look like 因此,它看起来像
print "\\\$1: '$1'";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.