2

I need to join 2 perl arrays/lists (sorry, do not know how they are named correctly) of format

[ a=>1, b=>2, c=>3 ] and [ d=>4, e=>5, f=6 ]

and need to join them to

[ a=>1, b=>2, c=>3, d=>4, e=>5, f=6 ]

How can I do this? I never used lists like this before...

I need it for HTTP::Request::Common

$ua->request(POST 'http://somewhere/foo', [foo => bar, bar => foo]);

because I have a set of standard parameters for every request and additionally custom parameters depending on the request. Of course I do not want to code the same stuff in every request but define the common parameters just once.

Tnx for your help, Robert

Clarification:

This is aprox. what I want to do:

my $result = httpPOST( $url, [ a=>1, b=2, ... ] );

sub httpPOST {
    my( $url, $params ) = @_; # ???
    my $ua = LWP::UserAgent->new;

    my $result  = $ua->request( POST $url, [ %auth, ????? ] );

    return $result->content;
}

Hmmm... I do not get the apropriate syntax. May you assist me one more time please!? ;)

2

2 Answers 2

6
[ ... ]

is similar to

do { my @anon = ( ... ); \@anon }

You have a hash (%auth) and a reference to an array ($params).


%hash gets the elements of a hash as a list of keys and values.

@array gets the elements of an array, which means that @$ref gets the elements of a referenced array.

All you need is

 [ %auth, @$params ]

Note that you should never use ->content. Use ->decoded_content instead. You should thus use the following code:

my $response  = $ua->request( POST $url, [ %auth, @$params ] );
return $response->decoded_content( charset => 'none' );

(charset => 'none' works around a bug that breaks XML responses.)

0
1

In Perl, => is called a "fat comma" and is basically the same as the normal ,, except that you "don't need to quote words to the left of it". So basically

[ a=>1, b=>2, c=>3 ] and [ d=>4, e=>5, f=>6 ]

is identical to

[ 'a', 1, 'b', 2, 'c', 3 ] and [ 'd', 4, 'e' 5, 'f', 6 ]

if you were wondering what the strange syntax did. In Perl, a common meme is to use things like function(key => 'value') in place of function('key', 'value') if it "looks more natural" to the writer. Perl itself does not mind either way.

You could merge these normally ([ @$list1, @$list2 ]), but in this case, it might not be what you want. You see, the reason HTTP::Request::Common uses arrays instead of regular hashes / hashrefs is that it needs to be able to define the same "key" with different "values". To quote from the module's perldoc:

Multivalued form fields can be specified by either repeating the field name or by passing the value as an array reference.

So if you expect your "additional custom parameters" to override any "standard parameters" of the same name, this will not happen. To do this, you should treat them as actual hashes, and then force them to array refs:

my %standards = ( foo => 'bar', bar => 'foo');
my %additionals = ( foo => 'not-bar', this => 'that');
$ua->request(POST 'http://somewhere/foo', [ %standards, %additionals ]);
1
  • Tnx. See my additional details below.
    – Enkidu
    Mar 29, 2013 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.