替换哈希数组中的反斜杠

时间:2017-04-06 16:19:11

标签: regex perl

我有下一段代码

sub raw_query {

    my ($self,%args) = @_;

    state $global_id = int(rand(10000));


    $args{'jsonrpc'} = '2.0';
    if ($self->cookie) { $args{'auth'} = $self->cookie; }
    $args{'id'} = $global_id++;


    if ($self->{verbosity} == 1) {
       print Dumper(\%args);
    }
    %args=quotemeta %args;
    for (my $i=0; $i< $self->{retries}; $i++){
      my $response = eval { $self->{ua}->post($self->{server},
                                              'Content-Type' => 'application/json-rpc',
                                              'Content' => JSON->new->escape_slash->utf8->encode(\%args)) };

它为某些值返回斜杠,因此JSON编码失败:

 \{
                                              'main' => '1',
                                              'hostid' => '1',
                                              'useip' => '1',
                                              'type' => '2',
                                              'bulk' => '1',
                                              'interfaceid' => '1',
                                              'ip' => '1.2.8.4',
                                              'port' => '1',
                                              'dns' => 'test'
                                            }

请你帮忙替换这个backslah:从\ {到{?

2 个答案:

答案 0 :(得分:1)

I suspect you have passed your arguments to the method as a hash reference

$self->raw_query( { a => 'b', c => 'd' } );

or perhaps

my %args = ( a => 'b', c => 'd' );

$self->raw_query( \%args );

This should be

$self->raw_query( a => 'b', c => 'd' );

or

$self->raw_query( %args );

答案 1 :(得分:0)

我的问题不在于原始源代码,而是在此代码中使用了Perl模块的源代码。一旦我纠正它 - 一切都成功了。谢谢大家的帮助!