From: Graham Knop Date: Fri, 6 Dec 2013 09:43:01 +0000 (-0500) Subject: allow specifying import options as hash, including version check X-Git-Tag: v1.002000~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit%2FImport-Into.git;a=commitdiff_plain;h=568eef3eaba8968cd37b29f669648c302bf7ac60 allow specifying import options as hash, including version check --- diff --git a/lib/Import/Into.pm b/lib/Import/Into.pm index 74b7358..11040cc 100644 --- a/lib/Import/Into.pm +++ b/lib/Import/Into.pm @@ -7,15 +7,25 @@ our $VERSION = '1.001001'; # 1.1.1 sub _prelude { my $target = shift; - my ($package, $file, $line) - = $target =~ /[^0-9]/ ? ($target) : caller($target + 2); + my ($package, $file, $line, $level) + = ref $target ? @{$target}{qw(package filename line)} + : $target =~ /[^0-9]/ ? ($target) + : (undef, undef, undef, $target); + if (defined $level) { + my ($p, $fn, $ln) = caller($level + 2); + $package ||= $p; + $file ||= $fn; + $line ||= $ln; + } qq{package $package;\n} . ($file ? "#line $line \"$file\"\n" : '') } sub _make_action { my ($action, $target) = @_; - eval _prelude($target).qq{sub { shift->$action(\@_) }} + my $version = ref $target && $target->{version}; + my $ver_check = $version ? '$_[0]->VERSION($version);' : ''; + eval _prelude($target).qq{sub { $ver_check shift->$action(\@_) }} or die "Failed to build action sub to ${action} for ${target}: $@"; } diff --git a/t/import_into.t b/t/import_into.t index 6bb30ad..4fcf9ca 100644 --- a/t/import_into.t +++ b/t/import_into.t @@ -21,12 +21,14 @@ BEGIN { warnings->import::into($target); MyExporter->import::into($target, 'thing'); CheckFile->import::into(1); + } $INC{"MultiExporter.pm"} = 1; } my @checkcaller; +my $checkversion; BEGIN { package CheckFile; @@ -34,6 +36,9 @@ BEGIN { sub import { @checkcaller = caller; } + sub VERSION { + $checkversion = $_[1]; + } $INC{"CheckFile.pm"} = 1; } @@ -66,3 +71,15 @@ like($w[0], qr/uninitialized/, 'Correct warning'); is $checkcaller[0], 'TestPackage', 'import by level has correct package'; is $checkcaller[1], __FILE__, 'import by level has correct file'; is $checkcaller[2], 1, 'import by level has correct line'; + +CheckFile->import::into({ + package => 'ExplicitPackage', + filename => 'explicit-file.pl', + line => 42, + version => 219, +}); + +is $checkcaller[0], 'ExplicitPackage', 'import with hash has correct package'; +is $checkcaller[1], 'explicit-file.pl', 'import with hash has correct file'; +is $checkcaller[2], 42, 'import with hash has correct line'; +is $checkversion, 219, 'import with hash has correct version';