in reply to Re: print() on closed filehandle WRITE_FH
in thread print() on closed filehandle WRITE_FH
Hi afoken,
I'm sorry but I am new to perl and not sure how to apply your suggestion.
Would you happen to have an example handy that you could share?
Thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: print() on closed filehandle WRITE_FH
by afoken (Chancellor) on Sep 25, 2018 at 22:50 UTC | |
Download and save this as demo.pl:
Then run it:
(Both quotes from The Fellowship of the Ring by J. R. R. Tolkien) Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-) | [reply] [d/l] [select] |
by TonyNY (Beadle) on Sep 26, 2018 at 12:28 UTC | |
Thanks Alexander for the cool examples, I'm a big LOTR fan BTW. However I'm still getting the following error:
Basically what I'm trying to do is write to a text file close the file handler then open the file for reading but even If I try to open another text file for reading I get the same error. If I'm closing the file handle after writing shouldn't I be able to open a text file and print the contents? I can open the text file for reading but when I try to print is when I get the error Please see code below
Thanks | [reply] [d/l] [select] |
by hippo (Archbishop) on Sep 26, 2018 at 13:14 UTC | |
There's something which you are obviously missing. Let's see if I can explain this in such a way as the penny drops.
The above code generates only 1 warning (intentionally as an illustration for you). See select for all the good stuff. When you run this code you will see this on the terminal:
and this in foo.txt:
Is it all clear now? It's also worth noting that using a bareword (or other global) filehandle is rarely considered best practice in these modern times so that might be the next thing you could change. Update: Fix in diagnostic for open: s/read/writ/ (thanks soonix for spotting). | [reply] [d/l] [select] |
by poj (Abbot) on Sep 26, 2018 at 12:58 UTC | |
Don't use select, just write/read to the filehandle you want poj | [reply] [d/l] |
by AnomalousMonk (Archbishop) on Sep 26, 2018 at 15:35 UTC | |
Here's another way to look at it: Solution: Don't use select: Use explicit filehandles for I/O operations. Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] |