Structured instructions for simple extraction script
This is how I would implement it:

#!/usr/bin/perl
use warnings;
use strict;    

my $file = 'input.dat';
open my $IN, '&lt;', $file or die $!;

my $uniq;
while (my $line = &lt;$IN&gt;) {
    next unless $line =~ /^\s*keyword123/;
    if (defined $uniq and $line ne $uniq) {
        die "Entry not unique. Exiting.\n";
    }
    $uniq = $line;
}
$uniq =~ s/.*\((.*)\).*/$1/;
$uniq =~ s/\s.*//;
print "keyword123: $uniq";


You can check the uniqueness each time you read the keyword line in. Once you find a non-unique line, you can stop right at the moment, no need to search the rest of the file.