Skip to content

Instantly share code, notes, and snippets.

@schwern
Created February 25, 2010 05:58
Show Gist options
  • Save schwern/314272 to your computer and use it in GitHub Desktop.
Save schwern/314272 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -w
use strict;
use warnings;
use Benchmark qw(cmpthese);
BEGIN {
*CORE::GLOBAL::require = sub {
return CORE::require $_[0];
};
}
my $eval_require = sub {
my $ret = eval { CORE::require($_[0]) };
if( !$ret ) {
if( my($mod) = $@ =~ /\ACan't locate (\S+)/ ) {
$mod =~ s{/}{::}g;
$mod =~ s{\.pm$}{};
$@ = <<"END";
Can't locate $mod in your Perl library. You may need to install it
from CPAN or another repository. Your library paths are:
END
$@ .= " $_\n" for @INC;
}
die $@;
}
return $ret;
};
my $require_die_handler = sub {
if( my($mod) = $_[0] =~ /\ACan't locate (\S+)/ ) {
$mod =~ s{/}{::}g;
$mod =~ s{\.pm$}{};
$_[0] = <<"END";
Can't locate $mod in your Perl library. You may need to install it
from CPAN or another repository. Your library paths are:
END
$_[0] .= " $_\n" for @INC;
}
die @_;
};
my $sigdie_require = sub {
local $SIG{__DIE__} = $require_die_handler;
return CORE::require($_[0]);
};
my $module = "Text/Soundex.pm";
require $module;
cmpthese(shift || -3, {
eval_require => sub {
$eval_require->($module);
},
sigdie_require => sub {
$sigdie_require->($module);
},
core_require => sub {
CORE::require($module);
},
core_global_require => sub {
require($module);
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment