That code is pretty messy.

File::Find will descend into all subdirectories and retrieve all the files therein, so you don't need to pick out directories and search them yourself. You just need to provide find() with: (1) a subroutine reference and (2) the starting directory.

You create a subroutine reference like this:

sub do_stuff { my $full_path_to_file = $File::Find::name; ... } my $sub_ref = \&do_stuff; #or find(\&do_stuff, $start_dir);

Do not try to cram the subroutine definition/reference into the call to find(). A lot of perl programmers favor brevity over clarity. Do not follow their lead. They have forever cursed perl as a write only language.

The full path to the file will be contained in a variable called $File::Find::name--but the "full path" will be relative to the starting directory you provide. So if you provide "." for the starting directory, then the "full paths" will look like: ./dir1/dir2/file1, etc., i.e. they won't actually be full paths. You need the full path (or a path relative to the current directory) to open the file later. If you only provide a file name, e.g. file1, perl will try to open a file called file1 in the current directory.

a)Get rid of all constructs like this: "$var_name". The quotes are extra typing and do nothing. The correct use of double quotes to interpolate a variable is for situations when you have something additional in the string, for instance:

print "The number of people was: $count.\n";

b) Poor indenting makes code hard to read. Indent 4 spaces--do not use tabs, do not use 3 spaces, do not use 6 spaces, do not use 0 spaces. For-loops are written, spaced, and indented like this in perl:

for my $val (@vals) { ... ... }

c) Putting blank lines after every line of code is poor use of spacing. Try to group related code and then use a blank line to separate other sections.

d) Do not us a pipe(|) as a separator in s/// or m/// or anything else. Use a / or braces {}, and that's it. Just because you can do something does not mean you should.

e) *You* are required to start every perl program with these lines:

use strict; use warnings; use 5.010; #if using perl 5.10+

That will require that you declare all variables with my()--which all good programmers do--or you will get an error.

f) print() and say() are your friends. Before you try to open a file, print out the file name to see if you actually have the correct full path.


In reply to Re: Help regarding a perl script by 7stud
in thread Help regarding a perl script by Anonymous Monk

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.