1UNIVERSAL::require(3) User Contributed Perl DocumentationUNIVERSAL::require(3)
2
3
4
6 UNIVERSAL::require - require() modules from a variable
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 If you've ever had to do this...
21
22 eval "require $module";
23
24 to get around the bareword caveats on require(), this module is for
25 you. It creates a universal require() class method that will work with
26 every Perl module and its secure. So instead of doing some arcane
27 eval() work, you can do this:
28
29 $module->require;
30
31 It doesn't save you much typing, but it'll make a lot more sense to
32 someone who's not a ninth level Perl acolyte.
33
35 require
36
37 my $return_val = $module->require or die $@;
38 my $return_val = $module->require($version) or die $@;
39
40 This works exactly like Perl's require, except without the bareword
41 restriction, and it doesn't die. Since require() is placed in the
42 UNIVERSAL namespace, it will work on any module. You just have to use
43 UNIVERSAL::require somewhere in your code.
44
45 Should the module require fail, or not be a high enough $version, it
46 will simply return false and not die. The error will be in $@ as well
47 as $UNIVERSAL::require::ERROR.
48
49 $module->require or die $@;
50
51 use
52
53 my $require_return = $module->use or die $@;
54 my $require_return = $module->use(@imports) or die $@;
55
56 Like "UNIVERSAL::require", this allows you to "use" a $module without
57 having to eval to work around the bareword requirement. It returns the
58 same as require.
59
60 Should either the require or the import fail it will return false. The
61 error will be in $@.
62
63 If possible, call this inside a BEGIN block to emulate a normal "use"
64 as closely as possible.
65
66 BEGIN { $module->use }
67
69 UNIVERSAL::require makes use of "eval STRING". In previous versions of
70 UNIVERSAL::require it was discovered that one could craft a class name
71 which would result in code being executed. This hole has been closed.
72 The only variables now exposed to "eval STRING" are the caller's
73 package, filename and line which are not tainted.
74
75 UNIVERSAL::require is taint clean.
76
78 Copyright 2001, 2005 by Michael G Schwern <schwern@pobox.com>.
79
80 This program is free software; you can redistribute it and/or modify it
81 under the same terms as Perl itself.
82
83 See http://www.perl.com/perl/misc/Artistic.html
84
86 Michael G Schwern <schwern@pobox.com>
87
88 Now maintained by Neil Bowers (NEILB).
89
91 Module::Load, "require" in perlfunc, <http://dev.perl.org/rfc/253.pod>
92
93
94
95perl v5.32.0 2020-07-28 UNIVERSAL::require(3)