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...

--
If you can't understand the incipit, then please check the IPB Campaign.

In reply to How not to hardcode a package name? by blazar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.