If you're worried about catching and accidently printing nulls, there's a quick and easy way that almost everyone will recommend you do first: add the following to your program:
use strict;
use warnings;
The problem in particular seems odd; when I do
my $foo = 'zip';
$foo =~ /(bal)/;
print "\$1: '$1'";
I get
$1: ''
(and with use strict and warnings, the additional error
Use of uninitialized value in concatenation (.) or string at - line 5.
Of course, you can prevent $1 from ever being null if you test your regex:
if ($foo =~ /(pattern)/) {
# $1 is guaranteed to be ok here, if it matched
}
So yeah, it might be your logger re-interpreting $1 as something else. Try adding two more \\
; one for escaping the $, and another for escaping an extra backslash. Thus it'd look like
print "\\\$1: '$1'";