Don't ask to ask, just ask | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Without going into the golfish ways of doing this, but sticking to script form, there's a whole lot of things you can improve.
I would prefer to write this like so:
and later test using exists $skip_for{$ext} Next note: you can just use $File::Find::name rather than "$File::Find::dir/$_" Then we have a case of redundant syntax: in \&{ sub { ... } } the sub{ ... } already gives you a reference. Then your &{} goes and dereferences it, only to feed it back to the \ which makes a reference from the result again. You can drop the surrounding \&{} and simply write sub { ... } here. I am a bit puzzled by this: my ($nil,$ext) = $file =~ /^(.*?)\.(.*?)$/gs;If you throw away the first capture, why capture at all? my ($ext) = $file =~ /^.*?\.(.*?)$/gs;which is better written as my ($ext) = $file =~ /[.]([^.]+)$/gs;(In words: I want as many non-dot characters as there are in front of the end of the string, update: but only if there's a dot in the filename.) The $ext = '' unless defined $ext; can be avoided if you put the $skip{$ext} inside an if(/match here/) Lastly, since you're not interested in the individual lines of your input, but separating the input costs effort, it would be better to unconditionally slurp large chunks of X bytes instead. The next point is a maneuvre critique. Why would one first fetch a list of directories and then go and read each directory manually, when the same first search already gives you all the file names on a silver plate? (And why are counting something, when you never use that count? :-)) And lastly, rather than hardcode the directory in the script, it's preferrable to take them as parameters from the commandline. So here's an updated version:
Further improvement might be to use some Getopt:: module to allow the user to change the $skip_for rules. Update: I must have been asleep as well. Kudos to Zaxo for pointing out my regex would return the whole filename for extensionless files. Also, I need to go flaggelate myself for a while: and, of course, since this is a sub, not a for loop. I feel stupid now. Oh well, guess we can feel stupid together. :-) Makeshifts last the longest. In reply to Re: Recursive File Substitution
by Aristotle
|
|