papai has asked for the wisdom of the Perl Monks concerning the following question:

I am using Net::DNS::ZoneFile::Fast::parse in my script to parse through hundreds of zone files. If there are errors in the zone files an error is printed out with the line number where the error is located. However, I also want to be able to print out the filename that it has found errors in. I can do this but currently my code will print out every filename it processes whether the error occurs or not. I want to run my script and only print out the filename when an error pops up and prints to screen. My code reads as follows:

foreach $filename (@files) { #print "Processing $filename....\n" #I don't want this $rr = Net::DNS::ZoneFile::Fast::parse(file=>$filename,soft_errors= +>1); foreach $rec (@{$rr}) { .... } }

Replies are listed 'Best First'.
Re: identifying dns zone filename
by gman (Friar) on Oct 20, 2009 at 22:56 UTC
    what about using on_error => /&error_sub ?
Re: identifying dns zone filename
by keszler (Priest) on Oct 20, 2009 at 23:05 UTC
    Use the on_error parameter:
    foreach $filename (@files) { $rr = Net::DNS::ZoneFile::Fast::parse( file=>$filename, on_error => sub {print "Error in $filename\n";} ); }

      Yes that will print the filename, however now I do not get the line number? I need both. I also tried

      foreach $filename (@files) { $rr = Net::DNS::ZoneFile::Fast::parse( file=>$filename, on_error => sub {print "Error in $filename on line: $ln\n";} ); }

      but that didn't work

        According to Net::DNS::ZoneFile::Fast:

        on_error
        An optional parameter, user-defined error handler. If specified, it must be a subroutine reference, which will be called on any error. This subroutine will be passed two parameters: a line number in the zone, where the error occurred, and the error description.
        So:
        foreach $filename (@files) { $rr = Net::DNS::ZoneFile::Fast::parse( file=>$filename, on_error => sub { my ($ln,$desc) = @_; print "Error in $filename on line: $ln\n"; print "Description: $desc\n\n"; } ); }