http://www.perlmonks.org?node_id=771176


in reply to Re: Using closures to achieve data hiding in OO Perl
in thread Using closures to achieve data hiding in OO Perl

That is a much better way to use closures. Rather than accessing the method through, $obj->{'key'}->(), it makes more sense to do it the way you've posted. Thanks. Interesting comment that one, about closures being hard to debug. In a big application, data hiding can come at a cost.

How often do you guys use closures? I mean, there are certain obvious places where closures can help like callbacks. I've never used closures. Never felt the need to. But citing from your experience, are there any other places where using closures is a good idea?

  • Comment on Re^2: Using closures to achieve data hiding in OO Perl

Replies are listed 'Best First'.
Re^3: Using closures to achieve data hiding in OO Perl
by demerphq (Chancellor) on Jun 13, 2009 at 11:26 UTC

    I use closures all the time. But never explicitly for data hiding (although it can come as a byproduct). Closures especially when coupled with eval, makes for an extremely powerful approach to solving problems, optimizing, simplifying design, and providing encapsulation. Using closures you can basically avoid OO, and do things that are extremely inefficient to do via OO techniques (especially in perl where method calls are slow). In fact I'd argue that closures are generally speaking more useful than OO.

    Go and get yourself a copy of Higher Order Perl by dominus. Its one of the better Perl books out there, and its almost all about using closures.

    Note: I updated this node by changing the previous link to mjd to the correct dominus.

    ---
    $world=~s/war/peace/g

      Go and get yourself a copy of Higher Order Perl by dominus. Its one of the better Perl books out there, and its almost all about using closures.

      Thanks for the link. It looks interesting. Will definitely read it.

        Dont thank me for linking to it, thank dominus for writing it. Writing perl books is really really hard, and he did an absolutely outstanding job on it. Additionally if you ever get a chance to attend any kind of talk, or workshop or lecture or even just a beer with him then take it. It will definitely be worth it. He also has an excellent set of online resources: perl.plover.com

        ---
        $world=~s/war/peace/g

Re^3: Using closures to achieve data hiding in OO Perl
by ikegami (Patriarch) on Jun 13, 2009 at 20:40 UTC

    I didn't say closures were hard to debug, and I don't think that's true in most cases.

    What's difficult to debug are objects that hide their attributes. You can't dump them (fixable) or inspect them in a debugger (not fixable).

    In C, private attributes are hidden by denying code access to them. The debugger isn't subject to those access restrictions, so it can access private attributes just as easily as public ones. In Perl, the attributes aren't simply hidden. They are fundamentally different than public attributes. In a sense, they're not even in the object. (In inside-out objects, they truly aren't in the object at all.)

    So what has no cost in C has a great cost in Perl.

    How often do you guys use closures?

    Often (but never for the purpose you propose). I worked on a VB project recently, and closures were what I missed the most.