Home > Archive > PERL Beginners > September 2004 > How to check the FILE HANDLE STATUS ???
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to check the FILE HANDLE STATUS ???
|
|
| Madhu Reddy 2004-09-24, 3:59 pm |
| hi,
I want to check the status of File handle before
reading/writing to file ? How to do this ?
like following
open(FH_IN_FILE, ">file.txt");
# This statement is executed by some other function
close(FH_IN_FILE);
print FH_IN_FILE "SOME DATA";
here before writing to file, i want to check the
status of FH_IN_FILE..(whether file is opened or
closed )
Thanks
__________________________________
Do you Yahoo!?
Yahoo! Mail is new and improved - Check it out!
http://promotions.yahoo.com/new_mail
| |
| Wiggins d Anconia 2004-09-24, 3:59 pm |
| > hi,
> I want to check the status of File handle before
> reading/writing to file ? How to do this ?
>
> like following
>
> open(FH_IN_FILE, ">file.txt");
>
Which itself is a bad idea, always check that the open succeeded in the
first place,
open FH_IN_FILE, ">file.txt" or die "Can't open file for writing: $!";
> # This statement is executed by some other function
> close(FH_IN_FILE);
>
Iffy, but ok.
>
> print FH_IN_FILE "SOME DATA";
>
> here before writing to file, i want to check the
> status of FH_IN_FILE..(whether file is opened or
> closed )
>
perldoc -f fileno
Is about the only way to check (IIRC). The only way to know whether a
file can really be written to is to try and catch any errors that occur.
> Thanks
>
http://danconia.org
| |
| Dave Gray 2004-09-24, 8:56 pm |
| > open(FH_IN_FILE, ">file.txt");
>
> # This statement is executed by some other function
> close(FH_IN_FILE);
>
> print FH_IN_FILE "SOME DATA";
>
> here before writing to file, i want to check the
> status of FH_IN_FILE..(whether file is opened or
> closed )
You could do something like the following:
-----
#!/usr/local/bin/perl -w
use strict;
my %fhs;
# open the filehandle, store in hash
open $fhs{one}, '> test1.txt' or die "couldn't write file: $!\n";
# function takes hashref and name of which filehandle to use
sub blah {
my ($fh, $which) = @_;
close $fh->{$which};
# delete the filehandle when you close it
delete $fh->{$which};
}
blah(\%fhs, 'one');
# it's closed if it's gone from the hash
close $fhs{one} if exists $fhs{one};
-----
There might be a better way to do whatever you're trying to do,
though. What's the bigger problem you're trying to solve?
Dave
| |
| John W. Krahn 2004-09-24, 8:56 pm |
| Madhu Reddy wrote:
> hi,
Hello,
> I want to check the status of File handle before
> reading/writing to file ? How to do this ?
>
> like following
>
> open(FH_IN_FILE, ">file.txt");
>
> # This statement is executed by some other function
> close(FH_IN_FILE);
>
>
> print FH_IN_FILE "SOME DATA";
>
> here before writing to file, i want to check the
> status of FH_IN_FILE..(whether file is opened or
> closed )
if ( defined fileno FH_IN_FILE ) {
print FH_IN_FILE "SOME DATA";
}
else {
warn "Error: FH_IN_FILE is closed!\n";
}
John
--
use Perl;
program
fulfillment
|
|
|
|
|