Perl - work with open files or write to @lists first?

I am dealing will many thousand fairy small files.
I need to search them for various matches and depending on what I find, may need to search some files again for additional matches.

Generally speaking, is it better to write a txt file to an @array/@list and then work with it (multiple searches within it, creating $vars etc.) or open the file and go through it few times using while?

ie. Say I am searching through a file for A, B and C.
Depending what I find, I may then need to look in the same file for 1, 2, 3. Also, I will not always find A, B and C in that order.... it could be B, A, C etc.

I could just open the file and use while to look line by line for matches and depending on what I find I could use another (or more) while to go through the file again and close it when done.

OR, I could write the file to @list and search it for what I want.

Depending on what I find, some files will need a lot of searching, some very little.

Is there a rule of thumb for which approach is faster / easier on resources when dealing with thousands of files?

Thanks,
-OG-

Opening a file takes time. So if possible, you should open each file only once (or the minimum number of times you can) and get everything you might need and save it in memory (in variables).

OTOH, I understand that the way Linux works now is to use all unused RAM as cache. So if you open a file, search it and close it, the whole file will stay cached in RAM until some other allocation causes it to be discarded. The result is that the second open will probably be almost instantaneous.

Of course, if you have many more files than will fit in your available RAM, then you'll lose the cache advantage.