Maybe it would help if you added a print just after the loop, to confirm you have exited the loop, as follows:
} if ( $x =~ m/^Patient/ ) { print "matched here"; last WID; } } print "out of loop"; if ( $obj->has_members ) { dump_members($obj); } }
I wonder what output dump_members($obj) is producing.
update3 I suspect you are calling your subroutine more than once. Each call produces one or two tagged (e.g. <DATE></DATE>) blocks of data. Your loop within the subroutine is probably exiting as you expect (note that if you put the last before your print statement the print doesn't execute) but the caller then calls again with more data and your subroutine produces more output. You can test for this by adding a print "starting dump_afp" at the start of your subroutine.
update: If you post a dump of the object you are passing to the subroutine I can try running it and let you know what happens on my system.
update2: Here is the result of running your sub through B::Deparse. I see nothing obviously wrong.
$ perl -MO=Deparse test.pl BEGIN { $^W = 1; } sub dump_afp { my $obj = shift @_; my $struct = $obj->struct; my(@keys) = sort(grep((!/^_|^(?:Data|EscapeSequence|ControlCode|Le +ngth|CC|(?:Sub)?Type|FlagByte)$/), keys %$struct)); push @keys, 'Data' if exists $$struct{'Data'}; WID: foreach my $key (@keys) { next if ref $$struct{$key}; next unless length($x = $$struct{$key}); if ($obj->ENCODING and grep {$key eq $_;} $obj->ENCODED_FIELDS +) { $x = $obj->$key; $x = qq["$x"]; } elsif ($x =~ /[^\w\s]/) { $x = ''; } if ($key eq 'Data') { if ($x =~ /^"(\w|\d|\$)/) { $x =~ s/"|\(|\)//g; if ($x =~ m[^\d\d/\d\d/\d\d\s]) { my(@dateinfo) = split(/\s/, $x, 0); if ($dateinfo[0] =~ m[^\d\d/\d\d/\d\d]) { print '<DATE>'; print $dateinfo[0]; print "</DATE>\n"; print '<DESCRIPTION>'; print $dateinfo[1]; print "</DESCRIPTION>\n"; } } else { if ($x =~ m[^\d\d/\d\d/\d\d]) { print '<DATE>'; print $x; print "</DATE>\n"; } if (not $x =~ m[^\d\d/\d\d/\d\d] and $x != 'Patien +t') { if (not $x =~ /^((\d+)||(\d+,\d+))\.\d\d/) { print '<DESCRIPTION>'; print $x; print "</DESCRIPTION>\n"; } } } if ($x =~ /^((\d+)||(\d+,\d+))\.\d\d/) { print '<AMOUNT>'; print $x; print "</AMOUNT>\n"; } if ($x =~ /^((\$\d+)||(\$\d+,\d+))\.\d\d/) { print '<TOTAL>'; print $x; print "</TOTAL>\n"; } } } if ($x =~ /^Patient/) { last WID; } } if ($obj->has_members) { dump_members($obj); } } test.pl syntax OK
In reply to Re: Break perl foreach loop
by ig
in thread Break perl foreach loop
by pvecchio
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |