Alt-Module-Runtime-ButEUMM
view release on metacpan or search on metacpan
lib/Module/Runtime.pm view on Meta::CPAN
=item $sub_module_spec_rx
Matches a module specification for use with L</compose_module_name>,
where a prefix is being used.
=cut
my $unqual_sub_module_spec_rx = qr#[0-9A-Z_a-z]+(?:(?:/|::)[0-9A-Z_a-z]+)*#;
our $sub_module_spec_rx = qr/$qual_module_spec_rx|$unqual_sub_module_spec_rx/o;
=back
=head1 FUNCTIONS
=head2 Basic module handling
=over
=item is_module_name(ARG)
Returns a truth value indicating whether I<ARG> is a plain string
satisfying Perl module name syntax as described for L</$module_name_rx>.
=cut
sub is_module_name($) { _is_string($_[0]) && $_[0] =~ /\A$module_name_rx\z/o }
=item is_valid_module_name(ARG)
Deprecated alias for L</is_module_name>.
=cut
*is_valid_module_name = \&is_module_name;
=item check_module_name(ARG)
Check whether I<ARG> is a plain string
satisfying Perl module name syntax as described for L</$module_name_rx>.
Return normally if it is, or C<die> if it is not.
=cut
sub check_module_name($) {
unless(&is_module_name) {
die +(_is_string($_[0]) ? "`$_[0]'" : "argument").
" is not a module name\n";
}
}
=item module_notional_filename(NAME)
Generates a notional relative filename for a module, which is used in
some Perl core interfaces.
The I<NAME> is a string, which should be a valid module name (one or
more C<::>-separated segments). If it is not a valid name, the function
C<die>s.
The notional filename for the named module is generated and returned.
This filename is always in Unix style, with C</> directory separators
and a C<.pm> suffix. This kind of filename can be used as an argument to
C<require>, and is the key that appears in C<%INC> to identify a module,
regardless of actual local filename syntax.
=cut
sub module_notional_filename($) {
&check_module_name;
my($name) = @_;
$name =~ s!::!/!g;
return $name.".pm";
}
=item require_module(NAME)
This is essentially the bareword form of C<require>, in runtime form.
The I<NAME> is a string, which should be a valid module name (one or
more C<::>-separated segments). If it is not a valid name, the function
C<die>s.
The module specified by I<NAME> is loaded, if it hasn't been already,
in the manner of the bareword form of C<require>. That means that a
search through C<@INC> is performed, and a byte-compiled form of the
module will be used if available.
The return value is as for C<require>. That is, it is the value returned
by the module itself if the module is loaded anew, or C<1> if the module
was already loaded.
=cut
# Don't "use constant" here, to avoid dependencies.
BEGIN {
*_WORK_AROUND_HINT_LEAKAGE =
"$]" < 5.011 && !("$]" >= 5.009004 && "$]" < 5.010001)
? sub(){1} : sub(){0};
*_WORK_AROUND_BROKEN_MODULE_STATE = "$]" < 5.009 ? sub(){1} : sub(){0};
}
BEGIN { if(_WORK_AROUND_BROKEN_MODULE_STATE) { eval q{
sub Module::Runtime::__GUARD__::DESTROY {
delete $INC{$_[0]->[0]} if @{$_[0]};
}
1;
}; die $@ if $@ ne ""; } }
sub require_module($) {
# Localise %^H to work around [perl #68590], where the bug exists
# and this is a satisfactory workaround. The bug consists of
# %^H state leaking into each required module, polluting the
# module's lexical state.
local %^H if _WORK_AROUND_HINT_LEAKAGE;
if(_WORK_AROUND_BROKEN_MODULE_STATE) {
my $notional_filename = &module_notional_filename;
my $guard = bless([ $notional_filename ],
"Module::Runtime::__GUARD__");
my $result = CORE::require($notional_filename);
pop @$guard;
return $result;
} else {
( run in 0.492 second using v1.01-cache-2.11-cpan-39bf76dae61 )