GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

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

UNIVERSAL::require - require() modules from a variable [deprecated]

  # 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 $@ }

Before using this module, you should look at the alternatives, some of which are listed in SEE ALSO below.

This module provides a safe mechanism for loading a module at runtime, when you have the name of the module in a variable.

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 a lot more sense to someone who's not a ninth level Perl acolyte.

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 }

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 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

Michael G Schwern <schwern@pobox.com>

Now maintained by Neil Bowers (NEILB).

Module::Load provides functions for loading code, and importing functions. It's actively maintained.

Module::Runtime provides a number of usesful functions for require'ing and use'ing modules, and associated operations.

Mojo::Loader is a class loader and plugin framework. Module::Loader is a stand-alone module that was inspired by "Mojo::Loader".

There are many other modules that may be of interest on CPAN. An old review of some of them can be read at <https://neilb.org/reviews/module-loading.html>.

"require" in perlfunc.

2021-03-27 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.