Use of uninitialized value $_ in substitution (s///)

Viewed 1307

I have a script that runs and downloads some data. Everything works fine for around 60 pulls and then suddenly I get the error:

Use of uninitialized value $_ in substitution (s///)

while ( my $row = $ia_applicant_query->fetchrow_arrayref ) {

    s/\t/ /g for @$row;    # <------- THIS IS WHAT THE ERROR POINTS TO
    my $line = join "\t", map { defined $_ ? $_ : '.' } @$row;
    $mydb->func( "$line\n", "putline" );
}

The error is then just repeated for the remaining 800 pulls. Any Idea what could be going wrong?

1 Answers

That means one of the values in @$row is undef, which happens if the column is NULL.

This can be avoided by replacing

s/\t/ /g for @$row;
my $line = join "\t",  map { defined($_) ? $_ : '.' } @$row;

with

my $line = join "\t",  map { defined($_) ? s/\t/ /gr : '.' } @$row;   # 5.14+

or

sub tabs_to_spaces { ( my $s = shift ) =~ s/\t/ /g; $s }

my $line = join "\t",  map { defined($_) ? tabs_to_spaces($_) : '.' } @$row;
Related