1 package Sub::Prototype::Util;
7 use Scalar::Util qw/reftype/;
11 Sub::Prototype::Util - Prototype-related utility routines.
19 our $VERSION = '0.02';
23 use Sub::Prototype::Util qw/flatten recall/;
26 my @args = ( \@a, 1, { d => 2 }, undef, 3 );
28 my @flat = flatten '\@$;$', @args; # ('a', 'b', 'c', 1, { d => 2 })
29 recall 'CORE::push', @args; # @a contains 'a', 'b', 'c', 1, { d => 2 }, undef, 3
33 Prototypes are evil, but sometimes you just have to bear with them, especially when messing with core functions. This module provides several utilities aimed at facilitating "overloading" of prototyped functions.
35 They all handle C<5.10>'s C<_> prototype.
41 my %sigils = qw/SCALAR $ ARRAY @ HASH % GLOB * CODE &/;
46 if (!defined $a || !defined($r = reftype $a)) { # not defined or plain scalar
47 croak 'Got ' . ((defined $a) ? 'a plain scalar' : 'undef')
48 . ' where a reference was expected';
50 croak 'Unexpected ' . $r . ' reference' unless exists $sigils{$r}
51 and $p =~ /\Q$sigils{$r}\E/;
55 =head2 C<flatten $proto, @args>
57 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<flatten> returns the list of what C<@_> would have been if there were no prototype.
63 return @_ unless defined $proto;
65 while ($proto =~ /(\\?)(\[[^\]]+\]|[^\];])/g) {
69 my $r = _check_ref $a, $p;
71 SCALAR => sub { push @args, $$a },
72 ARRAY => sub { push @args, @$a },
73 HASH => sub { push @args, %$a },
74 GLOB => sub { push @args, *$a },
75 CODE => sub { push @args, &$a }
78 } elsif ($p =~ /[\@\%]/) {
90 =head2 C<recall $name, @args>
92 Calls the function C<$name> with the prototyped argument list C<@args>. That is, C<@args> should be what C<@_> is when you define a subroutine with the same prototype as C<$name>. For example,
95 recall 'CORE::push', $a, 1, 2, 3;
97 will call C<push @$a, 1, 2, 3> and so fill the arrayref C<$a> with C<1, 2, 3>. This is especially needed for core functions because you can't C<goto> into them.
103 croak 'Wrong subroutine name' unless $name;
105 $name =~ s/[\s\$\@\%\*\&;].*//;
106 my $proto = prototype $name;
108 if (defined $proto) {
110 while ($proto =~ /(\\?)(\[[^\]]+\]|[^\];])/g) {
113 my $r = _check_ref $a[$i], $p;
114 push @args, join '', $sigils{$r}, '{$a[', $i, ']}';
115 } elsif ($p =~ /[\@\%]/) {
116 push @args, join '', '@a[', $i, '..', (@a - 1), ']';
118 } elsif ($p =~ /\&/) {
119 push @args, 'sub{&{$a[' . $i . ']}}';
120 } elsif ($p eq '_') {
123 push @args, '$a[' . $i . ']';
128 @args = map '$a[' . $_ . ']', 0 .. @a - 1;
130 my @ret = eval $name . '(' . join(',', @args) . ');';
137 The functions L</flatten> and L</recall> are only exported on request, either by providing their name or by the C<':funcs'> and C<':all'> tags.
141 use base qw/Exporter/;
145 'funcs' => [ qw/flatten recall/ ]
147 our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS;
148 $EXPORT_TAGS{'all'} = [ @EXPORT_OK ];
152 L<Carp>, L<Exporter> (core modules since perl 5), L<Scalar::Util> (since 5.7.3).
156 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
158 You can contact me by mail or on #perl @ FreeNode (vincent or Prof_Vince).
162 Please report any bugs or feature requests to C<bug-sub-prototype-util at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Sub-Prototype-Util>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
166 You can find documentation for this module with the perldoc command.
168 perldoc Sub::Prototype::Util
170 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/Sub-Prototype-Util>.
172 =head1 COPYRIGHT & LICENSE
174 Copyright 2008 Vincent Pit, all rights reserved.
176 This program is free software; you can redistribute it and/or modify it
177 under the same terms as Perl itself.
181 1; # End of Sub::Prototype::Util