in reply to Re: How not to hardcode a package name?
in thread How not to hardcode a package name?
A little convention can go a long way:$class->SUPER::import(ARRAY => __PACKAGE__.'::Work');
I personally believe that it would still amount to "double hardcoding," albeit in a more "hyerarchical" (for what that it may mean in Perl 5) and thus clean way: what if I decide to change say "::Work" to "::Tool" later?
If you want to be more fancy/flexible, you could do it like this:
Well, this is just as ugly as working with symrefs generally is ;) and somehow unsatisfactory for leaving the same sub in two namespaces. (I presume that one could go just as far as deleting it from Array::Extract, but that would be kind of an exaggeration, I admit.) Or else I may actually adopt an anonymous sub to start with:
package Array::Extract; my $worker_package = __PACKAGE__ . '::Work'; # or whatever other name +you want { no strict 'refs'; *{"$worker_package\::extract"} = sub { ... your code ... }; }
And that would be nearly the same thing in most cases: that is, unless that sub creates a closure, which after all may be what I want anyway...
The only problem I see with this approach is that instead of having a single sub like that, I may have quite a lot of them. (In which case I would probably put them into a hash and generate their package names programmatically...)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: How not to hardcode a package name?
by Corion (Patriarch) on Aug 28, 2008 at 10:35 UTC |