UNIVERSAL-require-0.17/000755 000765 000024 00000000000 12324337454 014726 5ustar00neilbstaff000000 000000 UNIVERSAL-require-0.17/Changes000644 000765 000024 00000003511 12324333470 016213 0ustar00neilbstaff000000 000000 Revision history for Perl module UNIVERSAL::require 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.17/lib/000755 000765 000024 00000000000 12324337454 015474 5ustar00neilbstaff000000 000000 UNIVERSAL-require-0.17/Makefile.PL000644 000765 000024 00000002425 12324324012 016665 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, }, ($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.17/MANIFEST000644 000765 000024 00000000443 12324337454 016060 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.17/META.json000644 000765 000024 00000002410 12324337454 016344 0ustar00neilbstaff000000 000000 { "abstract" : "require() modules from a variable", "author" : [ "Michael G Schwern " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 6.94, CPAN::Meta::Converter version 2.133380", "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" } } }, "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.17" } UNIVERSAL-require-0.17/META.yml000644 000765 000024 00000001241 12324337454 016175 0ustar00neilbstaff000000 000000 --- abstract: 'require() modules from a variable' author: - 'Michael G Schwern ' build_requires: ExtUtils::MakeMaker: 0 configure_requires: ExtUtils::MakeMaker: 0 dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 6.94, CPAN::Meta::Converter version 2.133380' 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 resources: license: http://dev.perl.org/licenses/ repository: git://github.com/neilbowers/UNIVERSAL-require.git version: 0.17 UNIVERSAL-require-0.17/README000644 000765 000024 00000001152 12314124321 015567 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. 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.17/t/000755 000765 000024 00000000000 12324337454 015171 5ustar00neilbstaff000000 000000 UNIVERSAL-require-0.17/t/Dummy.pm000644 000765 000024 00000000306 12116450325 016611 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; UNIVERSAL-require-0.17/t/require.t000644 000765 000024 00000003174 12324327077 017037 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.17/t/taint.t000644 000765 000024 00000000341 12324333333 016463 0ustar00neilbstaff000000 000000 #!/usr/bin/perl -Tw use strict; use Test::More 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.17/t/use.t000644 000765 000024 00000000654 12116450325 016147 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.17/lib/UNIVERSAL/000755 000765 000024 00000000000 12324337454 017044 5ustar00neilbstaff000000 000000 UNIVERSAL-require-0.17/lib/UNIVERSAL/require.pm000644 000765 000024 00000011351 12324326223 021047 0ustar00neilbstaff000000 000000 package UNIVERSAL::require; $UNIVERSAL::require::VERSION = '0.17'; # 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]+)*/; use vars qw($Level); $Level = 0; =pod =head1 NAME UNIVERSAL::require - require() modules from a variable =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 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, L, L =cut 1;