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

Aaron Crane perl at
Sat May 31 02:12:11 BST 2008

Abigail writes:
> Another thing you can do is to create the subroutines in
> unconditionally (at import time), and write 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

    package Control;

    use strict;
    use warnings;

    sub import {
        my ($class, @args) = @_;
        # ... 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 **

More information about the mailing list