Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Anonymous sub

Status
Not open for further replies.

Tve

Programmer
May 22, 2000
166
FR
I'm trying to compute a value and return it to a scalar. want to do this through an anonymous sub, but I can't figure out the syntax. I basically have something like

$val = sub { return 2 };

I'm creating a reference to the sub, but I would actualy like to get the value 2 in $val.

Must sound simple, but I've never worked with anonymous subdbs before....

AD AUGUSTA PER ANGUSTA

Thierry
 
Will this work out for you?

Code:
my $subref = sub {return 2;};
my $val = &{$subref};
print $val;
 
Your problem sounds artificial... is this homework? Is this just for the sake of learning and tinkering?

There are a ton of other ways to compute a value, why do you HAVE to use an anonymous subroutine?

--jim
 
Could work, but I actually need the answer several times, which implies that the sub would also be called several times instead of once.

I had actually figured out this solution ( and currently using it) , but there must be a better way....

Thanks


AD AUGUSTA PER ANGUSTA

Thierry
 
So put it inside of a loop......why dont you post your solution so we can help you further......
 
jim,

I suppose you're right. But I get these kind of idees and it just keeps my mind going until I find a solution (or get some help of course).

I suppose I would be better of with a beer.



AD AUGUSTA PER ANGUSTA

Thierry
 
Jim,

I did want to come back on a point....

You mention that there are tons of ways to compute values...of course. Maybe this is just the way I program, but I often end up with a complete set of small subs. I generally end up making small subs that are only called up once just for clarity, to avoid polluting my main code with all kind of loops and conditionnals. Most of all, I also like to use subs to scope my variables and make sure I don't have to maintain to many of them in the main program.

I suppose this is why I was looking at anonymous subs. I kept looking and I could use the eval function, but I thought I read one that eval have a pretty big impact on performance.

Any thoughts?





AD AUGUSTA PER ANGUSTA

Thierry
 
Thierry,

I don't think that Jim was saying that there is anything wrong with using subs, but, as he said, Why do you have to use an anonymous sub?

Why not just a normal sub, like I'm guessing you have through the rest of your code? I think the question was something like: what's the motivation for using an anonymous sub?
 
This is all very theoretical of course...

I suppose it's just to make the overall code clearer...
When you end up with tens of subs, it just makes it harder to keep an overview of the program flow.

Anyway, like Jim said, it's mostly "just for the sake of learning and tinkering".



AD AUGUSTA PER ANGUSTA

Thierry
 
I have find the solution:

Code:
sub anonimous()
{
 print "007\n";
}
 
Unless that's a joke, I think you need to look up the definition of the word "anonymous". :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top