in reply to Searching Line by Line Multiple Times
Why it's wrong: it evaluates STDIN in list context so all lines in STDIN get appended as parameters to open().open(SuitDoc,<STDIN>) || die "Cannot open file";
It's also the wrong approach if you want to iterate over the supplied list of files multiple times, since usually you can't seek back on STDIN.
Why it's dangerous: if STDIN contains something like ">/etc/passwd" or any other "magic" character it can cause the script to overwrite files or execute unsuspected programs. Use the 3 argument form of open to be sure what you're doing and check if the open() call succeeds:
open my $filehandle,"<",$filepath or die "Can't open $filepath for reading: $!"
You could store the content of STDIN in an array and iterate over that multiple times, but it's going to be quicker to check each line for each kind of suit and store the counts for each suit in 4 variables (or hash values) and print out the results after you've read all files. It only takes a couple of bytes to store all that info and you only have to read through all input files 1 time instead of 4. Note that disk IO is going to be by far the limiting factor in performance for this kind of task.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Searching Line by Line Multiple Times
by ikegami (Patriarch) on Aug 02, 2007 at 02:22 UTC |