Tinkering with a (safe) string use

I read Schwern’s post How (not) To Load a Module just as I was wanting to dynamically load different Module::Build subclasses for different OSes. It struck me just as odd as it seems to for everyone that use-ing a module from a string should be so hard.

In my spare time, I have been working on some use problems using Devel::Declare and it gives some intersting hope here. Preliminarily I am calling it UseX::Declare but hopefully someone will come up with something better. Basically it provides a function called use_from which acts like:

use UseX::Declare;
BEGIN {
  our $var = 'Net::FTP';
}
use_from $var;

Through the magic of Devel::Declare, the parser sees:

use UseX::Declare;
BEGIN {
  our $var = 'Net::FTP';
}
use_from(1); use Net::FTP;

The use_from(1); is no-op cruft that allows me to get around a limitation in Devel::Declare (or if not a limitation, then a failure in my understanding).

The upshot is that NO eval is needed (not even in the UseX::Declare module)! A string is stored, then that string is made to be a bareword. That’s it.

Does this seem good? Comments welcome!

P.S. Since I am not too set on a name, I don’t have a GitHub repo for it yet, however here it is as a gist.

P.P.S. Another feature I hope to add in a similar manner, is some better way to only use if something is installed.

Leave a comment

About Joel Berger

user-pic As I delve into the deeper Perl magic I like to share what I can.