Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Help with error handling

by eversuhoshin (Sexton)
on Mar 19, 2011 at 15:37 UTC ( #894156=perlquestion: print w/ replies, xml ) Need Help??
eversuhoshin has asked for the wisdom of the Perl Monks concerning the following question:

Hello~

Thank you fellow monks for all the good support :)

Without you guys I would be crying in my cubicle

I'm converting pdf files to text files but I don't know how to handle in cases the conversion fails

I want it to move to the next file if the conversion fails.

It would be great if you can help me write the error handling in case CAM::PDF fails to convert to the pdf to text

;

Below is my code thanks again :)

#!/usr/bin/perl -w use strict; use CAM::PDF; use CAM::PDF::PageText; use File::Spec::Functions; use File::Find::Rule; #1. Specify directory my $dirPath= 'F:/project_italia/firm_files'; my $src_fmt='*.*'; #2 retrieve all sub-dirs from the directory my @allDir = File::Find::Rule->maxdepth(1)->directory->in($dirPath); open(LIST, ">>", 'F:/project_italia/firmList_pdf.txt' ) or die "Can't +open destination.fil $!"; foreach (@allDir) { my $src_path = catfile($_, $src_fmt); my @filings = glob($src_path); foreach (@filings) { my $fileName=$_; my $pdf = CAM::PDF->new($_); #split path name my @textName = split(/\\/, $_); #Create new text file open(DEST, ">>", 'F:/project_italia/firm_text/'.$textName[4].'. +txt' ) or die "Can't open destination.fil $!"; #keeps track on whether the pdf to text conversion was successf +ul my $conv=0; my $count=0; #Get total page number of the pdf my $pageNum=$pdf->numPages(); for (1..$pageNum) { my $pageConv = $pdf->getPageContentTree($_); #error handle if fail go to next file print DEST CAM::PDF::PageText->render($pageConv); $count++; #conversion is successful only when all pages have been co +nverted $conv=1 if ($count==$pageNum); } close DEST; #keep track of which files converted successfully print LIST "| $conv"; } } close LIST;

Comment on Help with error handling
Download Code
Re: Help with error handling
by Eliya (Vicar) on Mar 19, 2011 at 15:49 UTC
    I want it to move to the next file if the conversion fails.

    What exactly is the problem?  Does the script die in case of error?

    If so, you might be looking for eval {...}.

Re: Help with error handling
by graff (Chancellor) on Mar 19, 2011 at 16:55 UTC
    So, there are some errors that would really stop the whole process, and others that only interfere with a particular file. Deal with the former before you start looping over files (die if any of them happen), and then deal with the latter inside the loop (and just warn about each one that happens).

    Based on the OP code, something like this would be one way to go:

    #!/usr/bin/perl -w use strict; use CAM::PDF; use CAM::PDF::PageText; use File::Spec::Functions; use File::Find::Rule; ## ... steps 1 and 2 as per OP code ... my $outputDir = 'F:/project_italia/firm_text'; ( -d $outputDir and -w _ ) or die "Output directory $outputDir not found or not writable\n"; open(LIST, ">>", 'F:/project_italia/firmList_pdf.txt' ) or die "Can't open destination.fil $!"; foreach (@allDir) { ... foreach (@filings) { my $fileName=$_; my $pdf = CAM::PDF->new($_) or do { warn "PDF->new failed on $_\n"; next; }; ... # open output text file (NB: ">>" appends, creates if necessar +y) open(DEST, ">>", "$outputDir/$textName[4].txt" ) or do { warn "Unable to append to $outputDir/$textName[4].txt: $!\ +n "; next; }; ...
    It looks like you're on the right track with handling problems inside the page loop. Just tweak the layout of the warning messages to suit your taste -- that is, think about tailoring the warnings to make them easy to count, search, summarize, etc.
      Thank you so much for your help graff :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://894156]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2014-09-17 16:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (91 votes), past polls