S
Steve Roscio
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.
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.