|Perl: the Markov chain saw|
tripping over filehandle / subroutine usageby chexmix (Hermit)
|on Dec 21, 2012 at 20:56 UTC||Need Help??|
chexmix has asked for the
wisdom of the Perl Monks concerning the following question:
Holidayish Greetings, Monks --
Today I had to whip up a quick script, which typically makes me nervous, and I noticed something that to my still-baby-Perl eyes seemed ... well, interesting, I guess.
I had to create some reports from a couple of tab-delimited text files, so as I usually do, I opened filehandles to them:
... and same deal for a second file / handle.
I then created subroutines to create the reports, and tried passing the filehandles into the subroutines. It worked fine for the first subroutine, but when I passed the same filehandles to a SECOND subroutine ... nothing happened. No output. It was as though I had not called that second subroutine at all.
I only explicitly closed the filehandles after the second subroutine call.
Just because I got desperate, I tried creating TWO MORE filehandles to the same files, and passing those to the second subroutine. This worked.
What is happening here? Do filehandles get auto-closed when they are passed to a subroutine and the subroutine returns?
(this is probably really basic, but somehow I have never encountered it before).
UPDATE: of course, I should _think_ before I post (and post code as well)
Here is what doesn't work:
And here is what worked:
And here is the report_one sub: