Sub::Install has a nice interface

I didn’t mention Sub::Install in the “Dynamic Subroutines” (or maybe the Symbol Table chapter. It was worth a mention (but only that). I show readers how to define subroutines in other packages (or even the current one, I guess) by assigning to a type glob:


{
no strict 'refs';
*{ $package . "::$name" } = sub { ... };
}

To do this, though, I have to violate strict‘s prohibition on symbolic references and I have to construct the fully-qualified package subroutine myself. After that, I have to use the typeglob, which isn’t common; it’s not in Learning Perl or Intermediate Perl. Those can be tricky for the new Perler.

Sub::Install gets around both of those with an interface that avoids the tricky, uncommon syntax:

use Sub::Install;

Sub::Install::install_sub({
    code => sub { ... },
    into  => $package,
    as     => $name,
    });

Most of the code in that module is just the infrastructure to make the module work, but it’s otherwise very simple.

I can understand why people would want to use the module: it makes things slightly easier to read to the novice programmer. I would weigh that against creating another dependency. In this case, there are no non-core dependencies, so its not so onerous. Other modules that give simplified interfaces aren’t so lean.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>