Now that eventually I could make it to work, I have an actual programming question. Fact is, giving a peek into (cherished module) autobox's docs, I concocted up the following tiny module:
# -*- Perl -*- use strict; use warnings; use 5.010; package Array::Extract; use base qw/autobox/; sub import { my $class = shift; $class->SUPER::import(ARRAY => 'Array::Extract::Work'); } package Array::Extract::Work; sub extract { my $aref=shift; my @out=@{$aref}[@_]; @$aref=@{$aref}[grep !($_ ~~ @_) => 0..$#$aref]; @out; } 1; __END__
Now, it's not to boast about it, which I've probably been doing already, the last few posts: but it exposes a situation I've already met and always found unsatisfactory: namely, not only do I hardcode the Array::Extract::Work (which is nothing but a "helper" package) name, but I even do it twice, once as a bareword and once as a string. Needless to say, I find this very inelegant in the first place, and possibly error prone in a more complex application.
One solution that sprang to mind for some fractions of a second is to use another one of my favourite techniques: "code in @INC." But thinking of it better, it would really be to shoot... well, I can't remember the idiomatic English phrase, but I guess you understood what I mean!
Another solution that springs to mind it to refactor the module like thus:
# -*- Perl -*- use strict; use warnings; use 5.010; package Array::Extract; use base qw/autobox/; my $pkgname; { package Array::Extract::Work; $pkgname = __PACKAGE__; sub extract { my $aref=shift; my @out=@{$aref}[@_]; @$aref=@{$aref}[grep !($_ ~~ @_) => 0..$#$aref]; @out; } } sub import { my $class = shift; $class->SUPER::import(ARRAY => $pkgname); } 1; __END__
Indeed, this does fit the bill. But I'm concerned about other possible problems: i.e. in this case "pieces" of the respective packages are sparse all over the module's code. And I wonder how things could grow, in a more realistic and complex case...
What would people do, anyway? At least those that share my concerns for elegance, that is...
In reply to How not to hardcode a package name? by blazar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |