1844087 Members
2728 Online
110227 Solutions
New Discussion

perl error message

 
SOLVED
Go to solution
Jeff Picton
Regular Advisor

perl error message

hello

I am trying to find out an error with a perl filehandle. I have opened the file with a valid filehandle and closed it but it tells me the file is still open.

Any suggestions ?
14 REPLIES 14
Fred Ruffet
Honored Contributor

Re: perl error message

Hi !

Could you please send us output from your script and the part that open/close the file ?

Thanks,

Fred
--

"Reality is just a point of view." (P. K. D.)
Jeff Picton
Regular Advisor

Re: perl error message

Hi

The following is a code snippet :-

$filename=$file_name{$header[7]};

$file_name is a hash list

mkdir ("temp",0775);

open (FILE2, "> temp/$filename");
binmode (FILE2);
print FILE2 $data;
close (FILE2);
}

Jeff
Fred Ruffet
Honored Contributor

Re: perl error message

And what about the error ? What does it says exactly ?

Fred
--

"Reality is just a point of view." (P. K. D.)
Jeff Picton
Regular Advisor

Re: perl error message

Hi

The error to stdout as follows :-

print() on closed filehandle main::FILE2 at ./expand_SIGWX.pl line 179.
print() on closed filehandle main::FILE2 at ./expand_SIGWX.pl line 179.
print() on closed filehandle main::FILE2 at ./expand_SIGWX.pl line 179.
expand_SIGWX.pl: Info: Completed. Number of warnings = 0

hth
Fred Ruffet
Honored Contributor

Re: perl error message

Error tells that you want to print after close.
Isn't your close statement in a loop where it shouldn't be ? I see :
close (FILE2);
}
Shouldn't it look like :
}
close (FILE2);
?

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Jeff Picton
Regular Advisor

Re: perl error message

Hi Fred

You have got the gist of the problem as I would have to post the whole code for full analysis.

Cheers

Jeff
Fred Ruffet
Honored Contributor

Re: perl error message

Can't you post it as an attachement ?

Fred

PS : Excuse my bad english : "gist" ?
--

"Reality is just a point of view." (P. K. D.)
Jeff Picton
Regular Advisor

Re: perl error message

Here is the full program attached
Jeff Picton
Regular Advisor

Re: perl error message

Hi

The reasons for this is that I am running from a test environment and the filename /temp/$filename is not created. As you see there is a hashed out comment referring to the existence of temp/$filename.
Fred Ruffet
Honored Contributor

Re: perl error message

Labels always seemed bad things to me. IMHO you should better use a return value in contruct_filename, and execute read_write_data in main loop only if return code is good...

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
R. Allan Hicks
Trusted Contributor

Re: perl error message

try changing your open statement to

open (FILE2, "> temp/$filename") or
die "Cannot open $filename";

In looking for the simpler solution, I was asking myself "Self, what would happen if the file didn't actually open?"

Seriously, IHMO any open ought to have an or die or carp or something to give you a file open indication. Should you have to misfortune to pick a directory or a filename that you don't have write permissions.

--Good Luck
"Only he who attempts the absurd is capable of achieving the impossible
Hein van den Heuvel
Honored Contributor

Re: perl error message


>> The error to stdout as follows :-
>> print() on closed filehandle main::FILE2 ...

'Clearly' it is the open that failed, not the close as initially reported.

You MUST test the open status, otherwise it will bite you later (now) in more obscure ways.

You must do so both for your input and for your output file. IMHO doing a -e existance tests is almost pointless (for input. For output it has value).
What really counts is whether you can open it, and what better way to find out about that then to actually try to open it!

So use
open (FILE1, "< $input_file") || die "Open for input $input_file";

and:
open (FILE2, "> temp/$filename") || die "Create temp/$filename";



fwiw,
Hein.
Jeff Picton
Regular Advisor

Re: perl error message

Hello

The $filename is not being created. Can anyone explain what the outcome of the following is ?

if ($header[7] ne 0 && $header[7] ne "") {
$filename=$file_name{$header[7]};

If the array @header contains the following values ?

0, 256
1, 2004
2, 7
3, 21
4, 16
5, 0
6, 291840
7, 6
8, 0
9, 0
10, 0
11, 0
12, 0
13, 0
14, 3
15, 1254
16, 0
17, 0
18, 0
19, 0
20, 0
21, 3
22, 0
23, 0
24, 0
25, 0
26, 0
27, 0
28, 0
29, 0
30, 0
31, 0
32, 0
33, 0
34, 0
35, 0
36, 0
37, 0
38, 0
39, 0
40, 0
41, 0
42, 0
43, 0
44, 0

and the $file_name is a list in a hash :-

%file_name=(
"JUFE00_YYYYMMDDHH.BUFR" => 2,
"JUVE00_YYYYMMDDHH.BUFR" => 5,
"JUBE99_YYYYMMDDHH.BUFR" => 6

Cheers

Jeff
Fred Ruffet
Honored Contributor
Solution

Re: perl error message

I don't know what it will do exactly, but $header[7] will return 6, and $file_name{$header[7]} will not return what you want.

I imagine that you want as a filename the key of the hash that corresponds to the value returned by $header[7]. To get that, you must access the hash by value. To do that, have a look at http://www.perldoc.com/perl5.8.4/pod/perlfaq4.html#How-do-I-look-up-a-hash-element-by-value-
But maybe, you'd better create your hash another way or use an array.

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)