1UNIVERSAL::require(3) User Contributed Perl DocumentationUNIVERSAL::require(3)
2
3
4
6 UNIVERSAL::require - require() modules from a variable [deprecated]
7
9 # This only needs to be said once in your program.
10 require UNIVERSAL::require;
11
12 # Same as "require Some::Module"
13 my $module = 'Some::Module';
14 $module->require or die $@;
15
16 # Same as "use Some::Module"
17 BEGIN { $module->use or die $@ }
18
20 Before using this module, you should look at the alternatives, some of
21 which are listed in SEE ALSO below.
22
23 This module provides a safe mechanism for loading a module at runtime,
24 when you have the name of the module in a variable.
25
26 If you've ever had to do this...
27
28 eval "require $module";
29
30 to get around the bareword caveats on require(), this module is for
31 you. It creates a universal require() class method that will work with
32 every Perl module and its secure. So instead of doing some arcane
33 eval() work, you can do this:
34
35 $module->require;
36
37 It doesn't save you much typing, but it'll make a lot more sense to
38 someone who's not a ninth level Perl acolyte.
39
41 require
42
43 my $return_val = $module->require or die $@;
44 my $return_val = $module->require($version) or die $@;
45
46 This works exactly like Perl's require, except without the bareword
47 restriction, and it doesn't die. Since require() is placed in the
48 UNIVERSAL namespace, it will work on any module. You just have to use
49 UNIVERSAL::require somewhere in your code.
50
51 Should the module require fail, or not be a high enough $version, it
52 will simply return false and not die. The error will be in $@ as well
53 as $UNIVERSAL::require::ERROR.
54
55 $module->require or die $@;
56
57 use
58
59 my $require_return = $module->use or die $@;
60 my $require_return = $module->use(@imports) or die $@;
61
62 Like "UNIVERSAL::require", this allows you to "use" a $module without
63 having to eval to work around the bareword requirement. It returns the
64 same as require.
65
66 Should either the require or the import fail it will return false. The
67 error will be in $@.
68
69 If possible, call this inside a BEGIN block to emulate a normal "use"
70 as closely as possible.
71
72 BEGIN { $module->use }
73
75 UNIVERSAL::require makes use of "eval STRING". In previous versions of
76 UNIVERSAL::require it was discovered that one could craft a class name
77 which would result in code being executed. This hole has been closed.
78 The only variables now exposed to "eval STRING" are the caller's
79 package, filename and line which are not tainted.
80
81 UNIVERSAL::require is taint clean.
82
84 Copyright 2001, 2005 by Michael G Schwern <schwern@pobox.com>.
85
86 This program is free software; you can redistribute it and/or modify it
87 under the same terms as Perl itself.
88
89 See http://www.perl.com/perl/misc/Artistic.html
90
92 Michael G Schwern <schwern@pobox.com>
93
94 Now maintained by Neil Bowers (NEILB).
95
97 Module::Load provides functions for loading code, and importing
98 functions. It's actively maintained.
99
100 Module::Runtime provides a number of usesful functions for require'ing
101 and use'ing modules, and associated operations.
102
103 Mojo::Loader is a class loader and plugin framework. Module::Loader is
104 a stand-alone module that was inspired by "Mojo::Loader".
105
106 There are many other modules that may be of interest on CPAN. An old
107 review of some of them can be read at
108 <https://neilb.org/reviews/module-loading.html>.
109
110 "require" in perlfunc.
111
112
113
114perl v5.38.0 2023-07-21 UNIVERSAL::require(3)