Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: No garbage collection for my-variables

by BrowserUk (Patriarch)
on Sep 16, 2008 at 21:57 UTC ( [id://711810]=note: print w/replies, xml ) Need Help??


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

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^4: No garbage collection for my-variables
by moritz (Cardinal) on Sep 16, 2008 at 22:36 UTC
    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

    Now that's surely tempting, but could lead to rather odd situations:

    sub do_stuff { ... do_other_stuff($variable); # remove that debugging statement, and do_other_stuff # will behave very differently if do_stuff is not # called in void context print "still here\n"; }

    Admittedly that's a fairly artificial situation and won't show up in real code very often, but if it does it's very nasty to debug.

    Designing interfaces around performance optimizations and memory management oddities just doesn't seem right to me.

      If that function is called in a non-void context, it is returning the result of the "debug statement", which seems more than a little artificial.

      If the function is ever called in a non-void context, it will be because it returns something useful. If the return statement (or the fall-off-the-end expression) is omitted, then the function isn't going to work the way it is intended anyway, so any other error is academic. Just another justifiction I'm afraid.

      Plenty of languages use pass-by-reference, and thousands of programmers and millions of programs use it every day without dire consequences.

      Designing interfaces around performance optimizations and memory management oddities just doesn't seem right to me.

      You seem to be saying: Oh, I mustn't use this language feature, because that would make my code more efficient and that would be an optimisation and it might be premature!

      Why does Text::CSV_XS exist? Or the XS version of List::Util? Or ...

      It's changing your transatlantic flight path to take full advantage of (or avoid) the gulf stream. It's shutting off the fuel supply to the injectors when the engine is on overrun. Removing your roof rack when you aren't using it.

      Perl is a context sensitive language, it is a uniquie and powerful defining feature. Perl programmers need to be aware of it, understand it and use it. Pretending it doesn't exists, or running scared of it you might as well use VB.

      It drives me nuts. I use Perl, because I like the language. But there are people around here won't use map or grep; or statement modifiers; or unless or until. They eshew aliasing; context sensitivity; autovivification; the implicit variables: it and these. They want everything OO; will jump through hoops creating singleton classes, "to avoid globals" and end up with nothing more than lexically-scoped globals (eg. our). Use ReadOnly variables as constants instead of constants.

      Why bother using Perl?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        ou seem to be saying: Oh, I mustn't use this language feature, because that would make my code more efficient and that would be an optimisation and it might be premature!

        No, I'm not. I'm merely saying that ... well, what I said. Everything else was interpretation from you.

        Wouldn't it be great if we could actually fix the problems in the compiler, instead of work around them?

        Why does Text::CSV_XS exist? Or the XS version of List::Util? Or ...

        The XS version of List::Util is a perfect example of where the interface is not affected by performance optimizations at all. As a programmer I don't even know if the pure perl or the XS version is actually loaded, which is exactly how performance optimizations should be: unintrusive in terms of interface.

        It drives me nuts.

        Relax. Remember there's always somebody wrong on the internet.

        Just like any natural language it becomes weird if you over-use a particular feature. IMHO context sensitivity is nice, but best used sparingly. It should just DWIM, but when it's used too often I have to actually think about contexts, which defeats the whole purpose of contexts.

        And just like in natural languages opinions on how to best use it vary greatly, so I don't really expect to come to a consensus with you.

        Eschew obfuscation.

        There are parts of my natural language that I don't use for various reasons. I don't see why Perl should be any different.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://711810]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-19 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found