package LWP::Simple::Post; use strict; use warnings; use HTTP::Request::Common; sub import { my $pkg = shift; my $callpkg = caller; require LWP::Simple; push @LWP::Simple::EXPORT, qw(post postprint poststore); } sub LWP::Simple::post ($;%) { my $url = shift; my $ret; LWP::Simple::_init_ua() unless $LWP::Simple::ua; my $request = POST $url, Content_Type => 'form-data', Content => [ @_ ]; my $response = $LWP::Simple::ua->request($request); return $response->is_success ? $response->content : undef; } sub LWP::Simple::postprint ($;%) { my $url = shift; LWP::Simple::_init_ua() unless $LWP::Simple::ua; my $request = POST $url, Content_Type => 'form-data', Content => [ @_ ]; local $\ = ""; # ensure standard $OUTPUT_RECORD_SEPARATOR my $callback = ($^O ne 'MacOS' ? sub { print $_[0]; } : sub { $_[0] =~ s/\015?\012/\n/g; print $_[0]; }); my $response = $LWP::Simple::ua->request($request, $callback); unless ($response->is_success) { print STDERR $response->status_line, " \n"; } return $response->code; } sub LWP::Simple::poststore ($$;%) { my $url = shift; my $file = shift; LWP::Simple::_init_ua() unless $LWP::Simple::ua; my $request = POST $url, Content_Type => 'form-data', Content => [ @_ ]; my $response = $LWP::Simple::ua->request( $request, $file ); return $response->code; } 1; __END__ =head1 NAME LWP::Simple::Post - adds the post method to LWP::Simple =head1 SYNOPSIS use LWP::Simple::Post; use LWP::Simple; poststore 'http://www.perlmonks.org', 'myfile.xml', node => 'diotalevi', displaytype => 'xml'; post 'http://www.perlmonks.org', node => 'diotalevi'; postprint 'http://www.perlmonks.org', node => 'diotalevi', displaytype => 'xml'; =head1 DESCRIPTION This module tacks the post(), postprint(), and poststore() functions onto LWP::Simple. =over 4 =item post($url, Header => Value,...) The post() function will fetch the document identified by the given URL and return it. It returns C if it fails. The $url argument can be either a simple string or a reference to a URI object. The remaining arguments are a list of parameters to pass as form data. You will not be able to examine the response code or response headers (like 'Content-Type') when you are accessing the web using this function. If you need that information you should use the full OO interface (see L). $url = 'http://www.perlmonks.org'; %parameters = ( node => 'Offering Plate ); post $url, %parameters; =item postprint($url, Header => Value,...) Post and print a document identified by a URL. The document is printed to STDOUT as data is received from the network. If the request fails, then the status code and message are printed on STDERR. The return value is the HTTP response code. =item poststore($url, $file, Header => Value,...) Store the document identified by a URL and stores it in the file. The return value is the HTTP response code. =back =head1 NOTES Be sure to use() LWP::Simple::Post before loading LWP::Simple. This module must extend LWP::Simple exports its own methods. =head1 SEE ALSO L, L =head1 AUTHOR Joshua b. Jore Ejjore@cpan.orgE =cut