in reply to Re: No garbage collection for my-variables
in thread No garbage collection for my-variables

In addition to moritz's excellent point that a function that modifies its arguments then could not be called with a literal, I'd also point out that a lot of Perl programmers probably don't know that @_ is full of aliases. I'd been programming in Perl off and on for over ten years before I came to the Monastery and learned that @_ is aliases. I've asked about this feature in interviews I've conducted, and the prospects out there have always been surprised at this feature. Documentation helps, of course, but someone who doesn't know this is possible could spend an awful lot of time debugging before discovering this (as you say) action at a distance.

Thumbs up on the use less, however.

Replies are listed 'Best First'.
Re^3: No garbage collection for my-variables
by BrowserUk (Patriarch) on Sep 16, 2008 at 22:10 UTC

      @a = sort @a is done in place before 5.10

      >perl580\bin\perl -MO=Concise -e"@a = sort @a" 2>&1 | find "sort" 7 <@> sort lK ->8 >perl588\bin\perl -MO=Concise -e"@a = sort @a" 2>&1 | find "sort" 7 <@> sort lK/INPLACE ->8 >perl5100\bin\perl -MO=Concise -e"@a = sort @a" 2>&1 | find "sort" 7 <@> sort lK/INPLACE ->8

      I don't have 5.8.1 to 5.8.7, so let's consult the perldeltas.

      perl584delta:

      In place sort optimised (eg @a = sort @a)

      But it was buggy in 5.8.4. perl585delta:

      The in-place sort optimisation introduced in 5.8.4 had a bug. For example, in code such as @a = sort ($b, @a), the result would omit the value $b. This is now fixed.

      It's the same mechanism that sort uses for in-place sorting in 5.10. I've thought about patching List::Util::shuffle() in the same way.

      I thought what you meant was that a subroutine can detect its being called in void context? But if I am not mistaken, 5.10's in-place sorting does not happen in void context:

      % perl5.10.0 -lwe '@a = (2,1); sort @a; print @a' Useless use of sort in void context at -e line 1. 21

      As I understand it, perl (5.10) will detect that in @a = sort @a, the destination array is the same as the source array, so it uses a more efficient algorithm (but it's still in list context).

Re^3: No garbage collection for my-variables
by shmem (Chancellor) on Sep 16, 2008 at 22:48 UTC
    that a function that modifies its arguments then could not be called with a literal

    There are edge cases. See foreach funny business.