Subject: | Feature patch - $Params::Check::CALLER_DEPTH variable. |
The attached patch adds $Params::Check::CALLER_DEPTH which you can use
to implement wrappers around Params::Check::check().
It adds the variable, code, tests and documentation with a small sample.
PS: Thanks for the nice lvalue-feature in Object::Accessor =)
Subject: | Params-Check-0.24.patch |
--- Params-Check-0.24/lib/Params/Check.pm Thu Mar 2 13:02:52 2006
+++ Params-Check-0.24/lib/Params/Check.pm Wed Jul 5 15:41:43 2006
@@ -12,7 +12,7 @@
use vars qw[ @ISA $VERSION @EXPORT_OK $VERBOSE $ALLOW_UNKNOWN
$STRICT_TYPE $STRIP_LEADING_DASHES $NO_DUPLICATES
$PRESERVE_CASE $ONLY_ALLOW_DEFINED $WARNINGS_FATAL
- $SANITY_CHECK_TEMPLATE
+ $SANITY_CHECK_TEMPLATE $CALLER_DEPTH
];
@ISA = qw[ Exporter ];
@@ -28,6 +28,7 @@
$ONLY_ALLOW_DEFINED = 0;
$SANITY_CHECK_TEMPLATE = 1;
$WARNINGS_FATAL = 0;
+ $CALLER_DEPTH = 0;
}
my %known_keys = map { $_ => 1 }
@@ -527,7 +528,7 @@
sub _who_was_it {
my $level = $_[0] || 0;
- return (caller(2 + $level))[3] || 'ANON'
+ return (caller(2 + $CALLER_DEPTH + $level))[3] || 'ANON'
}
=head2 last_error()
@@ -646,6 +647,30 @@
If set to true, L<Params::Check> will C<croak> when an error during
template validation occurs, rather than return C<false>.
+
+Default is 0;
+
+=head2 $Params::Check::CALLER_DEPTH
+
+This global modifies the argument given to C<caller()> by
+C<Params::Check::check()> and is useful if you have a custom wrapper
+function around C<Params::Check::check()>. The value must be an
+integer, indicating the number of wrapper functions inserted between
+the real function call and C<Params::Check::check()>.
+
+Example wrapper function, using a custom stacktrace:
+
+ sub check {
+ my ($template, $args_in) = @_;
+
+ local $Params::Check::WARNINGS_FATAL = 1;
+ local $Params::Check::CALLER_DEPTH = $Params::Check::CALLER_DEPTH + 1;
+ my $args_out = Params::Check::check($template, $args_in);
+
+ my_stacktrace(Params::Check::last_error) unless $args_out;
+
+ return $args_out;
+ }
Default is 0;
--- Params-Check-0.24/t/01_Params-Check.t Thu Mar 2 13:02:16 2006
+++ Params-Check-0.24/t/01_Params-Check.t Wed Jul 5 15:41:50 2006
@@ -319,3 +319,20 @@
is( $rv->{lastname}, $lastname,
" found provided values in rv" );
}
+
+### $Params::Check::CALLER_DEPTH test
+{
+ sub wrapper { check ( @_ ) };
+ sub inner { wrapper( @_ ) };
+ sub outer { inner ( @_ ) };
+ outer( { dummy => { required => 1 }}, {} );
+
+ like( last_error, qr/for .*::wrapper by .*::inner$/,
+ "wrong caller without CALLER_DEPTH" );
+
+ local $Params::Check::CALLER_DEPTH = 1;
+ outer( { dummy => { required => 1 }}, {} );
+
+ like( last_error, qr/for .*::inner by .*::outer$/,
+ "right caller with CALLER_DEPTH" );
+}