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!

Close a filehandle from sub

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Joined
Feb 6, 2002
Messages
1,851
Location
IL
HI,

I open a filehandle and use it in one sub.
Then in another sub I want to be able to close that file accessing it's handle.
But if I try to acces that handle,I get error "Bad file descriptor".
Looks like handles are not passed between subs.
Appreciate any advise.
Thanks

Long live king Moshiach !
 
You'll probably want to use references - something like this might work for you:
Code:
my $fh = openOutputFile("test_output.txt");

print $fh "testing 1 2 3\n";

closeFilehandle($fh);

sub openOutputFile {
	my $file = shift;
	open (INPUT, "> $file") or die "cannot open $file\n$!";
	return *INPUT;
}

sub closeFilehandle {
	my $filehandle = shift;
	close $filehandle or die "Cannot close filehandle\n$!";
}
 
or use the IO::File module.

- Kevin, perl coder unexceptional!
 
Any method of making file handles globally available will work ok.

Mike

The options are: fast, cheap and right - pick any two.. [orientalbow] & [anakin]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884
 
yes, but really it's just a "frontend" (the module authors wording) for IO:File, IO::Handle and IO::Seekable.

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top