From 1d2b7ec55763d41a18a61d1b44aedd531d305ad3 Mon Sep 17 00:00:00 2001 From: Nicholas Clark Date: Thu, 14 Oct 2010 21:30:16 +0100 Subject: [PATCH] XSLoader::load() with no arguments can use caller to find a default package. In the case of dynamic linking, it's already using caller to get a filename, so this isn't usually any extra work. --- dist/XSLoader/XSLoader_pm.PL | 30 +++++++++++++++++------------- dist/XSLoader/t/XSLoader.t | 28 +++++++++++++++------------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/dist/XSLoader/XSLoader_pm.PL b/dist/XSLoader/XSLoader_pm.PL index 66afa8e..0738fa5 100644 --- a/dist/XSLoader/XSLoader_pm.PL +++ b/dist/XSLoader/XSLoader_pm.PL @@ -8,7 +8,7 @@ print OUT <<'EOT'; package XSLoader; -$VERSION = "0.12"; +$VERSION = "0.13"; #use strict; @@ -26,9 +26,13 @@ package XSLoader; sub load { package DynaLoader; - die q{XSLoader::load('Your::Module', $Your::Module::VERSION)} unless @_; + my ($module, $modlibname) = caller(); - my($module) = $_[0]; + if (@_) { + $module = $_[0]; + } else { + $_[0] = $module; + } # work with static linking too my $boots = "$module\::bootstrap"; @@ -58,7 +62,6 @@ EOT print OUT <<'EOT'; my $modpname = join('/',@modparts); - my $modlibname = (caller())[1]; my $c = @modparts; $modlibname =~ s,[\\/][^\\/]+$,, while $c--; # Q&D basename EOT @@ -178,14 +181,14 @@ XSLoader - Dynamically load C libraries into Perl code =head1 VERSION -Version 0.10 +Version 0.13 =head1 SYNOPSIS package YourPackage; - use XSLoader; + require XSLoader; - XSLoader::load 'YourPackage', $YourPackage::VERSION; + XSLoader::load(); =head1 DESCRIPTION @@ -234,6 +237,13 @@ If no C<$VERSION> was specified on the C line, the last line becomes XSLoader::load 'YourPackage'; +If the call to C is from the YourPackage, then that can be further +simplified to + + XSLoader::load(); + +as C will use C to determine the package. + =head2 Backward compatible boilerplate If you want to have your cake and eat it too, you need a more complicated @@ -367,14 +377,8 @@ B<(W)> As the message says, some symbols stay undefined although the extension module was correctly loaded and initialised. The list of undefined symbols follows. -=item C - -B<(F)> You tried to invoke C without any argument. You must supply -a module name, and optionally its version. - =back - =head1 LIMITATIONS To reduce the overhead as much as possible, only one possible location diff --git a/dist/XSLoader/t/XSLoader.t b/dist/XSLoader/t/XSLoader.t index 211c4d8..0cac1f2 100644 --- a/dist/XSLoader/t/XSLoader.t +++ b/dist/XSLoader/t/XSLoader.t @@ -30,7 +30,7 @@ my %modules = ( 'Time::HiRes'=> q| ::can_ok( 'Time::HiRes' => 'usleep' ) |, # 5.7.3 ); -plan tests => keys(%modules) * 3 + 5; +plan tests => keys(%modules) * 3 + 7; # Try to load the module use_ok( 'XSLoader' ); @@ -40,18 +40,20 @@ can_ok( 'XSLoader' => 'load' ); can_ok( 'XSLoader' => 'bootstrap_inherit' ); # Check error messages -eval { XSLoader::load() }; -like( $@, '/^XSLoader::load\(\'Your::Module\', \$Your::Module::VERSION\)/', - "calling XSLoader::load() with no argument" ); - -eval q{ package Thwack; XSLoader::load('Thwack'); }; -if ($Config{usedl}) { - like( $@, q{/^Can't locate loadable object for module Thwack in @INC/}, - "calling XSLoader::load() under a package with no XS part" ); -} -else { - like( $@, q{/^Can't load module Thwack, dynamic loading not available in this perl./}, - "calling XSLoader::load() under a package with no XS part" ); +foreach (['Thwack', 'package Thwack; XSLoader::load(); 1'], + ['Zlott', 'package Thwack; XSLoader::load("Zlott"); 1'], + ) { + my ($should_load, $codestr) = @$_; + is(eval $codestr, undef, "eval '$codestr' should die"); + + if ($Config{usedl}) { + like( $@, qr/^Can't locate loadable object for module $should_load in \@INC/, + "calling XSLoader::load() under a package with no XS part" ); + } + else { + like( $@, qr/^Can't load module $should_load, dynamic loading not available in this perl./, + "calling XSLoader::load() under a package with no XS part" ); + } } # Now try to load well known XS modules -- 2.7.4