当前位置: 动力学知识库 > 问答 > 编程问答 >

perl - What happens when you put an array on the right side of a => operator?

问题描述:

This might seem like a stupid question but it's been a long day. I'm an adapting some Perl code for another use and I ran across this syntax:

my @request;

#... fill the array with stuff...

my $reply = $service->call('requestMessage' => @request, $header);

That method call seems implausible, if => is just a special kind of comma and @request gets interpolated into the list.

Is it actually equivalent to:

my $reply = $service->call('requestMessage' => \@request, $header);

What's going on here?

EDIT: Thanks for the answers. I am well aware of the difference between pass by value and pass by reference. I was asking if an apparent pass by value was being converted into a pass by reference. Apparently not. Thank you all for answering.

网友答案:

The code in question:

my $reply = $service->call('requestMessage' => @request, $header);

is not equivalent to this:

my $reply = $service->call('requestMessage' => \@request, $header);

It is, as you surmised, equivalent to this:

my $reply = $service->call('requestMessage', @request, $header);

Sometimes, the "fat comma," as => is known, is used to indicate a relationship between positional parameters. For example, the link between the method name some_method and its intended @arguments in this code:

$server->distribute_across_nodes(some_method => @arguments);
网友答案:

What happens when you put an array on the right side of a => operator?

=> is the fat comma. It has no effect on the RHS. It automatically quotes the LHS if the LHS is a bareword.

The rest of your question seems to boil down to if there is a difference between passing a function an array versus a reference to an array: Yes, there is. In the first case, the array is flattened and passed to the function. So, if the array has 1,000 elements, the functions argument list will grow by 1,000 elements.

In the second case, a reference to the array is passed, so the size of the argument list does not depend on the size of the array pointed to by the reference passed to the function.

网友答案:

There's a school of thought that => should be used as some kind of emphatic comma, even when it makes no difference. Advocates use it to indicate that one thing describes or acts on another, e.g.:

split /,/ => $var;

Maybe the author of your code falls into this camp?

网友答案:

Looking at the SOAP::Lite code, it eventually makes this call

$serializer->envelope(method => shift, @_)

So your original call

$service->call('requestMessage' => @request, $header);

is just using the fat comma to emphasize that the contents of (@request, $header ) will become the parameters for the soap call requestMessage. This is less than clear, because $header is part of the parameters, but the convention doesn't make it clear. I'd say that, in this case, the fat comma is not adding anything.

I have seen the "emphatic comma" used in CGI scripts, like so (untested)

$q->param(foo => $bar)

I think it does a pretty good job there of indicating that this call will set the parameter foo. But clearly this can also be confusing. Use with caution.

网友答案:

It will pass "requestMessage", then all values in @request (as the 2nd, 3rd... etc. parameters), and then $header. These will probably get forced into a hash.

Here's an example:

perl -MData::Dumper -e '@a = (1, 2, 3, 4); 
                        %b = ('a' => @a, 'c'); 
                        print Dumper(\%b);'
$VAR1 = {
          '4' => 'c',
          'a' => 1,
          '2' => 3
        };

So in this case it's the same as

%b = ('a' => 1, 2 => 3, 4 => 'c');
分享给朋友:
您可能感兴趣的文章:
随机阅读: