Hmm. Let me see if I understand: you want to be able
to do something like grep or map, where your code gets
passed a subref which you then call back?
If so, you don't need to worry about changing packages
at the time you call it, since it will have already
picked up the proper package when it was defined (and
compiled) in the caller's code:
package my;
$global = 'in_my';
sub own_map(&@) {
my $code = shift;
my @return = ();
push @return, $code->() for @_;
@return;
}
package main;
$global = 'in_main';
print my::own_map { $_, " ", $global, "\n" } (1..20);
# prints "1 in_main\n", "2 in_main\n" and so on
In the above code, my::own_map could have molested
${ (caller)[0] . "::global" } to pass a value
in to the block. I guess this might be OK (although it's
clearly an abuse of caller, which AFAIK was intended
for debugging), as long as it's well documented and you
are careful to localize everything.
Updated: removed a confusing and unneeded $_ argument
on the line that calls the callback. Oops!
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.