in reply to Re^2: No garbage collection for my-variables
in thread No garbage collection for my-variables
I think that you've overplayed the case. Using a do block instead of an anonymous block makes it look more complicated than it is.
Even wrapping a local var in a bare block is rarely necessary. Most code is nested at some level in a if or while or other loop block or subroutine body.
On the rare occasions that it is at the top level of a program or module, if you really want it to be garbage collected, undef is better (in that it will actually achieve something) anyway.
Even the use of a constant is a emphasising the rare case. Mostly data is read in from external sources and is in a variable already, so:
while( my $var = <$fh> ) { mutate( $var ); use( $var ); }
is hardly onerous, but even that can be avoided. Thanks to perl's context sensitivity, you can have the best of both worlds. For the simple case, subroutines behave as passthru pass-by-value, but when the need arises to minimise memory allocation and copying, using it ina void context does the right thing:
#! perl -slw use strict; sub mutates { my $ref = defined wantarray ? \shift : \$_[ 0 ]; $$ref =~ s[(?<=\b[^ ])([^ ]+)(?=[^ ]\b)][scalar reverse $1]ge; return $$ref if defined wantarray; return; } sub doSomething { print shift; } doSomething( mutates( 'antidisestablishmentarismania' ) ); my $var = 'The quick brown fox jumps over the lazy dog'; mutates( $var ); doSomething( $var ); __END__ c:\test>junk ainamsiratnemhsilbatsesiditna The qciuk bworn fox jpmus oevr the lzay dog
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: No garbage collection for my-variables
by moritz (Cardinal) on Sep 16, 2008 at 22:36 UTC | |
by BrowserUk (Patriarch) on Sep 17, 2008 at 00:06 UTC | |
by moritz (Cardinal) on Sep 17, 2008 at 07:19 UTC | |
by BrowserUk (Patriarch) on Sep 17, 2008 at 08:28 UTC | |
by kyle (Abbot) on Sep 17, 2008 at 03:19 UTC | |
by BrowserUk (Patriarch) on Sep 17, 2008 at 05:53 UTC | |
by kyle (Abbot) on Sep 17, 2008 at 15:08 UTC | |
|