简体   繁体   中英

Perl: Strings not escaping backslashes

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 '\\\\' . '@' . $key '\\\\' . '@' . $key or '\\@' . $key '\\@' . $key or '\\\\@' . $key '\\\\@' . $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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM