Syntactic Confectionery Delight | |
PerlMonks |
Re: Help regarding a perl scriptby 7stud (Deacon) |
on Dec 07, 2010 at 23:32 UTC ( [id://875913]=note: print w/replies, xml ) | Need Help?? |
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:
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:
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:
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:
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 Section
Seekers of Perl Wisdom
|
|