in reply to Re: How not to hardcode a package name?
in thread How not to hardcode a package name?

The package of a line of code is set when that line is compiled.

Furthermore, package is lexically scoped, so you can't just put the eval in a BEGIN.

You can do

my $p = 'Package'; my $c = 'print(__PACKAGE__);'; # Some code eval "package $a; $c";

Replies are listed 'Best First'.
Re^3: How not to hardcode a package name?
by JadeNB (Chaplain) on Aug 28, 2008 at 20:48 UTC
    Thanks! Does this:
    The package of a line of code is set when that line is compiled.

    Furthermore, package is lexically scoped, so you can't just put the eval in a BEGIN.

    mean that it can't be done, or just that an eval is not the way to do it? (If I understand your code snippet correctly, you're saying that I can build up all the code I want in that package into a single string and eval that—but I'm sure that's not Best Practice, to say the least.)

      Does this mean that it can't be done

      I showed one way of doing it. It could also be done using source filters. It might even be possible to do it after the fact by poking at the internals. And of course, you could patch your Perl to get the effect you want.

      Whether or not those methods are worthwhile depends on the situation.