开发者

printf "Modification of a read-only value attempted" error

when attempting to print object as in:

print "$response{_content} \n";
printf OUTPUT "$response{_content} \n"; 

The printf statement generates error "Modification of a read-only value attempted"

It's an intermittent error. Only happens once in 开发者_运维技巧a while, but this program needs to be 100% reliable. dang.

It prints fine to STDOUT.

What am I doing wrong? arrgh!


The first argument of printf is interpreted as output format, not output itself. See perldoc -f printf and man 3 printf for details.

The problem is, printf might occasionally try to write to its args (this has even been the source of several vulnerabilities in C programs), for instance:

perl -we 'printf "abc%n\n", $_; print "$_\n";'

As you can see, this sets $_ to 3, which is the number of characters written before %n occurred. Try %n without further args and you'll see the exact error message from OP.

Long story short: use print unless you really need advanced formatting. Keep first argument to printf r/o unless you really need even more advanced formatting.


You will need to inspect stdout for the failures. My guess is that once in a while, $response{_content} contains sequences that have special meaning to printf.


I just had the same error message, also with printf, but I was doing this:

printf "%-10s $value\n", $label;

The value sometimes contained hex-encoded data from weblogs. Besides doing the padding with the "x" operator, I found that getting the value out of the format string also worked:

printf "%-10s %s\n", $label, $value;

I thought problem in the format string might be due to percents (%) being interpreted as a formatted value, but fiddling with the ampersands (&) made the error go away. Regardless, inserting a value where it will be interpreted now seems like something to avoid.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜