Your code has a bug. In the regex in
"
if ($line =~ m/^interface (\.*)/) {"
you have escaped the dot, which isn't what you want. The regex should be "
/^interface (.*)/)".
Less importantly, you are using a leading ampersand in the sub calls
"&parse_ethernet($1);" and "&parse_gigabit($1);". They are unnecessary and you shouldn't use them unless you know what they do and want tthe effect. Here, you don't.
As for your parsing problem, you can set the input separator to "!\n" when you read the file. Then you will have one complete chunk of input to give to any of the subs parse_* or whatever. Note that these must now expect a multiline string containing all the information in the current chunk.
Here is a sketch:
$/ = "!\n";
while ( <DATA> ) {
chomp;
if ( /^interface (.*)/ ) {
if ( $1 eq 'Ethernet' ) {
parse_ethernet( $_);
} elsif ( $1 eq 'Gigabit' ) {
parse_gigabit( $_);
}
} elsif ( /^system (.*)/ ) {
# blah...
}
}
sub parse_gigabit {
my $chunk = shift;
$chunk =~ tr/\n/ /;
print "GIGABIT: $chunk\n";
}
sub parse_ethernet {
my $chunk = shift;
$chunk =~ tr/\n/ /;
print "ETHERNET: $chunk\n";
}
__DATA__
!
interface Ethernet
blah...
blah...
!
interface Gigabit
blah...
blah...
!
interface Ethernet
blah...
!
Anno