Created
April 4, 2021 11:03
-
-
Save neilb/cf9344c61cd3cc5c009a86c749ba3cea to your computer and use it in GitHub Desktop.
Another experimental module playing around with optional loading of other modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package optionally; | |
# don't use this | |
# | |
# this was an experiment in loading modules optionally, so you can write: | |
# | |
# use optionally; | |
# optionally use Module::Foo 1.23 qw/ function1 function2 /; | |
# | |
use strict; | |
use warnings; | |
use Keyword::Simple (); | |
use Carp qw/ croak /; | |
use Module::Runtime qw/ require_module /; | |
use Class::Unload (); | |
sub import | |
{ | |
my $importing_package = caller; | |
Keyword::Simple::define('optionally' => sub { | |
my $line = shift; | |
my ($matched, $action, $module, $rest) = ( $$line =~ m!\A(\s*(\S+)\s+(\S+)\s*(.*?);)! ) | |
or croak "syntax error near 'optionally'"; | |
substr($$line, 0, length($matched)) = ""; | |
eval "package $importing_package; $matched"; | |
}); | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment