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

Discussion in 'Perl Misc' started by nonews.org@gmail.com, Mar 13, 2006.

  1. Guest

    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?
    , Mar 13, 2006
    #1
    1. Advertising

  2. wrote in news:1142261719.083934.136100
    @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
    --
    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Mar 13, 2006
    #2
    1. Advertising

  3. >>>>> "nonews" == nonews org <> writes:

    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
    <> <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 ***
    Randal L. Schwartz, Mar 13, 2006
    #3
  4. Dr.Ruud Guest

    schreef:

    > Content => [ q => "$TEXT" ]


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

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    echo 014C8A26C5DB87DBE85A93DBF |perl -pe 'tr/0-9A-F/JunkshoP cartel,/'
    Dr.Ruud, Mar 13, 2006
    #4
  5. <> wrote:


    > $size = (stat(TXT))[7];



    my $size = -s TXT; # does the same thing, but much nicer to read


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 13, 2006
    #5
  6. Dr.Ruud <> wrote:
    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 13, 2006
    #6
  7. Dr.Ruud Guest

    Tad McClellan schreef:
    > Dr.Ruud:
    >> [attribution repaired] :


    >>> 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)

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    echo 014C8A26C5DB87DBE85A93DBF |perl -pe 'tr/0-9A-F/JunkshoP cartel,/'
    Dr.Ruud, Mar 13, 2006
    #7
  8. Dr.Ruud <> wrote:
    > Tad McClellan schreef:
    >> Dr.Ruud:
    >>> [attribution repaired] :

    >
    >>>> 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"?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 13, 2006
    #8
  9. Dr.Ruud Guest

    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.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    echo 014C8A26C5DB87DBE85A93DBF |perl -pe 'tr/0-9A-F/JunkshoP cartel,/'
    Dr.Ruud, Mar 14, 2006
    #9
  10. wrote:
    > 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";
    ...
    }

    --
    Charles DeRykus
    Charles DeRykus, Mar 14, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    4,780
    Tor Iver Wilhelmsen
    Aug 29, 2005
  2. James
    Replies:
    3
    Views:
    16,249
    Roedy Green
    Nov 25, 2005
  3. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    460
    santosh
    Jul 24, 2007
  4. Matt
    Replies:
    5
    Views:
    387
    Aaron Bertrand - MVP
    May 4, 2004
  5. Replies:
    1
    Views:
    214
    zentara
    Nov 6, 2006
Loading...

Share This Page