[Koha-devel] Perl advice for checking hash content

Victor Grousset/tuxayo victor at tuxayo.net
Sat Aug 28 09:38:04 CEST 2021


Hi :)

For a development, I'm adding additional keys to 
C4::Reserves::CanItemBeReserved returned hash.

But there are a bunch of tests that match the whole return value and 
thus don't expect the new keys in the hash.

Here is the best way so far I've found to adapt it.


is_deeply(
     CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
     { status => 'tooManyReservesToday', limit => 0 },
     'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
);

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

my @status_and_limit = @{CanItemBeReserved( $patron->borrowernumber, 
$itemnumber_2 )}{"status", "limit"};
is_deeply(
     \@status_and_limit,
     ['tooManyReservesToday', 0],
     'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
);


How could this be better?
Since it took me more than two hours only to have something that works 
and without warnings. There are surely other ways to do it™, and 
hopefully a nicer one.
Most of the time was reading and learning by trial and error about 
referencing and dereferencing and the specifics or Perl hashes and 
array. It's odd to have been working 3.5 years on Koha closer to the 
code than to the librarian stuff and only have to really start learning 
Perl now ^^
Away, this is why advice is needed to have something that fit the good 
practices :)

Key/Value Hash Slices would allow to not change the expected param of 
is_deeply() but that doesn't really look different for the readability.
And isn't worth having Koha bump minimal Perl version from 5.14 (2011) 
to 5.20 (2014). Ok it's just for running the tests and not production 
but still. -> very minor or inexistent readability improvement.
https://perldoc.perl.org/perldata#Key/Value-Hash-Slices

Having two assertions (is() calls) for status and limit would allow a 
simpler syntax but add duplication.


Cheers,

-- 
Victor Grousset/tuxayo


More information about the Koha-devel mailing list