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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print() on closed filehandle --- But why???

Status
Not open for further replies.

canguro

Programmer
Sep 15, 2002
57
US
Can someone please steer me straight with respect to my simple-minded test? I am running this program on Win 2K, and attempting to retrieve HTML, remove the tags (which I think is now working OK), and then write the scalar direct to a file 'open'ed for OUTPUT. Here is the code:

#!/usr/bin/perl
use warnings;
use strict;
use LWP::Simple;

my $site="my $content=get $site;
my $message=$content;

$message=~s/<.+?>/ /g;

open OUTPUT, &quot;>C:\test\file01.txt&quot;;
print OUTPUT $message;
close OUTPUT;

I am getting the &quot;print() on closed filehandle&quot; message shown above. The explanation is that &quot;The filehandle you're printing on got itself closed sometime before now. Check your control flow.&quot; I'm just a newbie doing some healthy testing and I don't see where I closed the actual filehandle OUTPUT.

Any suggestions would be greatly appreciated.

Thank you
 
Change your open statement to:

open OUTPUT, &quot;>C:\test\file01.txt&quot; or die &quot;Cannot open file [C:\test\file01.txt] for writing: $!\n&quot;;

The system will hopefully give you a better explanation as to why you can't access it. It's usually something to do with permissions.


Barbie
Leader of Birmingham Perl Mongers
 
Try this:

open OUTPUT, &quot;>C:/test/file01.txt&quot;;

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Well spotted Mike. I should have written:

open OUTPUT, '>C:\test\file01.txt';

canguro, just in case you didn't follow that, the first version uses &quot;&quot;, which interpolates the '\' character, thus your filename is passed to open with <tab> and <formfeed> in place of the \t and \f. Mike's version switches to Unix style to avoid the '\'. The above version uses the '' characters to avoid interpolating the string.

Barbie
Leader of Birmingham Perl Mongers
 
Barbie and Mike,

First off, many thanks for your quick responses and the interesting suggestions, which I will try out ASAP! I also appreciate the 'straightforward' manner in which you both provided me with tips. I can tell you that I have asked questions in 1 or 2 other forums and received condescending replies, and I even got a tongue-lashing once --- despite the fact that I tried to excuse myself as a newbie in my thread!

Again, many thanks!
 
Barbie and Mike,

I have tried your suggestions but it seems I am not getting very far --- certainly because of my limited experience in Perl (sorry about that!). Here is a summary of what's happening:

1. The first command (my original one) gave a message
&quot;print() on closed filehandle OUTPUT&quot;
2. Barbie's first suggestion gave the message
&quot;Cannot open file [C: st ile01.txt] for writing:
Invalid argument&quot;
3. Mike's suggestion gave the same message
&quot;print() on closed filehandle OUTPUT&quot;
4. Barbie's last suggestion gave the message &quot;Cannot open
file [C:\test\file01.txt] for writing: $!\n&quot;

As a raw newbie I am unsure as to how to proceed. With respect to the idea of permissions, I boot up my PC on my Win 2K system without any password, since nothing has been set (i.e. nothing is required). It would seem that I should be able to write and create without restriction. By the way, I am using an editor called Open Perl IDE instead of using the DOS command-line, but I don't think that should be causing my problem ......

Any further ideas would be greatly welcomed. I'll try any/all suggestions.

Thanks again,

Joe
 
1) For some reason the file isn't getting opened. In your original version you weren't catching the error, so after the open fails you're still trying to write to a file that isn't open, hence the error message.

2) Using the die statement shows there is an invalid argument to the open call. This is because the \t and \f have been interpolated as I mentioned in my other post.

3) Mike's try failed because there is still some error with the file, that you didn't catch, so prints the same error message as (1)

4) We're getting there! But I'm surprised that the $! Perl variable doesn't contain an error string.

There is definitely something that is preventing access to the file. Check permissions on the file and directory, as that is the most likely problem.

Incidentally another way of representing the file path can be:

my $file = &quot;C:\\test\\file01.txt&quot;;
open OUTPUT, &quot;>$file&quot; or die &quot;Cannot open file [$file] for writing: $!\n&quot;;

In this case the '\' is escaping itself.


Barbie
Leader of Birmingham Perl Mongers
 
Barbie,

I think --- as you say --- we're getting there! I tried your last suggestion and, although it fails, the message seems to indicate more 'movement'. Now I'm getting this:

&quot;Cannot open file [C:\test\file01.txt] for writing: No such file or directory&quot;

This is a curious statement. I KNOW there is no such file or directory, because I am trying to create the file/directory in the Open statement.

By the way, Barbie, did you notice my comment regarding what I think are my 'implied' permissions in my previous post? Does that seem right, or am I missing the mark?

Many thanks,

Joe
 
Okay go to the directory c:\test and right click. At the bottom of the menu is 'properties', click that. You will now have a little popup box, that near the end has a section called 'Attributes'. In windows these are the permissions for the directory. If either the Read-only or Archive boxes are checked, then you cannot write to the directory. To reverse this simply uncheck the boxes and click OK. This process also applies to files.

It is also possible there is already a file in the directory, that isn't writeable, that is hidden. To check this in the folder window click Tools->Folder Options. Under the View tab there is an entry for Hidden Files. Select 'Show all hidden files and folders'.

If you haven't created the directory 'Test' you will need to do that first. Although open will create a file, it doesn't create the directory structure. Sorry if that sounds a bit like preaching to the converted, but it has occasionally been a misunderstanding of open.

Barbie
Leader of Birmingham Perl Mongers
 
Barbie,

That last line in your reply was the clue! I am sorry for being naive, but I assumed that the &quot;Open&quot; would create the directory as well as the file, but it turns out that I have to 'pre-create' the directory. I created it and the code ran fine. As I had suspected, I DO have 'create' and 'write' permission.

The file 'file01.txt' was created and I looked at it in Notepad. Out of curiosity, it seems that I wrote whatever was in $message to the output file 'as is', so in order to see the data fully I need to drag the horizontal scrollbar on the bottom way over to the right. Is there a way I can split up the output into separate lines, say no more than 60 bytes at a time?

I am pleased about our progress.

Thanks

Joe
 
Try:

my $message=$content;
$message=~s/<.+?>/ /g;
$message=~s/\s+/ /g;
$message=~s/(.{1,60}\s)/$1\n/g;

The two extra lines first compact multiple whitespcae into 1 space, then break the string into blocks of 60 (+ 1 for a space) and adds a newline after each block.


Barbie
Leader of Birmingham Perl Mongers
 
Barbie,

That suggestion you made worked out very well! I'm beginning to see some of the power of Perl and I feel inspired to try out a whole bunch of tests ....

Thank you very much.

Joe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top