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

Here's an application of a problem-solving strategy I've found to be occasionally helpful

  1. Consult perldoc perldiag if you don't understand the error message
  2. Work out why you're getting the error message (if it isn't obvious) from the information thus gleaned.
  3. Follow up on any suggestions you can find in the docs.

Interesting how this question came up right after one4k4's concerning a similar issue; long story short, your whole script is a subroutine when you run it under Apache::Registry. 'Course, you're getting it because you have a nested sub the way you've coded it anyhow. But back to the strategy outlined above =>

From perldoc perldiag the solution to the "won't stay shared" issue is:

This problem can usually be solved by making the inner
              subroutine anonymous, using the sub {} syntax.

Another approach would be to move the read_size subroutine's declaration outside of the block.

These are purely structural solutions; 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).

HTH

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

Replies are listed 'Best First'.
Re: Re: mod_perl: variable $end will not stay shared (nested subroutines problem)
by princepawn (Parson) on Apr 18, 2001 at 00:04 UTC
    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...

      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