/usr/share/perl5/Module/Runtime.pm is in libmodule-runtime-perl 0.011-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | =head1 NAME
Module::Runtime - runtime module handling
=head1 SYNOPSIS
use Module::Runtime qw(
$module_name_rx is_module_name check_module_name
module_notional_filename require_module
);
if($module_name =~ /\A$module_name_rx\z/o) { ...
if(is_module_name($module_name)) { ...
check_module_name($module_name);
$notional_filename = module_notional_filename($module_name);
require_module($module_name);
use Module::Runtime qw(use_module use_package_optimistically);
$bi = use_module("Math::BigInt", 1.31)->new("1_234");
$widget = use_package_optimistically("Local::Widget")->new;
use Module::Runtime qw(
$top_module_spec_rx $sub_module_spec_rx
is_module_spec check_module_spec
compose_module_name
);
if($spec =~ /\A$top_module_spec_rx\z/o) { ...
if($spec =~ /\A$sub_module_spec_rx\z/o) { ...
if(is_module_spec("Standard::Prefix", $spec)) { ...
check_module_spec("Standard::Prefix", $spec);
$module_name =
compose_module_name("Standard::Prefix", $spec);
=head1 DESCRIPTION
The functions exported by this module deal with runtime handling of Perl
modules, which are normally handled at compile time.
=cut
package Module::Runtime;
{ use 5.006; }
use warnings;
use strict;
use Params::Classify 0.000 qw(is_string);
our $VERSION = "0.011";
use parent "Exporter";
our @EXPORT_OK = qw(
$module_name_rx is_module_name is_valid_module_name check_module_name
module_notional_filename require_module
use_module use_package_optimistically
$top_module_spec_rx $sub_module_spec_rx
is_module_spec is_valid_module_spec check_module_spec
compose_module_name
);
=head1 REGULAR EXPRESSIONS
These regular expressions do not include any anchors, so to check
whether an entire string matches a syntax item you must supply the
anchors yourself.
=over
=item $module_name_rx
Matches a valid Perl module name in bareword syntax.
The rule for this, precisely, is: the string must
consist of one or more segments separated by C<::>; each segment must
consist of one or more identifier characters (alphanumerics plus "_");
the first character of the string must not be a digit. Thus "C<IO::File>",
"C<warnings>", and "C<foo::123::x_0>" are all valid module names, whereas
"C<IO::>" and "C<1foo::bar>" are not.
Only ASCII characters are permitted; Perl's handling of non-ASCII
characters in source code is inconsistent.
C<'> separators are not permitted.
=cut
our $module_name_rx = qr/[A-Z_a-z][0-9A-Z_a-z]*(?:::[0-9A-Z_a-z]+)*/;
=item $top_module_spec_rx
Matches a module specification for use with L</compose_module_name>,
where no prefix is being used.
=cut
my $qual_module_spec_rx =
qr#(?:/|::)[A-Z_a-z][0-9A-Z_a-z]*(?:(?:/|::)[0-9A-Z_a-z]+)*#;
my $unqual_top_module_spec_rx =
qr#[A-Z_a-z][0-9A-Z_a-z]*(?:(?:/|::)[0-9A-Z_a-z]+)*#;
our $top_module_spec_rx = qr/$qual_module_spec_rx|$unqual_top_module_spec_rx/o;
=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
sub require_module($) {
# Explicit scalar() here works around a Perl core bug, present
# in Perl 5.8 and 5.10, which allowed a require() in return
# position to pass a non-scalar context through to file scope
# of the required file. This breaks some modules. require()
# in any other position, where its op flags determine context
# statically, doesn't have this problem, because the op flags
# are forced to scalar.
return scalar(require(&module_notional_filename));
}
=back
=head2 Structured module use
=over
=item use_module(NAME[, VERSION])
This is essentially C<use> in runtime form, but without the importing
feature (which is fundamentally a compile-time thing). The I<NAME> is
handled just like in C<require_module> above: it must be a module name,
and the named module is loaded as if by the bareword form of C<require>.
If a I<VERSION> is specified, the C<VERSION> method of the loaded module is
called with the specified I<VERSION> as an argument. This normally serves to
ensure that the version loaded is at least the version required. This is
the same functionality provided by the I<VERSION> parameter of C<use>.
On success, the name of the module is returned. This is unlike
L</require_module>, and is done so that the entire call to L</use_module>
can be used as a class name to call a constructor, as in the example in
the synopsis.
=cut
sub use_module($;$) {
my($name, $version) = @_;
require_module($name);
if(defined $version) {
$name->VERSION($version);
}
return $name;
}
=item use_package_optimistically(NAME[, VERSION])
This is an analogue of L</use_module> for the situation where there is
uncertainty as to whether a package/class is defined in its own module
or by some other means. It attempts to arrange for the named package to
be available, either by loading a module or by doing nothing and hoping.
An attempt is made to load the named module (as if by the bareword form
of C<require>). If the module cannot be found then it is assumed that
the package was actually already loaded but wasn't detected correctly,
and no error is signalled. That's the optimistic bit.
This is mostly the same operation that is performed by the L<base> pragma
to ensure that the specified base classes are available. The behaviour
of L<base> was simplified in version 2.18, and this function changed
to match.
If a I<VERSION> is specified, the C<VERSION> method of the loaded package is
called with the specified I<VERSION> as an argument. This normally serves
to ensure that the version loaded is at least the version required.
On success, the name of the package is returned. These aspects of the
function work just like L</use_module>.
=cut
sub use_package_optimistically($;$) {
my($name, $version) = @_;
check_module_name($name);
eval { local $SIG{__DIE__}; require(module_notional_filename($name)); };
die $@ if $@ ne "" && $@ !~ /\A
Can't\ locate\ .+\ at
\ \Q@{[__FILE__]}\E\ line\ \Q@{[__LINE__-1]}\E
/xs;
$name->VERSION($version) if defined $version;
return $name;
}
=back
=head2 Module name composition
=over
=item is_module_spec(PREFIX, SPEC)
Returns a truth value indicating
whether I<SPEC> is valid input for L</compose_module_name>.
See below for what that entails. Whether a I<PREFIX> is supplied affects
the validity of I<SPEC>, but the exact value of the prefix is unimportant,
so this function treats I<PREFIX> as a truth value.
=cut
sub is_module_spec($$) {
my($prefix, $spec) = @_;
return is_string($spec) &&
$spec =~ ($prefix ? qr/\A$sub_module_spec_rx\z/o :
qr/\A$top_module_spec_rx\z/o);
}
=item is_valid_module_spec(PREFIX, SPEC)
Deprecated alias for L</is_module_spec>.
=cut
*is_valid_module_spec = \&is_module_spec;
=item check_module_spec(PREFIX, SPEC)
Check whether I<SPEC> is valid input for L</compose_module_name>.
Return normally if it is, or C<die> if it is not.
=cut
sub check_module_spec($$) {
unless(&is_module_spec) {
die +(is_string($_[1]) ? "`$_[1]'" : "argument").
" is not a module specification\n";
}
}
=item compose_module_name(PREFIX, SPEC)
This function is intended to make it more convenient for a user to specify
a Perl module name at runtime. Users have greater need for abbreviations
and context-sensitivity than programmers, and Perl module names get a
little unwieldy. I<SPEC> is what the user specifies, and this function
translates it into a module name in standard form, which it returns.
I<SPEC> has syntax approximately that of a standard module name: it
should consist of one or more name segments, each of which consists
of one or more identifier characters. However, C</> is permitted as a
separator, in addition to the standard C<::>. The two separators are
entirely interchangeable.
Additionally, if I<PREFIX> is not C<undef> then it must be a module
name in standard form, and it is prefixed to the user-specified name.
The user can inhibit the prefix addition by starting I<SPEC> with a
separator (either C</> or C<::>).
=cut
sub compose_module_name($$) {
my($prefix, $spec) = @_;
check_module_name($prefix) if defined $prefix;
&check_module_spec;
if($spec =~ s#\A(?:/|::)##) {
# OK
} else {
$spec = $prefix."::".$spec if defined $prefix;
}
$spec =~ s#/#::#g;
return $spec;
}
=back
=head1 SEE ALSO
L<base>,
L<perlfunc/require>,
L<perlfunc/use>
=head1 AUTHOR
Andrew Main (Zefram) <zefram@fysh.org>
=head1 COPYRIGHT
Copyright (C) 2004, 2006, 2007, 2009, 2010, 2011
Andrew Main (Zefram) <zefram@fysh.org>
=head1 LICENSE
This module is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1;
|