Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > creating anonymous subroutines at runtime

Reply
Thread Tools

creating anonymous subroutines at runtime

 
 
Arne Gödeke
Guest
Posts: n/a
 
      08-25-2004
hey everyone,

is there a possibility to create anonymous subroutines at runtime with code-refs inside?
I am aware of 'eval' but I cannot get it working like this (example to makeclear what I want):

$callback = sub { "some code" }
$cb = eval "sub { &{$cb}(); }"; # This certainly fails.. since CODE(0xwhatever) is not a code-ref

$cb should then be passed somewhere else. Basically, I just want to create nested anonymous subroutines..

thanks in advance!
arne gödeke


 
Reply With Quote
 
 
 
 
Arne Gödeke
Guest
Posts: n/a
 
      08-25-2004
On Wed, 25 Aug 2004 19:06:45 +0200
Arne Gödeke <(E-Mail Removed)> wrote:

> hey everyone,
>
> is there a possibility to create anonymous subroutines at runtime with code-refs inside?
> I am aware of 'eval' but I cannot get it working like this (example to make clear what I want):
>
> $callback = sub { "some code" }
> $cb = eval "sub { &{$cb}(); }"; # This certainly fails.. since CODE(0xwhatever) is not a code-ref
>
> $cb should then be passed somewhere else. Basically, I just want to create nested anonymous subroutines..
>
> thanks in advance!
> arne gödeke
>

erm.. sorry, I made a mistake... that line should read:

$cb = eval "sub { &{$callback}(); }"; # This certainly fails.. since CODE(0xwhatever) is not a code-ref
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      08-25-2004


Arne Gödeke wrote:
> On Wed, 25 Aug 2004 19:06:45 +0200
> Arne Gödeke <(E-Mail Removed)> wrote:
>
>
>>hey everyone,
>>
>>is there a possibility to create anonymous subroutines at runtime with code-refs inside?
>>I am aware of 'eval' but I cannot get it working like this (example to make clear what I want):
>>
>>$callback = sub { "some code" }
>>$cb = eval "sub { &{$cb}(); }"; # This certainly fails.. since CODE(0xwhatever) is not a code-ref
>>
>>$cb should then be passed somewhere else. Basically, I just want to create nested anonymous subroutines..
>>
>>thanks in advance!
>>arne gödeke
>>

>
> erm.. sorry, I made a mistake... that line should read:
>
> $cb = eval "sub { &{$callback}(); }"; # This certainly fails.. since CODE(0xwhatever) is not a code-ref



Well the problem here is that $callback is iterpolating. Simply escape
the $ (or use some other quoting) to prevent this.

But I suspect there's more (or maybe less) to what you are tring to do
than you are describing.

Be warned - this sort of game very easily leads to memory leaks.

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-25-2004
"Arne Gödeke" <(E-Mail Removed)> wrote in message
news:20040825190920.7a19d34b.cpyfykf02@sneakemail. com...
On Wed, 25 Aug 2004 19:06:45 +0200 Arne Gödeke <(E-Mail Removed)>
wrote:

>> hey everyone,
>>
>> is there a possibility to create anonymous subroutines at runtime with

code-refs inside?
>> I am aware of 'eval' but I cannot get it working like this (example to

make clear what I want):
>>
>> $callback = sub { "some code" }
>> $cb = eval "sub { &{$cb}(); }"; # This certainly fails.. since

CODE(0xwhatever) is not a code-ref
>>
>> $cb should then be passed somewhere else. Basically, I just want to

create nested anonymous subroutines..
>>
>> thanks in advance!
>> arne gödeke
>>

> erm.. sorry, I made a mistake... that line should read:
> $cb = eval "sub { &{$callback}(); }"; # This certainly fails.. since

CODE(0xwhatever) is not a code-ref

I don't really understand why you would want to do this, but the following
worked for me when I modified your code:

$callback = sub {
print "Hello World\n";
};
$cb = eval 'sub { &{$callback}; }';
$cb->();

The main problem is that your double quotes in the eval were interpolating
$callback, whereas you wanted the eval to see the variable name. Changing to
single quotes eliminates this problem.

Paul Lalli


 
Reply With Quote
 
Arne Gödeke
Guest
Posts: n/a
 
      08-25-2004
On Wed, 25 Aug 2004 17:29:33 GMT
"Paul Lalli" <(E-Mail Removed)> wrote:

> "Arne Gödeke" <(E-Mail Removed)> wrote in message
> news:20040825190920.7a19d34b.cpyfykf02@sneakemail. com...
> On Wed, 25 Aug 2004 19:06:45 +0200 Arne Gödeke <(E-Mail Removed)>
> wrote:
>
> >> hey everyone,
> >>
> >> is there a possibility to create anonymous subroutines at runtime with

> code-refs inside?
> >> I am aware of 'eval' but I cannot get it working like this (example to

> make clear what I want):
> >>
> >> $callback = sub { "some code" }
> >> $cb = eval "sub { &{$cb}(); }"; # This certainly fails.. since

> CODE(0xwhatever) is not a code-ref
> >>
> >> $cb should then be passed somewhere else. Basically, I just want to

> create nested anonymous subroutines..
> >>
> >> thanks in advance!
> >> arne gödeke
> >>

> > erm.. sorry, I made a mistake... that line should read:
> > $cb = eval "sub { &{$callback}(); }"; # This certainly fails.. since

> CODE(0xwhatever) is not a code-ref
>
> I don't really understand why you would want to do this, but the following
> worked for me when I modified your code:
>
> $callback = sub {
> print "Hello World\n";
> };
> $cb = eval 'sub { &{$callback}; }';
> $cb->();
>
> The main problem is that your double quotes in the eval were interpolating
> $callback, whereas you wanted the eval to see the variable name. Changingto
> single quotes eliminates this problem.

Ya, well.. I know all that. My problem is, that $callback in the new sub isnot a copy of $callback but rather
that same var. By using double-quotes I just tried to indicate that I need a copy. $callback changes afterwards.
Try that piece of code:

my $callback = sub {
print "Hello World\n";
};

my $cb = eval { sub { &{$callback}(); } };
$callback = 0;
$cb->();

Is there any way to avoid that problem?

arne gödeke

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-25-2004

"Arne Gödeke" <(E-Mail Removed)> wrote in message
news:20040825212749.33c933fe.cpyfykf02@sneakemail. com...
> On Wed, 25 Aug 2004 17:29:33 GMT "Paul Lalli" <(E-Mail Removed)> wrote:


> > The main problem is that your double quotes in the eval were

interpolating
> > $callback, whereas you wanted the eval to see the variable name.

Changing to
> > single quotes eliminates this problem.

> Ya, well.. I know all that. My problem is, that $callback in the new sub

is not a copy of $callback but rather
> that same var. By using double-quotes I just tried to indicate that I need

a copy. $callback changes afterwards.
> Try that piece of code:
>
> my $callback = sub {
> print "Hello World\n";
> };
>
> my $cb = eval { sub { &{$callback}(); } };
> $callback = 0;
> $cb->();
>
> Is there any way to avoid that problem?


Now I'm more confused. Now it sounds like you just want $cb to call
whatever function was defined by $callback at the moment you define $cb. So
why are you bothering with 'eval' at all?

my $callback = sub { print "Hello World\n"; };
my $cb = $callback;
$callback=0;
$cb->();

I think at this point it might be helpful if you explained *why* you're
doing all this. What is the actual goal, for which you've decided the
correct solution is to use this "nested anonymous routine" method?

Paul Lalli


 
Reply With Quote
 
Arne Gödeke
Guest
Posts: n/a
 
      08-25-2004
> Now I'm more confused. Now it sounds like you just want $cb to call
> whatever function was defined by $callback at the moment you define $cb. So
> why are you bothering with 'eval' at all?
>
> my $callback = sub { print "Hello World\n"; };
> my $cb = $callback;
> $callback=0;
> $cb->();
>
> I think at this point it might be helpful if you explained *why* you're
> doing all this. What is the actual goal, for which you've decided the
> correct solution is to use this "nested anonymous routine" method?
>
> Paul Lalli


Okay, you're right. This was a bit confusing.
Perhaps it would be too much to explain the whole thing. Basically the problem
is that I get a code-ref and need to wrap an anonymous sub around it which does some
things and then calls the original procedure. There would be some other ways to achieve
the same goal.. but now I am bothered by the fact that I can not get it working
this way.

arne gödeke


 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      08-25-2004

Arne Gödeke wrote:

> I am bothered by the fact that I can not get it working
> this way.


In what way does your orginal code not work once you insert the missing
backlash?

 
Reply With Quote
 
Arne Gödeke
Guest
Posts: n/a
 
      08-25-2004
> > I am bothered by the fact that I can not get it working
> > this way.

>
> In what way does your orginal code not work once you insert the missing
> backlash?


I posted an example of the problem. The $callback may change
afterwards and so does $callback in the new subroutine which
is expected behaviour. I want to avoid that. To indicate
that I used double-quotes.. I want $callback inside the new
anonymous sub to be a copy of the original $callback so it
does never change.

arne gödeke

try this:

my $callback = sub {
print "Hello World\n";
};

my $cb = eval ' sub { &{$callback}(); } ';
$cb->(); # print "Hello World"
$callback = 0;
$cb->(); # does not
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      08-25-2004


Arne Gödeke wrote:

> On Wed, 25 Aug 2004 17:29:33 GMT
> "Paul Lalli" <(E-Mail Removed)> wrote:
>
>>The main problem is that your double quotes in the eval were interpolating
>>$callback, whereas you wanted the eval to see the variable name. Changing to
>>single quotes eliminates this problem.

>
> Ya, well.. I know all that. My problem is, that $callback in the new sub is not a copy of $callback but rather
> that same var. By using double-quotes I just tried to indicate that I need a copy. $callback changes afterwards.
> Try that piece of code:
>
> my $callback = sub {
> print "Hello World\n";
> };
>
> my $cb = eval { sub { &{$callback}(); } };
> $callback = 0;
> $cb->();
>
> Is there any way to avoid that problem?


OK, can you explain in what way the simple solution

my $cb = $callback;

Suggested earlier in this thread falls short of your requirements?

 
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
Prototypes and anonymous subroutines Brad Baxter Perl Misc 10 06-01-2007 11:46 PM
Is this a local anonymous class or a member anonymous class Reporter Java 3 05-12-2007 05:23 AM
variable interpolation of refs to anonymous subroutines cartercc@gmail.com Perl Misc 29 05-19-2006 05:32 PM
variable interpolation of refs to anonymous subroutines cartercc@gmail.com Perl Misc 0 05-16-2006 10:18 PM
creating named subroutines in a loop petercable@gmail.com Perl Misc 9 10-19-2005 11:21 AM



Advertisments