Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about -w 3

Status
Not open for further replies.

garymgordon

Programmer
Apr 5, 2000
307
US
I have an example I am looking at that says:


#!/usr/bin/per -w

open(OUT, &quot;<test.txt&quot;);
print (OUT &quot;This is text.\n&quot;);
close(OUT);

Output is:

print on closed filehandle main::OUT at test.pl line 4, <STDIN> chunk 2.

I don't understand the output.

Can you explain why, when using -w on this code, it would generate this warning. And, please explain what each piece of the warning is trying to tell me??

Thanks,
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
when you use < in ope(OUT, &quot;<test.txt&quot;) you open the file as read-only, therefore writing to it won't work, and generates a warning.

As a general tip, any time you have trouble with a function, do the following from a shell:
perldoc -f print
or whatever the function is, in this case the problem is very obvious.
Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Can you explain this statement:

print on closed filehandle main::OUT at test.pl line 4, <STDIN> chunk 2.

Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
I just did! re-read the previous post! You haven't opend the file for writing! Look at the perldoc for open!
Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Well, I mean ...

print on closed filehandle main::OUT at test.pl line 4, <STDIN> chunk 2.


when it says &quot;print on closed filehandle main::OUT&quot; ... I don't understand why it is saying &quot;print on closed filehandle&quot;. I understood what YOU said ... but I am trying to see it in direct relationship to what it printed out.

Also, ... when it says &quot;<STDIN> chunk 2

Why did it print out &quot;<STDIN>&quot; .. and what do they mean when they say &quot;chunk 2&quot; mean?

Thanks,

Gary Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
the choice of the term 'closed' on the part of the error message may not have been the best in this situation, since it seems remeniscent of the function 'close', but really, it's refering to the state of an IO pipe, one which has two ends, one for writing, one for reading. in this case, the one for writing is closed.
as for 'STDIN chunk 2', i don't know, but it's probly something to do with the way perl was trying to handle writing to a file that was closed for writing. change you 'open'ing statement to one that is either R/W or is writable... &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Think of it like this:

trying to write on a filehandle that is read-only has the same end result as trying to write to a close filehandle, nothing. It's just a convention of saying that the mode of the file is not write, it may be read-only, it may be closed, but it don't work...

MWB.
Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
I think the &quot;<STDIN> chunk 2&quot; part refers to the INPUT to the perl compiler, i.e. YOUR PROGRAM. It's trying to tell you where it found the error.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top