For expanding
Anno's solution, you can use a dispatch table to apply the corresponding policy :
my %parsers = (
'Ethernet' => sub {
my $chunk = shift;
$chunk =~ tr/\n/ /;
return "ETHERNET: $chunk";
},
'Gigabit' => sub {
my $chunk = shift;
$chunk =~ tr/\n/ /;
return "GIGABIT: $chunk";
}
);
$/ = "!\n";
$\ = "\n";
while ( <DATA> ) {
chomp;
if ( /^interface (.*)/ && $1 && exists $parsers{$1} ) {
print $parsers{$1}( $_ );
} elsif ( /^system (.*)/ ) {
# blah...
}
}
Edit: As anonymous said,
( ) instead of
{ }. Copypasta from/to Firefox is treacherous.