From: Vincent Pit Date: Thu, 28 Aug 2008 12:45:57 +0000 (+0200) Subject: Refactor the package name validation in a proper sub X-Git-Tag: v0.02~5 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=9bb6a20aa33aaac83290822b760963af86147263;p=perl%2Fmodules%2Fsubs-auto.git Refactor the package name validation in a proper sub --- diff --git a/lib/subs/auto.pm b/lib/subs/auto.pm index 6c4bf27..c0beebe 100644 --- a/lib/subs/auto.pm +++ b/lib/subs/auto.pm @@ -146,25 +146,27 @@ my $wiz = wizard data => sub { +{ pkg => $_[1] } }, my %pkgs; +sub _validate_pkg { + my ($pkg, $cur) = @_; + return $cur unless $pkg; + 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 =~ /^::/; + $pkg; +} + sub import { 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; - } + my $cur = (caller 1)[0]; + my $in = _validate_pkg $args{in}, $cur; $^H{bareword} = 1; - ++$pkgs{$pkg}; + ++$pkgs{$in}; no strict 'refs'; - cast %{$pkg . '::'}, $wiz, $pkg; + cast %{$in . '::'}, $wiz, $in; } sub unimport {