in reply to Help regarding a perl script
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Help regarding a perl script
by ikegami (Patriarch) on Dec 08, 2010 at 00:36 UTC |