This is just dangerous and wrong:
open(SuitDoc,<STDIN>) || die "Cannot open file";
Why it's wrong: it evaluates STDIN in list context so all lines in STDIN get appended as parameters to open().

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.


In reply to Re: Searching Line by Line Multiple Times by Joost
in thread Searching Line by Line Multiple Times by Dr.Avocado

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.