in reply to Re: local() for scalar file slurping
in thread local() for scalar file slurping

requires twice as much memory

Why?

print substr("Just another Perl hacker", 0, -2);
- apotheon
CopyWrite Chad Perrin

Replies are listed 'Best First'.
Re^3: local() for scalar file slurping
by ikegami (Patriarch) on Jul 28, 2006 at 23:30 UTC

      That's odd. Definitely let me know if you figure it out.

      print substr("Just another Perl hacker", 0, -2);
      - apotheon
      CopyWrite Chad Perrin

        I think that you use twice the memory in the version where $foo is assigned the results of the do{} block because you have the memory for the return value of the do{} block and the memory for the contents of $foo in use at the same time, so that you can copy from one to the other. Both contain a full copy of the whole file.

        In the version where $foo is assigned to from within the do{}, you just have the $foo variable to hold the whole file, since it is assigned to in chunks from the file buffer. There is no other space allocated to hold the contents returned by the do{} block because the value of the do{} is not assigned to anything. Thus only one copy of the whole file has to be held in memory at one time.

        That's the way I read it anyway.