I have recently started learning Perl and one of my latest assignments involves searching a bunch of files for a particular string. The user provides the directory name as an argument and the program searches all the files in that directory for the pattern. Using readdir() I have managed to build an array with all the searchable file names and now need to search each and every file for the pattern, my implementation looks something like this –
sub searchDir($) { my $dirN = shift; my @dirList = glob('$dirN/*'); for(@dirList) { push @fileList, $_ if -f $_; } @ARGV = @fileList; while(<>) { ## Search for pattern } }
My question is – is it alright to manually load the @ARGV array as has been done above and use the <> operator to scan in individual lines or should I open / scan / close each file individually? Will it make any difference if this processing exists in a subroutine and not in the main function?
On the topic of manipulating @ARGV – that’s definitely working code, Perl certainly allows you to do that. I don’t think it’s a good coding habit though. Most of the code I’ve seen that uses the ‘while (<>)’ idiom is using it to read from standard input, and that’s what I initially expect your code to do. A more readable pattern might be to open/close each input file individually:
That would read more easily to me, although it is a few more lines of code. Perl allows you a lot of flexibility, but I think that makes it that much more important to develop your own style in Perl that’s readable and understandable to you (and your co-workers, if that’s important for your code/career).
Putting subroutines in the main function or in a subroutine is also mostly a stylistic decision that you should play around with and think about. Modern computers are so fast at this stuff that style and readability is much more important for scripts like this, as you’re not likely to encounter situations in which such a script over-taxes your hardware.
Good luck! Perl is fun. 🙂
Edit: It’s of course true that if he had a very large file, he should do something smarter than slurping the entire file into an array. In that case, something like this would definitely be better:
The point when I wrote ‘you’re not likely to encounter situations in which such a script over-taxes your hardware’ was meant to cover that, sorry for not being more specific. Besides, who even has 4GB hard drives, let alone 4GB files? 😛
Another Edit: After perusing the Internet on the advice of commenters, I’ve realized that there are hard drives that are much larger than 4GB available for purchase. I thank the commenters for pointing this out, and promise in the future to never-ever-ever try to write a sarcastic comment on the internet.