From c73b26581660fe0d06ac40a37a93277018174005 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Thu, 18 Nov 2010 14:03:17 +0100 Subject: [PATCH] Hilfsfunktion call_sub_if --- SL/DB/Object.pm | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/SL/DB/Object.pm b/SL/DB/Object.pm index 71e0a3939..a2db47f48 100644 --- a/SL/DB/Object.pm +++ b/SL/DB/Object.pm @@ -84,6 +84,16 @@ sub call_sub { return $self->$sub(@_); } +sub call_sub_if { + my $self = shift; + my $sub = shift; + my $check = shift; + + $check = $check->($self) if ref($check) eq 'CODE'; + + return $check ? $self->$sub(@_) : $self; +} + 1; __END__ @@ -145,6 +155,16 @@ name is a composite, e.g. my $chart_id = $buchungsgruppe->call_sub(($is_sales ? "income" : "expense") . "_accno_id_${taxzone_id}"); +=item C + +Calls the sub C<$name> on C<$self> with the arguments C<@args> if +C<$check> is trueish. If C<$check> is a code reference then it will be +called with C<$self> as the only argument and its result determines +whether or not C<$name> is called. + +Returns the sub's result if the check is positive and C<$self> +otherwise. + =back =head1 AUTHOR -- 2.20.1