Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
Can someone please tell me what anonymous subroutines are all about? I've been wading through someone elses code for months now trying to make it work better and they keep popping up...
Thanks,
Tom
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Anonymous Subroutines
by dsb (Chaplain) on Jul 30, 2004 at 13:50 UTC | |
For the quick rundown:
dsb This @ISA my cool %SIG | [reply] [d/l] |
|
Re: Anonymous Subroutines
by Limbic~Region (Chancellor) on Jul 30, 2004 at 14:11 UTC | |
You have already been given the answer to your question, some references for more information, and a classic example (dispatch tables) of when you might use anonymous subs. I will offer my favorite - closures:
Cheers - L~R | [reply] [d/l] |
|
Re: Anonymous Subroutines
by xorl (Deacon) on Jul 30, 2004 at 13:53 UTC | |
I knew less about them than you, but here are some interesting documents:
I hope this helps. Update Steve_p - turned URLs into real links | [reply] |
by ysth (Canon) on Jul 30, 2004 at 15:38 UTC | |
| [reply] |
|
Re: Anonymous Subroutines
by cfreak (Chaplain) on Jul 30, 2004 at 13:55 UTC | |
Well I'm not certain what exactly you're looking for. They are closely related to references so something like this: Is the same as:
I like to use a dispatch table in web apps that uses either anonymous subs or sub references. Like so:
Hope that gives you an idea of what's going on. | [reply] [d/l] [select] |
|
Re: Anonymous Subroutines
by FoxtrotUniform (Prior) on Jul 30, 2004 at 19:58 UTC | |
Let me try to give you a different perspective on anonymous subs than you've gotten so far. (Not that what's been written is bad, wrong, or inaccurate; it's all very good stuff. But it's not the whole story.) Perl allows you to treat functions as first-class data objects -- you can pass them to other functions, return them, store (references to) them, and generate them on the fly. This is difficult to handle with the usual symbol table mechanism (named functions that you call just like builtins), so Perl lets you store (refs to) functions in scalars. This is where you get anonymous functions: functions that aren't in the symbol table. Here's an example: suppose you're turning Usenet-style text (*bold*, _italics_,, etc) into HTML-style markup. One way to do it would be to write a bunch of conversion functions: but that gets tedious pretty quick. Another way (and I'm not saying it's the best way, but it illustrates the point) is to write a function to generate the conversion functions: Then you can keep all the translation patterns in a config file, and build your translation functions on the fly: Want to add support for +monospace text+? Thanks to your translation-function builder, all you have to do is add a line to your config file. This may seem unnecessarily complex, but what it does is keep the complexity in a small part of your code -- the translation-function builder -- rather than spread it out over dozens of functions. | [reply] [d/l] [select] |
|
Re: Anonymous Subroutines
by gmpassos (Priest) on Jul 30, 2004 at 20:34 UTC | |
But the most usefult thing of anonymous sub is closure: Output: Also is useful to create functions with eval: You also can redefine a normal sub with the declaration of a anonymous sub: See perlref and perlsub
Graciliano M. P. | [reply] [d/l] [select] |
|
Re: Anonymous Subroutines
by doom (Deacon) on Jul 31, 2004 at 09:18 UTC | |
What File::Find does for you is to descend through one or more trees of directories, and it runs some user defined code at each point through the tree. So the author designed around the idea of passing in a code reference to an arbitrary routine which can you can call anything you want. In this example it's "wanted", in another application it might be "unlink_big_files". (There's a strong argument that this File::Find interface was a bad idea, and it has a number of competitors these days like File::Find::Rule, but still if you're working on existing code, you're likely to see File::Find used.) Maybe you should try and give us a better idea of what you don't get about anonymous subs... | [reply] [d/l] |