First of all, many thanks for your valuable input! I learn a lot from your comments :-)
I understood that my ($infile, $param) = @ARGV; is a smart way to read two input parameters from the command line. What happens if the second parameter is missing, i.e. perl entityPairs.pl input.xml? Will that mean that the condition in ternary operator expression evaluates to false and the values after ":" are taken?
I've got two issues:
- The output only shows $regex, but not $1 (the current value), so for example &[^;]+;\s&[^;]+;: for each occurrence in the original file and for example –§: in the modified file.
There is a warning with respect to the line that prints the occurrences.
print {$out} "$regex: $1\n" while $xml =~ /$regex/g;
Use of uninitialized value $1 in concatenation (.) or string at entityPairs.pl
line 42, <$in> line 1 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.
To help you figure out what was undefined, perl will try to tell you
the name of the variable (if any) that was undefined. In some cases
it cannot do this, so it also tells you what operation you used the
undefined value in. Note, however, that perl optimizes your program
and the operation displayed in the warning may not necessarily appear
literally in your program. For example, "that $foo" is usually
optimized into "that " . $foo, and the warning will refer to the
concatenation (.) operator, even though there is no . in
your program.
FYI, the current version of the script:
#!/usr/bin/perl
use warnings;
use strict;
use diagnostics;
print "Find pair of entities without/with separating space\n";
# read input file and param ('mod' = for modified files)
my ($infile, $param) = @ARGV;
my @regexes = $param ? (qr/–§/,
qr/–Ü/,
qr/ߧ/)
: (qr/&[^;]+;\s&[^;]+;/);
open my $in, '<', $infile or die "Cannot open $infile for reading: $!"
+;
#read input file in variable $xml
my $xml;
{
local $/ = undef;
$xml = <$in>;
}
#define output file
open my $out, '>', 'pairs.txt' or die $!;
#output statistics
print {$out} "Find pair of entities without/with separating space\n\ni
+nput file: ";
print {$out} "$infile";
print {$out} "\n======================================================
+==================\n\n";
for my $i (0 .. $#regexes) {
my $regex = $regexes[$i];
$regex =~ s/^\(\?\^://;
$regex =~ s/\)$//;
print {$out} "$regex: $1\n" while $xml =~ /$regex/g;
}
close $in;
close $out;