Your usage is basically correct, but there are several things to note:  The filenames in @all (as returned by readdir) won't include any directory components of the path, so unless your $ARGV[0] always is '.', you probably want

my $pdf = PDF->new("$path/$aa");

Next, and more importantly, PDF->new() does die in some cases, which will make the entire script terminate... To work around this, you need to eval { ... } that call:

foreach my $aa (@all) { my $pdf; eval { $pdf = PDF->new("$path/$aa") }; if (!$@ && $pdf->IsaPDF) { print "$aa is a pdf file\n" } else { print "$aa is corrupted\n"; } }

Lastly, I'm not sure at all if the very simple method used to identify PDF files, i.e.

# from PDF/Parse.pm sub IsaPDF { return ($_[0]->{Header} != undef) ; }

with Header being extracted as the version component of the basic PDF header (e.g. "%PDF-1.6" --> "1.6"), would actually identify more subtle corruptness in the file. So, unless the problem is such that it would make the parser die, the error would likely go unnoticed...


In reply to Re: I have a problem in finding corrupted PDF files by almut
in thread I have a problem in finding corrupted PDF files by hurix_03

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.