in reply to Some trouble with closures

Suppose you have a method of a class that you write using Moose(but a plain Perl object is also relevant), and you use inside that method a static variable (static in the C sense, by that I mean a variable that has scope local to the method but a lifetime that spans each call of said method). I was recommended that I use a closure and enclose the declaration of that variable and the method inside the closure to cause the variable to be static(again, the C sense of static).
My recommendation would be to declare the variable with the static keyword inside the sub you want to static variable.
give up the closure and the static(in C sense) variable and go for an attribute in the class that would be used for this, which if you use Moose has the advantage of being able to specify default => value , so the attribute is set to that value upon constructing a new object
Huh? What? Now I'm confused. Do you want a static variable, or not? If it's static (as in the C sense; and in the Perl sense), there's just one. If you want a different one per object, you don't have static variables; you have your classic, run-of-the-mill, 14-in-a-dozen, object attribute. But C doesn't have objects, so if it's "in the C sense", it cannot be related to objects, can it?

Perhaps you should explain what you want this variable to be.

Replies are listed 'Best First'.
Re^2: Some trouble with closures
by DStaal (Chaplain) on Feb 17, 2010 at 13:51 UTC
    My recommendation would be to declare the variable with the static keyword inside the sub you want to static variable.

    Perl 5.8.x is still the default perl for most OS's at this point. I'd recommend that someone know both ways, but unless they can be sure their project is going to be running on the latest version of perl, I'd use the slightly clunkier style. Synatic sugar is nice, but having a working program is better.

    Of course, the point that they should know whether they actually want a static variable or not is right on.

      Perl 5.8.x is still the default perl for most OS's at this point.
      I don't get this argument. We always tell people to download the whole world from CPAN, even offering how to use modules if you can't install them the place your sysadmin does.

      But going to CPAN and getting perl itself is somehow a big NoNo?

        Managing multiple different sourced programs quickly becomes chaotically complex. It's much better to maintain a single update source for major system components.

        Or: Keeping track of when you need to update you own installed version of perl, as well as the system version of perl, is just more work than most people want to deal with.

        Also: In case you are the sysadmin, it's fairly easy to accidentally replace the system perl with your new version. Which could have immediate or delayed problems. Immediate if something else in the system is broken by the new version of perl, delayed when your next system update removes the new version and puts the old version back (and then breaks some of your scripts...).

        The last one, notably, doesn't apply to CPAN modules generally (there are a very few cases where it does...): If you install something that's not in the system, it won't affect anything in the system, as they will never use it.

        And, depending on the situation, the developer may not have control over - or even access to - the deployment environment. Think of the case where they are developing something for a department, which will then be placed on multiple department servers.

        Generally, I think of it as best to require as few auxillary software changes as possible when you are developing software. If there is something that really will make a difference in how long it will to get the program written (or if it gets written), use it, but if it would just be 'nice to have', try doing without.

        Often that will make a difference in whether you can get approval to use the script you just wrote, or not.

        I don't get this argument. We always tell people to download the whole world from CPAN, even offering how to use modules if you can't install them the place your sysadmin does.

        But going to CPAN and getting perl itself is somehow a big NoNo?

        It's not so much a "NoNo" as potentially a lot of work for the testing staff and/or the sysadmins.

        If you install a new module from CPAN, that module can't break your existing code, because there isn't any. No additional testing is needed to prove that you can safely promote your code.

        If you need to upgrade an existing CPAN module, you will need to do some extra testing to in order to prove that the new version doesn't break any existing production code which uses that CPAN module.

        If you need to upgrade perl itself, you need to do a lot of extra testing to prove that the new version of perl doesn't break any existing production code that uses perl.

        Depending upon how much perl code is in production, this can be a lot of extra work. I've seen a new installation take several weeks to approve and complete.

        Whether or not it's worth it to do that work is a business decision: based upon some kind of a cost/benefit analysis of the relative merits, costs, and risks.