in reply to reg perl substitution

Hopefully the others have addressed your issue. I'd like to comment on

open FILE, "+< test.cfg" || die "Cannot open file\n" ;

It's the same thing as

open FILE, ("+< test.cfg" || die "Cannot open file\n") ;

You want

open FILE, "+< test.cfg" or die "Cannot open file\n";

General rule: If you don't care about the return vakue of the OR, you want or instead of ||.

Better yet, use a lexical file handle, use the 3-arg form of open, and include the cause of the error ($!) in the error message.

open my $fh, '+<', 'test.cfg" or die "Cannot open file: $!\n";