How to enable warnings, part one of many
Today I sent an RFC about turning on warnings in all of Koha’s Perl scripts and modules. Of course, turning on warnings by itself does nothing except fill up the Apache log; the trick is to quell them by fixing the underlying problems.
As an example, consider misc/migrations_tools/bulkmarcimport.pl. On the plus side, it already contains a use warnings; statement; on the minus side, it was commented out. So close!
After enabling warnings and running bulkmarcimport -d -file test.mrc, we get
deleting biblios Use of uninitialized value in pattern match (m//) at \ misc/migration_tools/bulkmarcimport.pl line 118.
One of the most common errors you after turning on warnings are complaints about uninitialized variables. So what’s the variable in question?
if ($format =~ /XML/i) {
OK, so what is $format and why is it uninitialized?
my ($version, $delete, $test_parameter, $skip_marc8_conversion, $char_encoding, $verbose, $commit, $fk_off,$format);
$|=1;
GetOptions(
'commit:f' => \$commit,
'file:s' => \$input_marc_file,
'n:f' => \$number,
'o|offset:f' => \$offset,
'h' => \$version,
'd' => \$delete,
't' => \$test_parameter,
's' => \$skip_marc8_conversion,
'c:s' => \$char_encoding,
'v:s' => \$verbose,
'fk' => \$fk_off,
'm:s' => \$format,
);
$format starts off with an undefined value when it is declared by my, and if you don’t supply the -m switch when running bulkmarcimport.pl, it stays undefined.
Fortunately, the script’s usage tells us what the default value of $formatshould be
m format, MARCXML or ISO2709 (defaults to ISO2709)
Thus, we can fix that particular warning with this patch:
--- a/misc/migration_tools/bulkmarcimport.pl
+++ b/misc/migration_tools/bulkmarcimport.pl
@@ -2,7 +2,7 @@
# Import an iso2709 file into Koha 3
use strict;
-#use warnings;
+use warnings;
#use diagnostics;
BEGIN {
# find Koha's Perl modules
@@ -30,7 +30,8 @@ use IO::File;
binmode(STDOUT, ":utf8");
my ( $input_marc_file, $number, $offset) = ('',0,0);
-my ($version, $delete, $test_parameter, $skip_marc8_conversion, $char_encoding, $verbose, $commit, $fk_off,$format);
+my ($version, $delete, $test_parameter, $skip_marc8_conversion, $char_encoding, $verbose, $commit, $fk_off);
+my $format = "ISO2709";
$|=1;
We’re not done with bulkmarcimport.pl, but this is a start. More anon.
