From 937717fb8767faa45b2fb5ea8e879be43615c12d Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Mon, 4 Nov 2024 09:23:47 +0000 Subject: [PATCH] Bug 30657: Try parent method first This patch adds a try/catch inside the Koha::Patron AUTOLOAD to try and find the method on the parent, including it's AUTOLOAD, prior to running the local AUTOLOAD code to generate attribute accessors and then use the called method. --- Koha/Patron.pm | 75 +++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 35 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 3e56e8911da..d6fb91e42ad 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -130,44 +130,49 @@ sub AUTOLOAD { my $self = shift(@args); my $name = $AUTOLOAD; $name =~ s/.*:://; # Remove package name - my $found = 0; - - # Fetch attributes dynamically - my $attribute_types = Koha::Patron::Attribute::Types->search(); - while ( my $attr = $attribute_types->next ) { - my $code = $attr->code; - $found = 1 if ( $code eq $name ); - - # Dynamically create accessor method - no strict 'refs'; - if ( !defined *{ ref($self) . "::$code" }{CODE} ) { - if ( $attr->repeatable ) { - *{ ref($self) . "::$code" } = sub { - my ($self) = @_; - return $self->_result->get_column($code) if $self->_result->has_column($code); - my $attributes = $self->_result->borrower_attributes->search( { code => $code } ); - my $values = []; - while ( my $attribute = $attributes->next ) { - push @$values, $attribute->attribute; - } - return $values; - }; - } else { - *{ ref($self) . "::$code" } = sub { - my ($self) = @_; - return $self->_result->get_column($code) if $self->_result->has_column($code); - my $attribute = $self->_result->borrower_attributes->search( { code => $code } )->first; - return $attribute ? $attribute->attribute : undef; - }; + + return try { + my $wt = 'SUPER::' . $name; + $self->$wt(@args); + } catch { + + # Fetch attributes dynamically + my $found = 0; + my $attribute_types = Koha::Patron::Attribute::Types->search(); + while ( my $attr = $attribute_types->next ) { + my $code = $attr->code; + $found = 1 if ( $code eq $name ); + + # Dynamically create accessor method + no strict 'refs'; + if ( !defined *{ ref($self) . "::$code" }{CODE} ) { + if ( $attr->repeatable ) { + *{ ref($self) . "::$code" } = sub { + my ($self) = @_; + return $self->_result->get_column($code) if $self->_result->has_column($code); + my $attributes = $self->_result->borrower_attributes->search( { code => $code } ); + my $values = []; + while ( my $attribute = $attributes->next ) { + push @$values, $attribute->attribute; + } + return $values; + }; + } else { + *{ ref($self) . "::$code" } = sub { + my ($self) = @_; + return $self->_result->get_column($code) if $self->_result->has_column($code); + my $attribute = $self->_result->borrower_attributes->search( { code => $code } )->first; + return $attribute ? $attribute->attribute : undef; + }; + } } } - } - if ($found) { - return $self->$name(); - } else { - my $wt = 'SUPER::' . $name; - return $self->$wt(@args); + if ($found) { + return $self->$name(); + } else { + $@; + } } } -- 2.47.0