HTTP::Request::Common and long Text via POST

N

nonews.org

Hi. I need put a long text via POST protocol. My souce code:

#!/usr/bin/perl

use HTTP::Request::Common;
use LWP::UserAgent;

$textfile = "testo.txt";
open(TXT, "<$textfile") || die ("Impossibile aprire $textfile");
$size = (stat(TXT))[7];
read(TXT,$TEXT,$size);

$ua = LWP::UserAgent->new;
$ua->request(
POST 'http://127.0.0.1/gandu/tmp/get.php',
Content_Type => 'form-data',
Content_Size => $size,
# Content => [ q => "qqq" ]
Content => [ q => "$TEXT" ]
);

close TXT;

Now, if I decomment "Content => [ q => "qqq" ]" it works, but if I put
$TEXT (or long string) script failed. Can you help me?
 
A

A. Sinan Unur

(e-mail address removed) wrote in @j52g2000cwj.googlegroups.com:
Hi. I need put a long text via POST protocol. My souce code:

#!/usr/bin/perl

use strict;
use warnings;

missing.
use HTTP::Request::Common;
use LWP::UserAgent;

$textfile = "testo.txt";

my $textfile = $ARGV[0] || 'testo.txt';
open(TXT, "<$textfile") || die ("Impossibile aprire $textfile");

open my $text_h, '<', $textfile
or die "Impossibile aprire $textfile: $!";
$size = (stat(TXT))[7];
read(TXT,$TEXT,$size);

Why?

use File::Slurp;
my $TEXT = read_file( $textfile );
$ua = LWP::UserAgent->new;
$ua->request(
POST 'http://127.0.0.1/gandu/tmp/get.php',
Content_Type => 'form-data',
Content_Size => $size,
# Content => [ q => "qqq" ]
Content => [ q => "$TEXT" ]

Content => [ q => $TEXT ],

perldoc -q always
);

close TXT;

Now, if I decomment "Content => [ q => "qqq" ]" it works, but if I put
$TEXT (or long string) script failed. Can you help me?

How does it fail?

Check for errors and tell us what those errors are.

Sinan
--
 
R

Randal L. Schwartz

nonews> Hi. I need put a long text via POST protocol. My souce code:
[points for including source code]

nonews> Now, if I decomment "Content => [ q => "qqq" ]" it works, but if I put
nonews> $TEXT (or long string) script failed. Can you help me?
[no points for not describing what "failed" means]

How about including:

1) what you did
2) what you expect
3) what you got
4) why you think that isn't what should happen

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[email protected]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
 
T

Tad McClellan

Dr.Ruud said:
(e-mail address removed) schreef:
Content => [ q => "$TEXT" ]

Did you try:
Content => [ q => $TEXT ]
?


Why do you think that your suggested change will make a difference?

I cannot think of a situation where it would, unless $TEXT is a
reference, which it is not in this case.
 
D

Dr.Ruud

Tad McClellan schreef:
Dr.Ruud:
[attribution repaired] (e-mail address removed):
Content => [ q => "$TEXT" ]

Did you try:
Content => [ q => $TEXT ]
?

Why do you think that your suggested change will make a difference?

Why don't you? OP said "long text".

I cannot think of a situation where it would, unless $TEXT is a
reference, which it is not in this case.

#!/usr/bin/perl
use strict; use warnings; $|++;

my $TEXT = (('T'x1024)x1024)x256;
my $q = "$TEXT";
#my $q = $TEXT;
print length($q), "\n";

That 256 is the border-value on a Perl 5.8.6 under FreeBSD here.

I also tried 5.8.7 under a Windows with 320 MB RAM, but hadn't the
patience to wait for the test to finish.
(yes, Ctrl-C worked)
 
T

Tad McClellan

Dr.Ruud said:
Tad McClellan schreef:
Dr.Ruud:
[attribution repaired] (e-mail address removed):
Content => [ q => "$TEXT" ]

Did you try:
Content => [ q => $TEXT ]
?

Why do you think that your suggested change will make a difference?

Why don't you? OP said "long text".


Oh. Very good. I should have thought of that.

For those playing along at home:

The 1st one makes a copy that the 2nd one doesn't.

(requiring a big chunk of memory.)



The above should be included in the FAQ answer as another example of

What's wrong with always quoting "$vars"?
 
D

Dr.Ruud

Tad McClellan:
["$var" vs. $var, where $var is huge]
For those playing along at home:
The 1st one makes a copy that the 2nd one doesn't.
(requiring a big chunk of memory.)

The above should be included in the FAQ answer as another example of
What's wrong with always quoting "$vars"?

An optimization is possible where <"$var"> is merely copied: why settle
for a copy of a temporary copy? This must've come up before, so I hope
it wasn't a "not my brother's keeper" reason for not implementing it.

Similar for <$var = "${var1}ABC${var2}"> cases: the
<"${var1}ABC${var2}"> is a recipe and not a buffer, so Deparse can
transform it to <$var = $var1 . 'ABC' . $var2> and the compiled code can
process that as a stream.

A 'possibly huge' attribute (set with slurp), and a warning when a
variable with that attribute is used to build some other value, seems a
good idea to me.

But OP should of course have used a reference, his $TEXT remains as is
and already contains all he wants.
 
C

Charles DeRykus

Hi. I need put a long text via POST protocol. My souce code:

#!/usr/bin/perl

use HTTP::Request::Common;
use LWP::UserAgent;

$textfile = "testo.txt";
open(TXT, "<$textfile") || die ("Impossibile aprire $textfile");
$size = (stat(TXT))[7];
read(TXT,$TEXT,$size);
^^^^^^^^^^^^^^^^^^^^^^

You may want to check the read return for surprises too.
From 'perldoc -f read' :

read FILEHANDLE,SCALAR,LENGTH
Attempts to read LENGTH bytes of data into variable
SCALAR from the specified FILEHANDLE. Returns the
number of bytes actually read, "0" at end of file,
or undef if there was an error.

eg.,

my $count = read( TXT, $TEXT, $size );
unless ( defined $count ) {
die "$txtfile read error: $!";
} elsif ( $count < $size ) {
warn "partial read or eof: $count vs. $size";
...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top