tags:

views:

145

answers:

5

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.

+1  A: 

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');
Corey
+10  A: 

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);
John Siracusa
+6  A: 

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.

Sinan Ünür
+1 for auto quoting LHS barewords.
Sean McMillan
+5  A: 

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?

ysth
Oh god I hope this doesn't catch on... But thanks for filling me in.
NXT
I also hope so.
ysth
I'm a fan of using => even when it doesn't quote anything since it can often clarify the relationship between arguments: `read $file => $data, 100;`
Eric Strom
+2  A: 

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.

Sean McMillan