Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
This is actually a two-part question:

1. Can Perl be coaxed into calling an anonymous sub as if the sub were in a package than it's own? For example:

package Foo; my $coderef = sub { $global }; $global = 'in Foo'; package Bar; $global = 'in Bar'; print &$coderef;
This code prints 'in Foo', but I would like a way of getting it to print 'in Bar'.

I don't want to go the route of defining code as a string for eval and then prepending a package line to the code string... things would be much cleaner if I could use anonymous sub refs instead.

2. Callback routines for this module should be defined as anonymous subs, ideally mimmicking the way map, grep, and sort work (except, of course, that a sub would be needed before the code block). The user of the module should not have to include code to set a bunch of parameters from @_ when they write their callbacks, as I want callbacks to be lean and simple. Ideally, I want parameters pre-prepared in a "throw-away" package that their subs would get called in (as above), but if that won't work, I'm considering the possibility of callbacks getting their parameters prepared in a hash referenced by a localized $_ (for example $$_{name}). Are there any pitfalls or drawbacks to doing things this way. Which would you consider the most "perlish" way to accomplish my goals?

Thanks monks!


In reply to Callback Design by MeowChow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-25 11:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found