SPUG: CGI.pm issue

Peter Darley pdarley at kinesis-cem.com
Thu Jan 13 07:30:14 PST 2005


Folks,
	I had a problem with CGI.pm, that I was able to resolve, but now I'm just
trying to understand exactly what the problem was.

	The problem was that when I had a textarea form field that had a lot of
data in it, the end of the data would be cut off.  I tracked this down to
the read_from_client sub, which is only called once, so has to return all
the data from the post in one go.  It wasn't returning all the data however.

	The original sub looks like this:

# Read data from a file handle
sub read_from_client {
    my($self, $fh, $buff, $len, $offset) = @_;
    local $^W=0;                # prevent a warning
    return undef unless defined($fh);
    return read($fh, $$buff, $len, $offset);
}

	I checked $len and it always matched the ammount of data that was expected,
and offset was always 0, but it wouldn't read all the data in one go.  I
couldn't find anything that indicated that read had an upper limit on what
it would grab in one go.  Is there a limit like this?

	The new sub looks like this:

# Read data from a file handle
sub read_from_client {
    my($self, $fh, $buff, $len, $offset) = @_;
    local $^W=0;                # prevent a warning
    return undef unless defined($fh);
#    return read($fh, $$buff, $len, $offset);

    while (read($fh, $Result, $len, $offset))
    {
        $$buff .= $Result;
    }

    return 0;
}

	Which works fine.  Basically I'm just reading till I run out of data and
then returning 0 which is what read should return when it hits an EOF.

	Anyway, I'm not sure what was causing the origional problem, so I'm not
entirely sure that this is a totally safe change.  I have done a lot of
testing and it seems to behave correctly.

	Any revelation about what's going on here is appreciated.

Thanks,
Peter Darley



More information about the spug-list mailing list