I can tell you're already happy with the solution you've got, but I'd like to recommend a few alternates anyway. :-)
I think you're losing a lot of flexibility by requiring the passed in coderef to be a named sub in the same namespace. Any time you're passing around coderefs, anonymous subs become more and more useful. To this end, I can think of a few ways to allow them:
- Pass in a label along with the coderef. This would allow the caller to use anonymous coderefs as well as refs to named subs, and would avoid the "in another namespace" problem.
- Pass in a label, but only optionally. If there is no label, the symbol table is searched. If there is one, it's used instead.
-
Pass in an object. At first this sounds a bit ugly, but it could be something as simple as:
foobar(Code::Wrapper->new( code => \&foo ));
foobar(Code::Wrapper->new( code => sub { "baz" }, label => "baz" )
+)
Then this object could encapsulate the behavior mentioned earlier, by either searching the symbol table or using the specified label.
Which one you choose depends on how much flexibility you want in the future, and how much you want to insulate certain parts of your code from others. I have a feeling you will just go with the symbol table scan, but hopefully you'll at least give my ideas some consideration.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.