Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

rjbs (4671)

  (email not shown publicly)
AOL IM: RicardoJBSignes (Add Buddy, Send Message)
Yahoo! ID: RicardoSignes (Add User, Send Message)

I'm a Perl coder living in Bethlehem, PA and working Philadelphia. I'm a philosopher and theologan by training, but I was shocked to learn upon my graduation that these skills don't have many associated careers. Now I write code.

Journal of rjbs (4671)

Tuesday December 05, 2006
01:42 PM

saving keystrokes in sub export lists

[ #31812 ]

I use Sub::Exporter a good bit. It makes my life easier by letting me generate nice, simple mixins. I think I just had a good idea for typing even less.

Right now, to export routine, you might write this:

package Toolkit;
use Sub::Exporter -setup => {
  exports => [ 'routine' ],

If you write a subclass of your Toolkit module, and it defines its own routine, that's what will be exported, because Sub::Exporter uses the universal can method to find routine.

If you want the routine sub to be generated at import time, based on args to the import call, you'd write something more like this.

package Toolkit;
use Sub::Exporter -setup => {
  exports => [ routine =>  \&_gen_routine ],

Now when someone imports routine, it's generated by calling the _gen_routine sub, which is passed these args:

$class - the class on which import was called
$name  - the name of the routine being generated
$arg   - arguments passed to the generator in the import line
$col   - arguments passed to the whole importer in the import line

The problem is that since you've given the exporter a reference to a specific routine, you can't just replace the generator in a subclass. The way you'd do that, now, is like this:

package Toolkit;
use Sub::Exporter -setup => {
  exports => [ routine =>  sub { shift->_gen_routine(@_) } ],

Well, it works, but who wants to write that over and over? I could make a Sub::Exporter::Util for it, but that's a pain to type, too.

Just now, a vision of popped into my head (blinding me briefly; fortunately, I was not crossing the road), and I realized that I could easily make this work:

package Toolkit;
use Sub::Exporter -setup => {
  exports => [ routine =>  \'_gen_routine' ],

Now I can easily write subclasses of routine factories! Time to go mix some stuff in.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.