Any way to "inject" an INIT block into a different module?

Aaron Crane perl at aaroncrane.co.uk
Sat May 31 02:12:11 BST 2008


Abigail writes:
> Another thing you can do is to create the subroutines in Control.pm
> unconditionally (at import time), and write Amp.pm like this:
> 
>     package Amp;
> 
>     use Control qw (volume sustain);
>     no warnings 'redefine';
> 
>     sub volume {
>         ...
>     }

You can avoid the `no warnings` in the caller if you do something like
this:

    package Control;

    use strict;
    use warnings;

    sub import {
        my ($class, @args) = @_;
        warnings->unimport(qw<redefine>);
        # ... and install @args into the caller ...
    }

(See also ex::caution, not to mention Moose, which is where I first
saw that trick.)

I'm not completely convinced I'd want to do that -- it seems somewhat
icky to silently fiddle with the caller's compilation environment
without a dirty great indication that that's what you're doing -- but
it's certainly possible.

-- 
Aaron Crane ** http://aaroncrane.co.uk/


More information about the london.pm mailing list