Skip to content

Commit 3ae57ca

Browse files
committed
pre-install and post-install hooks for Type::Params
1 parent 7d0eb87 commit 3ae57ca

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

lib/Type/Params.pm

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,21 @@ sub signature_for {
184184
goto( $compiled );
185185
};
186186

187+
our ( %PRE_INSTALL, %POST_INSTALL );
188+
if ( my $cb = $PRE_INSTALL{$package} ) {
189+
Types::Standard::assert_ArrayRef( $cb );
190+
$_->( $sig ) for @$cb;
191+
}
192+
187193
no strict 'refs';
188194
no warnings 'redefine';
189195
*$fullname = set_subname( SIGNATURE_SUBNAME_PREFIX . $fullname . SIGNATURE_SUBNAME_SUFFIX, $coderef );
190196

197+
if ( my $cb = $POST_INSTALL{$package} ) {
198+
Types::Standard::assert_ArrayRef( $cb );
199+
$_->( $sig ) for @$cb;
200+
}
201+
191202
return $sig;
192203
}
193204

@@ -2036,6 +2047,27 @@ Example usage:
20362047
20372048
=back
20382049
2050+
=head1 HOOKS
2051+
2052+
You can install coderefs which will be called whenever C<signature_for>,
2053+
C<signature_for_func>, or C<signature_for_method> are used to wrap a
2054+
function or method. They are ignored by C<signature> and by the pre-v2 API.
2055+
2056+
push @{ $Type::Params::POST_INSTALL{'My::Package'} ||= [] }, sub {
2057+
my $signature = shift;
2058+
...;
2059+
};
2060+
2061+
push @{ $Type::Params::PRE_INSTALL{'My::Package'} ||= [] }, sub {
2062+
my $signature = shift;
2063+
...;
2064+
};
2065+
2066+
The C<< $signature >> will be a blessed L<Type::Params::Signature> object.
2067+
2068+
The intention for this is to allow for future extensions to expose signature
2069+
data as metadata.
2070+
20392071
=head1 BUGS
20402072
20412073
Please report any bugs to
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
=pod
2+
3+
=encoding utf-8
4+
5+
=head1 PURPOSE
6+
7+
Test the pre-install and post-install hooks.
8+
9+
=head1 AUTHOR
10+
11+
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
12+
13+
=head1 COPYRIGHT AND LICENCE
14+
15+
This software is copyright (c) 2025 by Toby Inkster.
16+
17+
This is free software; you can redistribute it and/or modify it under
18+
the same terms as the Perl 5 programming language system itself.
19+
20+
=cut
21+
22+
use strict;
23+
use warnings;
24+
use Test::More;
25+
26+
my @GOT;
27+
28+
push @{ $Type::Params::PRE_INSTALL{'Local::Foo'} ||= [] }, sub {
29+
my $signature = shift;
30+
push @GOT, pre => $signature->subname;
31+
};
32+
33+
push @{ $Type::Params::POST_INSTALL{'Local::Foo'} ||= [] }, sub {
34+
my $signature = shift;
35+
push @GOT, post => $signature->subname;
36+
};
37+
38+
{
39+
package Local::Foo;
40+
use Types::Common -all;
41+
signature_for bar => ( pos => [] );
42+
sub bar { return 'bar' };
43+
}
44+
45+
is_deeply( \@GOT, [ qw/ pre bar post bar / ] );
46+
47+
done_testing;

0 commit comments

Comments
 (0)