X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=lib%2FSub%2FPrototype%2FUtil.pm;h=d2564d85e33dd66c758302d3798343d97b999367;hb=c7c6c66b6fb5da113036318ca7e196ac422b32b0;hp=df3e45635b3331189460ee8a5f5d31bfb80c4487;hpb=bc05eb29f9f194f122bea1ddd5ddbf81acf1f359;p=perl%2Fmodules%2FSub-Prototype-Util.git diff --git a/lib/Sub/Prototype/Util.pm b/lib/Sub/Prototype/Util.pm index df3e456..d2564d8 100644 --- a/lib/Sub/Prototype/Util.pm +++ b/lib/Sub/Prototype/Util.pm @@ -14,13 +14,13 @@ Sub::Prototype::Util - Prototype-related utility routines. =head1 VERSION -Version 0.08 +Version 0.09 =cut use vars qw/$VERSION/; -$VERSION = '0.08'; +$VERSION = '0.09'; =head1 SYNOPSIS @@ -70,13 +70,14 @@ sub _clean_msg { Flattens the array C<@args> according to the prototype C<$proto>. When C<@args> is what C<@_> is after calling a subroutine with prototype C<$proto>, C returns the list of what C<@_> would have been if there were no prototype. +It croaks if the arguments can't possibly match the required prototype, e.g. when a reference type is wrong or when not enough elements were provided. =cut sub flatten { my $proto = shift; return @_ unless defined $proto; - my @args; + my @args; while ($proto =~ /(\\?)(\[[^\]]+\]|[^\];])/g) { my $p = $2; if ($1) { @@ -128,7 +129,7 @@ Valid keys are : Specifies the function used in the generated code to test the reference type of scalars. Defaults to C<'ref'>. -You may also want to use C. +You may also want to use L. =item C<< wrong_ref => $code >> @@ -157,47 +158,52 @@ For example, this allows you to recall into C and C by us =cut sub _wrap { - my ($name, $proto, $i, $args, $cr, $opts) = @_; + my ($name, $proto, $i, $args, $coderefs, $opts) = @_; + while ($proto =~ s/(\\?)(\[[^\]]+\]|[^\];])//) { - my ($ref, $p) = ($1, $2); - $p = $1 if $p =~ /^\[([^\]]+)\]/; - my $cur = '$_[' . $i . ']'; + my ($ref, $sigil) = ($1, $2); + $sigil = $1 if $sigil =~ /^\[([^\]]+)\]/; + + my $cur = "\$_[$i]"; + if ($ref) { - if (length $p > 1) { - return 'my $r = ' . $opts->{ref} . '(' . $cur . '); ' - . join ' els', - map( { - "if (\$r eq '" . $reftypes{$_} ."') { " - . _wrap($name, $proto, ($i + 1), - $args . $_ . '{' . $cur . '}, ', - $cr, $opts) - . ' }' - } split //, $p), - 'e { ' . $opts->{wrong_ref} . ' }' + if (length $sigil > 1) { + my $code = "my \$r = $opts->{ref}($cur); "; + my @branches = map { + my $subcall = _wrap( + $name, $proto, ($i + 1), $args . "$_\{$cur}, ", $coderefs, $opts + ); + "if (\$r eq '$reftypes{$_}') { $subcall }"; + } split //, $sigil; + $code .= join ' els', @branches, "e { $opts->{wrong_ref} }"; + return $code; } else { - $args .= $p . '{' . $cur . '}, '; + $args .= "$sigil\{$cur}, "; } - } elsif ($p =~ /[\@\%]/) { + } elsif ($sigil =~ /[\@\%]/) { $args .= '@_[' . $i . '..$#_]'; - } elsif ($p =~ /\&/) { - my %h = do { my $c; map { $_ => $c++ } @$cr }; + } elsif ($sigil =~ /\&/) { + my %h = do { my $c; map { $_ => $c++ } @$coderefs }; my $j; - if (not exists $h{$i}) { - push @$cr, $i; - $j = $#{$cr}; - } else { + if (exists $h{$i}) { $j = int $h{$i}; + } else { + push @$coderefs, $i; + $j = $#{$coderefs}; } - $args .= 'sub{&{$c[' . $j . ']}}, '; - } elsif ($p eq '_') { - $args .= '((@_ > ' . $i . ') ? ' . $cur . ' : $_), '; + $args .= "sub{&{\$c[$j]}}, "; + } elsif ($sigil eq '_') { + $args .= "((\@_ > $i) ? $cur : \$_), "; } else { - $args .= $cur . ', '; + $args .= "$cur, "; } + } continue { ++$i; } + $args =~ s/,\s*$//; - return $name . '(' . $args . ')'; + + return "$name($args)"; } sub _check_name { @@ -309,7 +315,7 @@ Tests code coverage report is available at L