UNIVERSAL-require-0.19/000755 000765 000024 00000000000 14027652205 014724 5ustar00neilbstaff000000 000000 UNIVERSAL-require-0.19/Changes000644 000765 000024 00000004166 14027652016 016226 0ustar00neilbstaff000000 000000 Revision history for Perl module UNIVERSAL::require 0.19 2021-03-27 NEILB - Noted that the module is now deprecated - Added a list of alternatives to SEE ALSO 0.18 2015-02-24 - Skip the taint test if Perl was compiled without taint support. RAZ++ - Changed use of "use vars" to "our" - Added strict and warnings to PREREQ_PM 0.17 2014-04-19 - Check for valid module names. RT#94866 from TOBYINK. - Changed used of die() to croak() RT#23113 - Fixed typo in pod (fschlich++) 0.16 2014-02-03 - All seems fine with previous dev release 0.15_01 2014-02-03 - Added README - Specified min version of perl 5.6.0 - Now "use warnings" 0.15 2013-09-28 - Hadn't specified meta-spec version 2, so github repo wasn't turning up in the metadata. 0.14 2013-08-25 - Changed the repository meta_merge to the new format - Tweaked format (mainly release dates) to conform to CPAN::Changes::Spec 0.13_1 2013-03-08 - first test release by NEILB after getting comaint - no changes other than specifying github repository 0.13 2009-03-30 - $module->require now always resets $@ which removes a trap of using $@ to indicate an error, rather than checking $module->require directly. [rt.cpan.org 44444] 0.12 2009-03-30 - bleadperl introduced a new warning deprecating "use UNIVERSAL" (thank goodness) which interfered with t/require.t 0.11 2006-11-11 - Sped up require() by about 400% for already loaded modules. [rt.cpan.org 21141] - Fix the test for the changed version error in 5.10. 0.10 2005-10-10 - Split out of UNIVERSAL-exports into its own distribution. - UNIVERSAL::require no longer uses eval STRING in require(). This closes a security hole. - Testing that it works under taint mode. - Added license and copyright notice. - Added use() - Mention Module::Load in SEE ALSO. 0.03 2001-12-16 - Fixed a little nit when "use UNIVERSAL" is involved. 0.02 2001-06-25 - -->API CHANGE!<-- require() no longer dies on failure 0.01 2001-01-22 - First version, adapted from the Perl 6 RFC prototypes 253 and 257. UNIVERSAL-require-0.19/MANIFEST000644 000765 000024 00000000443 14027652205 016056 0ustar00neilbstaff000000 000000 Changes lib/UNIVERSAL/require.pm Makefile.PL MANIFEST This list of files README t/Dummy.pm t/require.t t/taint.t t/use.t META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) UNIVERSAL-require-0.19/t/000755 000765 000024 00000000000 14027652205 015167 5ustar00neilbstaff000000 000000 UNIVERSAL-require-0.19/README000644 000765 000024 00000001305 14027652061 015603 0ustar00neilbstaff000000 000000 README for Perl module UNIVERSAL::require This module lets you require other modules where the module name is in a variable, something you can't do with the 'require' built-in. There are better alternatives now, so you should start by looking at the list in SEE ALSO. You can read a nicely formatted version of the documentation for this module online: https://metacpan.org/pod/UNIVERSAL::require You should be able to install this using your usual method for installing modules from CPAN. If you don't have a usual method yet, have a look at: http://www.cpan.org/modules/INSTALL.html This module was written by Michael G Schwern. It is now being maintained by Neil Bowers UNIVERSAL-require-0.19/META.yml000644 000765 000024 00000001424 14027652205 016176 0ustar00neilbstaff000000 000000 --- abstract: 'require() modules from a variable [deprecated]' author: - 'Michael G Schwern ' build_requires: ExtUtils::MakeMaker: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 7.34, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: UNIVERSAL-require no_index: directory: - t - inc package: - UNIVERSAL requires: Carp: '0' Test::More: '0.47' perl: '5.006' strict: '0' warnings: '0' resources: license: http://dev.perl.org/licenses/ repository: git://github.com/neilbowers/UNIVERSAL-require.git version: '0.19' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' UNIVERSAL-require-0.19/lib/000755 000765 000024 00000000000 14027652205 015472 5ustar00neilbstaff000000 000000 UNIVERSAL-require-0.19/Makefile.PL000644 000765 000024 00000002513 12473136707 016706 0ustar00neilbstaff000000 000000 #!/usr/bin/perl -w use ExtUtils::MakeMaker; my $name = 'UNIVERSAL::require'; my $version_from = "lib/$name.pm"; $version_from =~ s{::}{/}g; my $mm_ver = $ExtUtils::MakeMaker::VERSION; if ($mm_ver =~ /_/) { # dev version $mm_ver = eval $mm_ver; die $@ if $@; } WriteMakefile( NAME => 'UNIVERSAL::require', VERSION_FROM => $version_from, ABSTRACT_FROM => $version_from, AUTHOR => 'Michael G Schwern ', PREREQ_PM => { 'Test::More' => 0.47, 'Carp' => 0, 'strict' => 0, 'warnings' => 0, }, ($mm_ver >= 6.31 ? (LICENSE => 'perl') : ()), ($mm_ver >= 6.48 ? (MIN_PERL_VERSION => 5.006) : () ), ($mm_ver <= 6.45 ? () : (META_MERGE => { 'meta-spec' => { version => 2 }, resources => { license => 'http://dev.perl.org/licenses/', bugtracker => 'http://rt.cpan.org/Public/Dist/Display.html?Name=UNIVERSAL-require', repository => { type => 'git', web => 'https://github.com/neilbowers/UNIVERSAL-require', url => 'git://github.com/neilbowers/UNIVERSAL-require.git', }, }, no_index => { package => ["UNIVERSAL"] }, })) ); UNIVERSAL-require-0.19/META.json000644 000765 000024 00000002610 14027652205 016344 0ustar00neilbstaff000000 000000 { "abstract" : "require() modules from a variable [deprecated]", "author" : [ "Michael G Schwern " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 7.34, CPAN::Meta::Converter version 2.150010", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "UNIVERSAL-require", "no_index" : { "directory" : [ "t", "inc" ], "package" : [ "UNIVERSAL" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "Carp" : "0", "Test::More" : "0.47", "perl" : "5.006", "strict" : "0", "warnings" : "0" } } }, "release_status" : "stable", "resources" : { "license" : [ "http://dev.perl.org/licenses/" ], "repository" : { "type" : "git", "url" : "git://github.com/neilbowers/UNIVERSAL-require.git", "web" : "https://github.com/neilbowers/UNIVERSAL-require" } }, "version" : "0.19", "x_serialization_backend" : "JSON::PP version 2.97001" } UNIVERSAL-require-0.19/lib/UNIVERSAL/000755 000765 000024 00000000000 14027652205 017042 5ustar00neilbstaff000000 000000 UNIVERSAL-require-0.19/lib/UNIVERSAL/require.pm000644 000765 000024 00000012643 14027651750 021066 0ustar00neilbstaff000000 000000 package UNIVERSAL::require; $UNIVERSAL::require::VERSION = '0.19'; # We do this because UNIVERSAL.pm uses CORE::require(). We're going # to put our own require() into UNIVERSAL and that makes an ambiguity. # So we load it up beforehand to avoid that. BEGIN { require UNIVERSAL } package UNIVERSAL; use 5.006; use strict; use warnings; use Carp; # regexp for valid module name. Lifted from Module::Runtime my $module_name_rx = qr/[A-Z_a-z][0-9A-Z_a-z]*(?:::[0-9A-Z_a-z]+)*/; our $Level = 0; =pod =head1 NAME UNIVERSAL::require - require() modules from a variable [deprecated] =head1 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 $@ } =head1 DESCRIPTION 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. =head1 Methods =head3 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 B 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 B. The error will be in $@ as well as $UNIVERSAL::require::ERROR. $module->require or die $@; =cut sub require { my($module, $want_version) = @_; $UNIVERSAL::require::ERROR = ''; croak("UNIVERSAL::require() can only be run as a class method") if ref $module; croak("invalid module name '$module'") if $module !~ /\A$module_name_rx\z/; croak("UNIVERSAL::require() takes no or one arguments") if @_ > 2; my($call_package, $call_file, $call_line) = caller($Level); # Load the module. my $file = $module . '.pm'; $file =~ s{::}{/}g; # For performance reasons, check if its already been loaded. This makes # things about 4 times faster. # We use the eval { } to make sure $@ is not set. See RT #44444 for details return eval { 1 } if $INC{$file}; my $return = eval qq{ #line $call_line "$call_file" CORE::require(\$file); }; # Check for module load failure. if( !$return ) { $UNIVERSAL::require::ERROR = $@; return $return; } # Module version check. if( @_ == 2 ) { eval qq{ #line $call_line "$call_file" \$module->VERSION($want_version); 1; } or do { $UNIVERSAL::require::ERROR = $@; return 0; }; } return $return; } =head3 use my $require_return = $module->use or die $@; my $require_return = $module->use(@imports) or die $@; Like C, this allows you to C 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 C as closely as possible. BEGIN { $module->use } =cut sub use { my($module, @imports) = @_; local $Level = 1; my $return = $module->require or return 0; my($call_package, $call_file, $call_line) = caller; eval qq{ package $call_package; #line $call_line "$call_file" \$module->import(\@imports); 1; } or do { $UNIVERSAL::require::ERROR = $@; return 0; }; return $return; } =head1 SECURITY NOTES UNIVERSAL::require makes use of C. 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 C are the caller's package, filename and line which are not tainted. UNIVERSAL::require is taint clean. =head1 COPYRIGHT Copyright 2001, 2005 by Michael G Schwern Eschwern@pobox.comE. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See F =head1 AUTHOR Michael G Schwern Now maintained by Neil Bowers (NEILB). =head1 SEE ALSO L provides functions for loading code, and importing functions. It's actively maintained. L provides a number of usesful functions for require'ing and use'ing modules, and associated operations. L is a class loader and plugin framework. L is a stand-alone module that was inspired by C. There are many other modules that may be of interest on CPAN. An old review of some of them can be read at L. L. =cut 1; UNIVERSAL-require-0.19/t/require.t000644 000765 000024 00000003174 12324327077 017041 0ustar00neilbstaff000000 000000 #!/usr/bin/perl -Tw use Test::More tests => 15; use_ok "UNIVERSAL::require"; use lib qw(t); is( Dummy->require, 23, 'require()' ); is( $UNIVERSAL::require::ERROR, '', ' $ERROR empty' ); ok( $Dummy::VERSION, ' $VERSION ok' ); { $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /^Subroutine \w+ redefined/ }; delete $INC{'Dummy.pm'}; is( Dummy->require(0.4), 23, 'require($version)' ); is( $UNIVERSAL::require::ERROR, '', ' $ERROR empty' ); delete $INC{'Dummy.pm'}; ok( !Dummy->require(1.0), 'require($version) fail' ); like( $UNIVERSAL::require::ERROR, '/^Dummy version 1.* required--this is only version 0.5/' ); } { my $warning = ''; local $SIG{__WARN__} = sub { $warning = join '', @_ }; eval 'require UNIVERSAL'; is( $warning, '', 'loading UNIVERSAL doesnt interfere' ); } my $evil = "Dummy; Test::More::fail('this should never be called');"; eval { $evil->require }; ok($@ && $@ =~ /invalid module name/, "trying to add trailing code should fail early due to a bad module name"); # make sure $@ and ERROR are set appropriately { local $@; ok( !$@, '$@ unset' ); # do a failed eval a before we try to load Dummy again eval { die $$ }; like( $@, qr/$$/, ' $@ set to ' . $$ ); ok( Dummy->require, " ->require()" ); ok( !$@, ' $@ unset ' . $@ ); ok( !$UNIVERSAL::require::ERROR, ' $ERROR unset ' ); } UNIVERSAL-require-0.19/t/taint.t000644 000765 000024 00000000514 12473136321 016472 0ustar00neilbstaff000000 000000 #!/usr/bin/perl -Tw use strict; use Config; use Test::More $Config{ccflags} =~ /-DSILENT_NO_TAINT_SUPPORT/ ? ( skip_all => 'No taint support' ) : ( tests => 2 ); use UNIVERSAL::require; my $tainted = $0; $tainted =~ s/\A.*\z/bananas/; ok !eval { $tainted->require or die $@ }; like $@, '/^Insecure dependency in require /'; UNIVERSAL-require-0.19/t/use.t000644 000765 000024 00000000654 12116450325 016151 0ustar00neilbstaff000000 000000 #!/usr/bin/perl -Tw use Test::More tests => 10; use_ok "UNIVERSAL::require"; use lib qw(t); my $Filename = quotemeta $0; is( Dummy->use, 23 ); is( Dummy->use("foo", "bar"), 1 ); is( foo(), 42 ); is( bar(), 23 ); ok( !Dummy->use(1) ); is( $UNIVERSAL::require::ERROR, $@ ); #line 23 ok( !Dont::Exist->use ); like( $@, qq[/^Can't locate Dont/Exist.pm in .* at $Filename line 23\./] ); is( $UNIVERSAL::require::ERROR, $@ ); UNIVERSAL-require-0.19/t/Dummy.pm000644 000765 000024 00000000306 12116450325 016613 0ustar00neilbstaff000000 000000 package Dummy; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(foo); @EXPORT_OK = qw(bar); $VERSION = 0.5; sub foo { 42 } sub bar { 23 } sub car { "yarblockos" } return 23;