in reply to Opening an array of file names

toolic has identified the basic problem. These ramblings are for your consideration (in light of the self-characterization in your home node):

You appear to be passing variables --$count and $newPrices{key} -- into getFiles() as arguments to the call. By doing it that way, your sub changes their values in main and in the sub. For example, if the first file has three lines and the second has four, and if the value of $count in main is 0 on entry to the sub, its value will be 7 when you return from the sub. That's probably a non-issue in the particular case, but it's a behavior of which you want to be aware.

You've also written a sub with no explicit return which means the sub may do something you're not expecting, as explained in perlsub:

"If no "return" is found and if the last statement is an expression, its value is returned."

All in all, then, I commend a close reading of perlsub re return and also with special attention to the explanation of passing variables by reference.

Update: clarifiation, p2, sentence 2 and fixed markup.

Replies are listed 'Best First'.
Re^2: Opening an array of file names
by tnyflmngs (Acolyte) on Apr 04, 2012 at 03:16 UTC

    Thanks for your input. I do know that a sub with no explicit return, it could create some unexpected return value. The subroutine is supposed to make changes to global hashes and I am not really relying on the return, so I am not too bothered by it. The  $count variable is a global in this case and I want to count all lines in the files. I made a few pretty drastic changes today, and I will continue to do that as I learn more stuff. One of the things I did was to reduce the number of global variables. I hope to eliminate global variables after I read what you suggested.