Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Inject fresh variable in scope

Reply
Thread Tools

Inject fresh variable in scope

 
 
Andrei Alexandrescu (See Website For Email)
Guest
Posts: n/a
 
      11-12-2005
Hello,


I set out to write a useful OnScopeExit feature that's supposed to
execute some code when the current scope is exited. What I came up with is:

package OnScopeExit;
my $todo = sub {};
sub new { shift; $todo = shift; bless {}; }
sub DESTROY {
&$todo;
}
package main;
sub OnScopeExit(&) {
return new OnExit(shift);
}

Now I can say in any scope I please:

{
... code ...
my $tempFile = "/tmp/temp.txt";
my $cookie1 = OnScopeExit { unlink($tempFile); };
... code ...
}

Sure enough, when the current scope exits, the local $cookie1 will have
its reference count go to zero, the DESTROY sub gets called, and the
code that deletes the temporary file is invoked. Very nice.

The only annoying part is that I need to define some "my" variable with
a unique name for each OnScopeExit action I want to perform. Ideally,
I'd only need to write:

{
... code ...
my $tempFile = "/tmp/temp.txt";
OnScopeExit { unlink($tempFile); }
... code ...
}

that is, the creation of a lexically-scoped anonymous variable that will
go out of scope should be somehow automated.

I looked through symbol tables, scratchpads, and the ilk, for a couple
of hours, to no avail. Is there a way?


Thanks,

Andrei
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      11-13-2005
"Andrei Alexandrescu (See Website For Email)"
<(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> I set out to write a useful OnScopeExit feature that's supposed to
> execute some code when the current scope is exited.


....

> Sure enough, when the current scope exits, the local $cookie1 will
> have its reference count go to zero, the DESTROY sub gets called, and
> the code that deletes the temporary file is invoked. Very nice.
>
> The only annoying part is that I need to define some "my" variable
> with a unique name for each OnScopeExit action I want to perform.


If you are willing to live with the necessity of creating only one
lexically scoped AtExit object, you might find:

http://search.cpan.org/~bradapp/AtExit-2.01/AtExit.pm

useful.

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
 
 
 
Eric J. Roode
Guest
Posts: n/a
 
      11-13-2005
"Andrei Alexandrescu (See Website For Email)"
<(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> Hello,
>
>
> I set out to write a useful OnScopeExit feature that's supposed to
> execute some code when the current scope is exited. What I came up
> with is:
>
> package OnScopeExit;
> my $todo = sub {};
> sub new { shift; $todo = shift; bless {}; }
> sub DESTROY {
> &$todo;
> }
> package main;
> sub OnScopeExit(&) {
> return new OnExit(shift);
> }
>
> Now I can say in any scope I please:
>
> {
> ... code ...
> my $tempFile = "/tmp/temp.txt";
> my $cookie1 = OnScopeExit { unlink($tempFile); };
> ... code ...
> }
>
> Sure enough, when the current scope exits, the local $cookie1 will
> have its reference count go to zero, the DESTROY sub gets called, and
> the code that deletes the temporary file is invoked. Very nice.
>
> The only annoying part is that I need to define some "my" variable
> with a unique name for each OnScopeExit action I want to perform.
> Ideally, I'd only need to write:
>
> {
> ... code ...
> my $tempFile = "/tmp/temp.txt";
> OnScopeExit { unlink($tempFile); }
> ... code ...
> }
>
> that is, the creation of a lexically-scoped anonymous variable that
> will go out of scope should be somehow automated.
>
> I looked through symbol tables, scratchpads, and the ilk, for a couple
> of hours, to no avail. Is there a way?


I don't believe so. I tried this same sort of thing a while back,
and I couldn't think of any way.

Two things about your object class, above. One, you should not use
a package variable ($todo) to store your action. What if someone needed
to do this?

{
my $one = OnScopeExit {...};
{
my $two = OnScopeExit {...};
}
}

For each object, store the to-do action *in* the object itself.
Instead of:
> sub new { shift; $todo = shift; bless {}; }

do
sub new { shift; bless { todo => shift }; }

Second: Don't hardcode OnScopeExit to be in package main. What if
you need an end-of-scope action in a module you're developing, a
module that exists in a different namespace than "main"? Export it
properly, via the Exporter module.

HTH.

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
Reply With Quote
 
brianr@liffe.com
Guest
Posts: n/a
 
      11-14-2005
"Eric J. Roode" <(E-Mail Removed)> writes:

> "Andrei Alexandrescu (See Website For Email)"
> <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>
>> Hello,
>>
>>
>> I set out to write a useful OnScopeExit feature that's supposed to
>> execute some code when the current scope is exited. What I came up
>> with is:
>>
>> package OnScopeExit;
>> my $todo = sub {};
>> sub new { shift; $todo = shift; bless {}; }
>> sub DESTROY {
>> &$todo;
>> }
>> package main;
>> sub OnScopeExit(&) {
>> return new OnExit(shift);
>> }
>>
>> Now I can say in any scope I please:
>>
>> {
>> ... code ...
>> my $tempFile = "/tmp/temp.txt";
>> my $cookie1 = OnScopeExit { unlink($tempFile); };
>> ... code ...
>> }
>>
>> Sure enough, when the current scope exits, the local $cookie1 will
>> have its reference count go to zero, the DESTROY sub gets called, and
>> the code that deletes the temporary file is invoked. Very nice.
>>
>> The only annoying part is that I need to define some "my" variable
>> with a unique name for each OnScopeExit action I want to perform.
>> Ideally, I'd only need to write:
>>
>> {
>> ... code ...
>> my $tempFile = "/tmp/temp.txt";
>> OnScopeExit { unlink($tempFile); }
>> ... code ...
>> }
>>
>> that is, the creation of a lexically-scoped anonymous variable that
>> will go out of scope should be somehow automated.
>>
>> I looked through symbol tables, scratchpads, and the ilk, for a couple
>> of hours, to no avail. Is there a way?

>
> I don't believe so. I tried this same sort of thing a while back,
> and I couldn't think of any way.
>
> Two things about your object class, above. One, you should not use
> a package variable ($todo) to store your action. What if someone needed
> to do this?
>
> {
> my $one = OnScopeExit {...};
> {
> my $two = OnScopeExit {...};
> }
> }
>
> For each object, store the to-do action *in* the object itself.
> Instead of:
>> sub new { shift; $todo = shift; bless {}; }

> do
> sub new { shift; bless { todo => shift }; }
>
> Second: Don't hardcode OnScopeExit to be in package main. What if
> you need an end-of-scope action in a module you're developing, a
> module that exists in a different namespace than "main"? Export it
> properly, via the Exporter module.


ISTM that it could be done with a source filter e.g.

use strict;
use warnings;

package OnScopeExitHelper;

sub new ($&) {
shift;
return bless {code => shift};
}

sub DESTROY {
&{$_[0]->{code}};
}

package UniqueVarName;

require Tie::Scalar;
our @ISA = qw /Tie::StdScalar/;

our $base_var = "aaaaaaaaa";

sub FETCH {
return $base_var++;
}

package OnScopeExit;

use Filter::Simple;
use Regexp::Common qw /balanced/;

our $var;
tie $var, 'UniqueVarName';

FILTER_ONLY code => sub { s{\b OnScopeExit \b
\s+
( $RE{balanced}{-parens=>'{}'} )
}
{my \$$var = OnScopeExitHelper->new(sub $1)
}gsx
};

1;

Seems to work, but I expect that it could be improved.

HTH

--
Brian Raven
That should probably be written:
no !@#$%^&*:@!semicolon
-- Larry Wall in <(E-Mail Removed)>
 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      11-14-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in news:(E-Mail Removed):

> ISTM that it could be done with a source filter
>
>


Hmmm, that's not a bad idea. I must explore that.

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
Reply With Quote
 
Andrei Alexandrescu (See Website For Email)
Guest
Posts: n/a
 
      11-15-2005
(E-Mail Removed) wrote:
> ISTM that it could be done with a source filter e.g.


(snip)

Thanks! Interesting idea; I got it to work, but then I noticed someone
has written something along the same lines:

http://search.cpan.org/~abergman/Hoo...-0.04/Scope.pm

Now, I tried to install that, and found out it depends on a zillion
other modules. Now, the question is: is there a simple way to manage
dependencies in Perl - i.e., download a package and all of its
dependents from somewhere? Thanks!


Andrei
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-15-2005
Andrei Alexandrescu (See Website For Email) <(E-Mail Removed)> wrote:

> Now, the question is: is there a simple way to manage
> dependencies in Perl - i.e., download a package and all of its
> dependents from somewhere?



perldoc CPAN


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
inject does not inject last value Peņa, Botp Ruby 4 08-07-2006 09:26 AM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments