open my $iplist_fh, '<', 'iplist.txt' or die "Can't read iplist.txt: $!\n"; my %text_for; while ( <$iplist> ) { chomp; if ( m{ \A # line start ( \d{1,3} (?: \. \d{1,3} ){3} ) # IP address \s+ \| \s+ # separator ( .* ) # text \z # line end }xms ) { $text_for{$1} = $2; } } close $iplist_fh or die "Can't close??: $!\n";