开发者

Perl: Strings not escaping backslashes

开发者 https://www.devze.com 2023-02-04 22:17 出处:网络
I cannot开发者_开发问答 understand what I\'m doing wrong with such a simple statement. The problem occurs on the line:

I cannot开发者_开发问答 understand what I'm doing wrong with such a simple statement. The problem occurs on the line:

$this->_validate_hash($hashref->{$key}, $schemaref->{$key}, $path . (ref($hashref->{$key}) ? $key . "/" : "\\\@$key"), $errors);

So I want things to end with /path/to/some/\@attribute. However, what I'm getting at the moment is path/to/some/\\@attribute. I tried various combos for the last part including '\\' . '@' . $key or '\@' . $key or '\\@' . $key but I still can't escape the backslash. Is this some quirk in perl strings that I'm not aware of? Thanks.


That is not a simple statement; it is split over two lines and contains all sorts of stuff. This achieves the same result, as near as I can see (and assuming $xpath is not used elsewhere in your script):

my $xpath = $path . (ref($hashref->{$key}) ? $key . "/" : "\\\@$key");
$this->_validate_hash($hashref->{$key}, $schemaref->{$key}, $xpath, $errors);

However, it is more nearly readable - still not particularly easy, but much more readable than the original.

Your problem child can be reduced, it seems, to:

use strict;
use warnings;

my $key  = "attribute";
my $path = "/path/to/some/";
my $localpath = $path . "\\\@$key";

print "Key:   $key\n";
print "Path:  $path\n";
print "Local: $localpath\n";

That code works cleanly:

Key:   attribute
Path:  /path/to/some/
Local: /path/to/some/\@attribute

So, either I've got a different Perl (this was 5.12.1 on Linux x86/64) or there is something different in your setup. Given that we don't have all the hashes etc, it will be hard to get at it - but you should break down your problem in an analogous way.

0

精彩评论

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

关注公众号