perl - Write into a file and open the same file for input -


i writing perl script below

open(fh, '>', "temp.out") or die "cannot open"; select fh;  print "hello world!"; close fh;  open (fi, "temp.out") or die "cannot open"; while ( <fi> ) {     print $_; } 

unfortunately when running script not getting "hello world!" printed. should ideal case, isn't it?

however in temp.out file can see "hello world!" printed.

i tried using variable filename, didn't work.

always use strict , warnings in scripts, have caught error encountered:

print() on closed filehandle fh @ t.pl line 10, <fi> line 1. 

your fh still selected after all, if closed it. unless have lot of print statements , can somehow isolate select (e.g. select @ start of function , restore previous default @ end) i'd it's preferable explicitly specify file handle in print statement.


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -