README
lib/subs/auto.pm
t/00-load.t
+t/05-args.t
t/10-base.t
+t/11-pkg.t
t/90-boilerplate.t
t/91-pod.t
t/92-pod-coverage.t
use strict;
use warnings;
+use Carp qw/croak/;
use Symbol qw/gensym/;
use Variable::Magic qw/wizard cast dispell getdata/;
=cut
BEGIN {
- if (!Variable::Magic::VMG_UVAR) {
- require Carp;
- Carp::croak('uvar magic not available');
- }
+ croak 'uvar magic not available' unless Variable::Magic::VMG_UVAR;
}
my @core = qw/abs accept alarm atan2 bind binmode bless break caller chdir
my %pkgs;
sub import {
- my $pkg = caller 1;
+ shift;
+ croak 'Optional arguments must be passed as keys/values pairs' if @_ % 2;
+ my %args = @_;
+ my $cur = (caller 1)[0];
+ my $pkg;
+ if ($pkg = $args{in}) {
+ croak 'Invalid package name' if ref $pkg
+ or $pkg =~ /(?:-|[^\w:])/
+ or $pkg =~ /(?:\A\d|\b:(?::\d|(?:::+)?\b))/;
+ $pkg =~ s/::$//;
+ $pkg = $cur . $pkg if $pkg eq '' or $pkg =~ /^::/;
+ } else {
+ $pkg = $cur;
+ }
$^H{bareword} = 1;
++$pkgs{$pkg};
no strict 'refs';
--- /dev/null
+#!perl -T
+
+use strict;
+use warnings;
+
+use Test::More tests => 7;
+
+my $invalid = qr/Invalid\s+package\s+name/;
+
+eval "use subs::auto in => \\( q{foo::bar} )";
+like($@, $invalid, 'no ref as package name');
+
+eval "use subs::auto in => qq{foo\\nbar}";
+like($@, $invalid, 'no newline in package name');
+
+eval "use subs::auto in => q{foo-bar}";
+like($@, $invalid, 'no dash in package name');
+
+eval "use subs::auto in => q{foo:bar}";
+like($@, $invalid, 'no single colon in package name');
+
+eval "use subs::auto in => q{foo:::bar}";
+like($@, $invalid, 'no three colons in package name');
+
+eval "use subs::auto in => q{1foo::bar}";
+like($@, $invalid, 'no package name starting by a digit');
+
+eval "use subs::auto in => q{foo::2bar}";
+like($@, $invalid, 'no package name with a digit inside');
--- /dev/null
+#!perl -T
+
+use strict;
+use warnings;
+
+use Test::More tests => 12;
+
+our $foo;
+
+{
+ use subs::auto in => 'subs::auto::Test::Pkg';
+
+ eval { subs::auto::Test::Pkg::foo 5 };
+ is($@, '', 'compiled to subs::auto::Test::Pkg::foo(5)');
+ is($foo, 10, 'subs::auto::Test::Pkg::foo was really called');
+
+ {
+ use subs::auto;
+
+ eval { foo 3 };
+ is($@, '', 'compiled to foo(3)');
+ is($foo, 3, 'main::foo was really called');
+
+ {
+ package subs::auto::Test::Pkg;
+
+ eval { foo 7 };
+ Test::More::is($@, '', 'compiled to foo(7)');
+ Test::More::is($foo, 14, 'subs::auto::Test::Pkg::foo was really called');
+
+ eval { main::foo 9 };
+ Test::More::is($@, '', 'compiled to main::foo(9)');
+ Test::More::is($foo, 9, 'main::foo was really called');
+ }
+ }
+}
+
+{
+ use subs::auto in => '::';
+
+ eval { foo 11 };
+ is($@, '', 'compiled to foo(11)');
+ is($foo, 11, 'main::foo was really called');
+}
+
+{
+ package subs::auto::Test;
+
+ use subs::auto in => '::Pkg';
+
+ {
+ package subs::auto::Test::Pkg;
+
+ eval { foo 13 };
+ Test::More::is($@, '', 'compiled to foo(13)');
+ Test::More::is($foo, 26, 'subs::auto::Test::Pkg::foo was really called');
+ }
+}
+
+sub foo {
+ $main::foo = $_[0];
+}
+
+sub subs::auto::Test::Pkg::foo {
+ $main::foo = 2 * $_[0];
+}