I have code that does a sequential post to a number of web sites, which is tried & tested. However I am now writing a variation in the script to make multiple parallel PUTs to these web sites. My reference documents are the pod for AnyEvent::HTTP & AnyEvent::Intro and I'm running Perl 5.10 on a Solaris box
#!/path/to/bin/perl -w use strict; use AnyEvent::HTTP; use Data::Dumper; # CONF my @targets = qw(place1 place2 place3 place4 place5); my ($host, $username, $password, $collection) = (); my $endpoints = {}; ## deleted fragment that sets up end-point details ## ... basic format: ##$endpoints->{'place'}->{'host'} = 'my-host:port'; ##$endpoints->{'place'}->{'path'} = '/some/path'; ##$endpoints->{'place'}->{'username'} = 'u243'; ##$endpoints->{'place'}->{'password'} = 'divedivedive'; # content to send via POST: my $file = "broker_deposit.zip"; # An AnyEvent method to send a zip file to a sword endpoint # sub transfer($) { my ($params) = @_; my $cb = AnyEvent->condvar; my ($file, $host, $collection, $username, $password, $protocol); $file = $params->{file} if exists $params->{file}; $host = $params->{host} if exists $params->{host}; $protocol = $params->{protocol} if exists $params->{protocol}; $collection = $params->{collection} if exists $params->{collection +}; $username = $params->{username} if exists $params->{username}; $password = $params->{password} if exists $params->{password}; print "open file...."; # Load the file. If there is no file, then return from the event w +ith nothing my $archive = ""; open(FILE, $file) or return $cb->send("Unable to create Transfer package"); binmode FILE; while (my $l = <FILE>) { $archive .= $l; } close FILE; # Tell SWORD to process the contents of the zip file as the new OA +-RJ type my %headers = ( 'X-Packaging' => 'http://opendepot.org/broker/1.0', 'X-No-Op' => 'false', 'X-Verbose' => 'false', 'Content-Disposition' => "filename=$file", 'Content-Type' => 'application/zip', 'User::Agent' => 'OA-RJ Broker v0.2', ); print " make http request (http://${username}:${password}\@${host}${co +llection}:\n"; # this is a long call, interspersed with comments! http_post( # the main request we want to make "http://${username}:${password}\@${host}${collection}", # the body of the http_request $archive, # the headers for that request headers => \%headers, # What we do whilst we wait sub { my ($body, $header) = @_; my $status = $header->{Status}; if ($status =~ /20[01234]/ ) { # download ok || resume ok || file already fully downloade +d $cb->send($body); } else { $cb->send( Dumper($header) ); } } ); return $cb; } ## end sub transfer($$) my @responses; my $cv = AnyEvent->condvar; foreach my $target (@targets) { $cv->begin; print "$target: "; my $host = $endpoints->{$target}->{'host'} if exists $endpoints->{$target}->{'host'}; my $collection = $endpoints->{$target}->{'collection'} if exists $endpoints->{$target}->{'collection'}; my $username = $endpoints->{$target}->{'username'} if exists $endpoints->{$target}->{'username'}; $password = $endpoints->{$target}->{'password'} if exists $endpoints->{$target}->{'password'}; my %params = ( protocol => 'http', host => $host, collection => $collection, username => $username, password => $password, file => $zipfile, ); push @responses, transfer( \%params ); $cv->end; } ## end foreach my $target (@targets...) $cv->recv; print Dumper(\@responses); exit;
This works, but I get the following dump at the end:
$VAR1 = [ bless( {}, 'AnyEvent::CondVar' ), bless( {}, 'AnyEvent::CondVar' ), bless( {}, 'AnyEvent::CondVar' ), bless( {}, 'AnyEvent::CondVar' ), bless( {}, 'AnyEvent::CondVar' ) ];
Fairly obviously, the problem is the line push @responses, transfer( \%params ); - but how DO I get the response from each PUT into my list of responses?


-- Ian Stuart
A man depriving some poor village, somewhere, of a first-class idiot.

In reply to AnyEvent::HTTP and getting data back from a post request by kiz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.