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

I don't know, but BrowserUk confirmed my findings.

Replies are listed 'Best First'.
Re^4: local() for scalar file slurping
by apotheon (Deacon) on Jul 29, 2006 at 00:09 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.

        That may be the case but it's wrong behaviour on the part of the interpreter. The value being returned should be marked as a temporary and should be able to be re-used by the next part of the expression. It shouldn't be copied. If it is, that's a bug. Perl is supposed to be good at this kind of thing.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        Ahh, that makes sense. I suppose I should have figured that out for myself. Silly me.

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