in reply to Re: mod_perl: variable $end will not stay shared (nested subroutines problem)
in thread mod_perl: variable $end will not stay shared (nested subroutines problem)

I'm not even sure you need to be doing what you're trying to do here (read is built to handle the issue of when you try to read a larger chunk than what's left in the file).

I said what I was doing: I don't want to always read to end of file. Only to the byte position specified...

  • Comment on Re: Re: mod_perl: variable $end will not stay shared (nested subroutines problem)

Replies are listed 'Best First'.
Re: Re: Re: mod_perl: variable $end will not stay shared (nested subroutines problem)
by arturo (Vicar) on Apr 18, 2001 at 00:35 UTC

    Hm, well, then, here's an alternate approach that does away with a subroutine. I assume the goal is to avoid loading a large file willy nilly into a Perl scalar (because read is already buffered):

    • Work out the total # of bytes to read.
    • Work out how many chunks of $bufsiz that would need to be read.
    • Work out the remaining # of bytes
    • print out a sufficient # of big chunks
    • print out the last chunk

    Some code (totally off the top of my head, untested, just to give the idea):

    # get $start, $end my $bufsiz = 4096; my $total_length = $end-$start; my $chunks = int $total_length / $bufsiz; my $data; if ($chunks) { foreach (1 ... $chunks) { read (FILE, $data, $bufsiz); print $data; } } read (FILE, $data, $total_length % $bufsiz); print $data;

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor