Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

universal::require(3) [osx man page]

UNIVERSAL::require(3)					User Contributed Perl Documentation				     UNIVERSAL::require(3)

NAME
UNIVERSAL::require - require() modules from a variable SYNOPSIS
# This only needs to be said once in your program. require UNIVERSAL::require; # Same as "require Some::Module" my $module = 'Some::Module'; $module->require or die $@; # Same as "use Some::Module" BEGIN { $module->use or die $@ } DESCRIPTION
If you've ever had to do this... eval "require $module"; to get around the bareword caveats on require(), this module is for you. It creates a universal require() class method that will work with every Perl module and its secure. So instead of doing some arcane eval() work, you can do this: $module->require; It doesn't save you much typing, but it'll make alot more sense to someone who's not a ninth level Perl acolyte. Methods require my $return_val = $module->require or die $@; my $return_val = $module->require($version) or die $@; This works exactly like Perl's require, except without the bareword restriction, and it doesn't die. Since require() is placed in the UNIVERSAL namespace, it will work on any module. You just have to use UNIVERSAL::require somewhere in your code. Should the module require fail, or not be a high enough $version, it will simply return false and not die. The error will be in $@ as well as $UNIVERSAL::require::ERROR. $module->require or die $@; use my $require_return = $module->use or die $@; my $require_return = $module->use(@imports) or die $@; Like "UNIVERSAL::require", this allows you to "use" a $module without having to eval to work around the bareword requirement. It returns the same as require. Should either the require or the import fail it will return false. The error will be in $@. If possible, call this inside a BEGIN block to emulate a normal "use" as closely as possible. BEGIN { $module->use } SECURITY NOTES
UNIVERSAL::require makes use of "eval STRING". In previous versions of UNIVERSAL::require it was discovered that one could craft a class name which would result in code being executed. This hole has been closed. The only variables now exposed to "eval STRING" are the caller's package, filename and line which are not tainted. UNIVERSAL::require is taint clean. COPYRIGHT
Copyright 2001, 2005 by Michael G Schwern <schwern@pobox.com>. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://www.perl.com/perl/misc/Artistic.html AUTHOR
Michael G Schwern <schwern@pobox.com> SEE ALSO
Module::Load, "require" in perlfunc, <http://dev.perl.org/rfc/253.pod> perl v5.16.2 2009-03-30 UNIVERSAL::require(3)

Check Out this Related Man Page

Module::Load(3pm)					 Perl Programmers Reference Guide					 Module::Load(3pm)

NAME
Module::Load - runtime require of both modules and files SYNOPSIS
use Module::Load; my $module = 'Data:Dumper'; load Data::Dumper; # loads that module load 'Data::Dumper'; # ditto load $module # tritto my $script = 'some/script.pl' load $script; load 'some/script.pl'; # use quotes because of punctuations load thing; # try 'thing' first, then 'thing.pm' load CGI, ':standard' # like 'use CGI qw[:standard]' DESCRIPTION
"load" eliminates the need to know whether you are trying to require either a file or a module. If you consult "perldoc -f require" you will see that "require" will behave differently when given a bareword or a string. In the case of a string, "require" assumes you are wanting to load a file. But in the case of a bareword, it assumes you mean a module. This gives nasty overhead when you are trying to dynamically require modules at runtime, since you will need to change the module notation ("Acme::Comment") to a file notation fitting the particular platform you are on. "load" eliminates the need for this overhead and will just DWYM. Rules "load" has the following rules to decide what it thinks you want: o If the argument has any characters in it other than those matching "w", ":" or "'", it must be a file o If the argument matches only "[w:']", it must be a module o If the argument matches only "w", it could either be a module or a file. We will try to find "file.pm" first in @INC and if that fails, we will try to find "file" in @INC. If both fail, we die with the respective error messages. Caveats Because of a bug in perl (#19213), at least in version 5.6.1, we have to hardcode the path separator for a require on Win32 to be "/", like on Unix rather than the Win32 "". Otherwise perl will not read its own %INC accurately double load files if they are required again, or in the worst case, core dump. "Module::Load" cannot do implicit imports, only explicit imports. (in other words, you always have to specify explicitly what you wish to import from a module, even if the functions are in that modules' @EXPORT) ACKNOWLEDGEMENTS
Thanks to Jonas B. Nielsen for making explicit imports work. BUG REPORTS
Please report bugs or other issues to <bug-module-load@rt.cpan.org<gt>. AUTHOR
This module by Jos Boumans <kane@cpan.org>. COPYRIGHT
This library is free software; you may redistribute and/or modify it under the same terms as Perl itself. perl v5.18.2 2013-11-04 Module::Load(3pm)
Man Page