Eric Wilhelm writes:
> use Package::Unicorn sub {
> package BlahBlahBlah;
> sub thing {foo->stuff()};
> __PACKAGE__;
> };
> I've tried to come up with something that doesn't need the trailing
> __PACKAGE__ or require typing BlahBlahBlah twice, but can't think of
> anything that doesn't involve DB or B::*.
Hmm. Tricky.
$Data::Dumper::Deparse sounds like the obvious approach, but it uses
B::Deparse under the hood.
This kind-of works:
# Package::Unicorn->import glues a package declaration onto the front
# of the string, then evals the whole thing:
use Package::Unicorn BlahBlahBlah => q{
sub thing {foo->stuff()};
};
But it has the disadvantage of forcing you to stuff the code into
a string, so it makes life hard if you want unmatched curlies in
literals or comments (for suitable values of "unmatched curlies" if you
choose a different delimiter), and it'll definitely make it awkward to
use backslashes in literals, or to write regexes that match literal
backslashes. At least the pseudo-package is still compiled at compile
time with respect to the code that does the C<use Package::Unicorn>,
though.
How about this as a different way of accomplishing your goals? It's not
trying to do anything clever, but it should be simple and robust:
{
package BlahBlahBlah;
# Package::Unicorn->import looks at C<caller> to work out what's
# trying to do this, and fiddles with C<%INC> as desired
use Package::Unicorn;
sub thing {foo->stuff()}
}
--
Aaron Crane