A couple of comments on your code ...

Firstly, the system( 'dir /b /s > files.tmp' ); comment is entirely unnecessary as there are many ways by which you can obtain a directory listing of files to process within Perl without the external dependency on other system programs - This issue has been discussed before by myself here. Furthermore, the listing of files by this external process to a temporary file (with a known name) for subsequent iteration compounds this problem further - The use of a known or easily predicted file name makes it easier for someone to introduce bogus values into the file for action upon by your program. This point was discussed with reference to POSIX::tmpnam on comp.lang.perl.moderated here.

Now, admittedly this emphasis on security dependencies may be a little extreme, but even in a more basic sense, you are creating more work for yourself by obtaining and processing your file list in this fashion. Consider your code (comments removed for brevity) ...

system( 'dir /b /s > files.tmp' ); open( IN, 'files.tmp' ); my @files = grep( /\.java$/, <IN> ); close IN; chomp( @files ); system( 'del files.tmp' ); print "Processing files.\n"; my $file; foreach $file (@files) { ... }

... compared with a more direct piece of code to action upon specific files using File::Find ...

use File::Find; find ({ 'wanted' => \&processfile, 'follow' => 0 }, '/'); sub processfile { my ($dev, $ino, $mode, $nlink, $uid, $gid); return unless (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_) +); return unless -f _ && -r _ && /^.*\.java\z/si; ... };

In contrast the code fragment above not only finds files ending in .java but also ensures that they are in fact files and are readable by the current process - With your code, it was possible that directory names ending in .java would also be included in @files to be subsequently processed.

Another little thing I noticed in your code was the dependency on the regular expression $filename =~ s/^.*\\(\w+\.java).*$/$1/; to return the name of file. This won't work on any *nix system because of the specification of path separators as '\' - A better solution in this instance would be to use File::Spec to extract the filename from the full-path (or alternatively use $File::Find::name if using File::Find).

Other than these (mostly anal) points, a very useful little piece of code.

 

perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'


In reply to Re: Gather dependency list from Java packages. by rob_au
in thread Gather dependency list from Java packages. by rje

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.