Syntactic sugar for scope closing hook?

Discussion in 'Perl Misc' started by Steve Roscio, Jan 9, 2009.

  1. Steve Roscio

    Steve Roscio Guest

    Howdy -

    I have a module where its functions work in pairs: an opening and
    closing pairs. Because it's likely that these functions will be used
    where the programmer also uses exceptions (eval/die), or just wants to
    be lazy, it's important that the close function is always called when
    exiting the enclosing scope. I find it tedious to track down every
    possible exit path and include the closure.

    The simple way to achieve this is to create and object and have a
    DESTROY method for it:
    {
    my $trak = function_returning_some_object (args...);
    . . .
    } # DESTROY() method called here, invokes close function

    That's OK, but I'd like to take it to the next level and make it
    syntactically look nicer. Some sugar, if you will. I want to just do this:

    {
    blah args...; # blah creates object in *current* scope
    . . .
    } # Closure for 'blah' object called

    How can I do this?

    I've had some success by stuffing a closure (with a DESTROY) into the
    caller(1)'s symbol glob. But that only handles the case when the scope
    is the enclosing sub. How do I do this for local scopes created simply
    by pairs of { }'s ?

    I've also done source filters that simply convert the sugar-coated form
    into the primitive form. But that feels evil.

    Thanx in advance,
    - Steve

    PS: Sorry, I know I'm not explaining this well. I hope you can get the
    gist of it.
     
    Steve Roscio, Jan 9, 2009
    #1
    1. Advertising

  2. Steve Roscio

    C.DeRykus Guest

    On Jan 9, 2:08 pm, Steve Roscio <> wrote:
    > Howdy -
    >
    > I have a module where its functions work in pairs: an opening and
    > closing pairs. Because it's likely that these functions will be used
    > where the programmer also uses exceptions (eval/die), or just wants to
    > be lazy, it's important that the close function is always called when
    > exiting the enclosing scope. I find it tedious to track down every
    > possible exit path and include the closure.
    >
    > The simple way to achieve this is to create and object and have a
    > DESTROY method for it:
    > {
    > my $trak = function_returning_some_object (args...);
    > . . .
    > } # DESTROY() method called here, invokes close function
    >
    > That's OK, but I'd like to take it to the next level and make it
    > syntactically look nicer. Some sugar, if you will. I want to just do this:
    >
    > {
    > blah args...; # blah creates object in *current* scope
    > . . .
    > } # Closure for 'blah' object called
    >
    > How can I do this?
    >
    > I've had some success by stuffing a closure (with a DESTROY) into the
    > caller(1)'s symbol glob. But that only handles the case when the scope
    > is the enclosing sub. How do I do this for local scopes created simply
    > by pairs of { }'s ?
    >
    > I've also done source filters that simply convert the sugar-coated form
    > into the primitive form. But that feels evil.


    IIUC Scope::Guard may be able to help:

    use Scope::Guard;

    {
    my $cleanup = sub { print "cleaning up...\n"; };
    my $guard = Scope::Guard->new( $cleanup );
    }

    --
    Charles DeRykus
     
    C.DeRykus, Jan 10, 2009
    #2
    1. Advertising

  3. Steve Roscio

    Kevin Ryde Guest

    Steve Roscio <> writes:
    >
    > syntactically look nicer


    There's quite a few of those finalizer things. Hook::Scope or
    B::Hooks::EndOfScope might do enough syntax for you. Scope::Upper might
    help create such a thing. The basic ones include AtExit, Guard and
    Sub::ScopeFinalizer, but personally I've found the little Scope::Guard
    already mentioned enough.
     
    Kevin Ryde, Jan 10, 2009
    #3
  4. Steve Roscio

    Steve Roscio Guest

    Thanx guys - that's what I needed. I like the XS methods done by Guard
    and B::Hooks::EndOfScope. But I'm not yet very comfortable writing my
    own XS so I'll use Scope::Upper until I am.
     
    Steve Roscio, Jan 13, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Heyes

    Is -> syntactic sugar?

    Jason Heyes, Dec 15, 2003, in forum: C++
    Replies:
    17
    Views:
    588
    Ron Natalie
    Dec 19, 2003
  2. Replies:
    1
    Views:
    409
    Victor Bazarov
    Jan 24, 2005
  3. Christopher T King

    xrange() syntactic sugar

    Christopher T King, Jan 9, 2004, in forum: Python
    Replies:
    0
    Views:
    324
    Christopher T King
    Jan 9, 2004
  4. F Jamitzky

    Ruby like syntactic sugar

    F Jamitzky, Mar 2, 2004, in forum: Python
    Replies:
    6
    Views:
    370
    Raymond Hettinger
    Mar 4, 2004
  5. Bas
    Replies:
    4
    Views:
    307
    Diez B. Roggisch
    Apr 14, 2006
Loading...

Share This Page