Connecting Tech Pros Worldwide Help | Site Map

File Handle Issue

  #1  
Old September 9th, 2006, 07:25 PM
Louis
Guest
 
Posts: n/a
Hope somebody can tell me why this happens to filehandles...

I am playing with perl packages, and I had all functions in one file.
Now I want to split them into different files using package.

BEFORE: I have a function that opens a filehandle (FH1) and then calls
another function to print messages. This works good.

NOW: I put the calling function in another file. When it calls the print
function it uses &main::prtmess();, perl prints out errors: printf() Non
unopened filehandle FH1 at.....

ANd if I copy the prtmess() function in the same file as the calling
function, it works again.


My question is: I read somewhere that once you open a filehandle, perl
keeps it open until you close it. So why the different file scenario
doesn't seem to work?

Appreciate the help. Thanks.
  #2  
Old September 10th, 2006, 11:05 AM
Joe Smith
Guest
 
Posts: n/a

re: File Handle Issue


Louis wrote:
Quote:
BEFORE: I have a function that opens a filehandle (FH1) and then calls
another function to print messages. This works good.
>
NOW: I put the calling function in another file.
In that case, you should store the file handle in a variable,
instead of using "FH1".
  #3  
Old September 12th, 2006, 09:15 AM
Michael Wehner
Guest
 
Posts: n/a

re: File Handle Issue


Louis wrote:
Quote:
NOW: I put the calling function in another file. When it calls the print
function it uses &main::prtmess();, perl prints out errors: printf() Non
unopened filehandle FH1 at.....
Firstly, it's probably not a good idea to be splitting these functions
across packages. If they have such interrelated functionality, why split
them up at all? And certainly if you have to split them up, they
shouldn't both be operating on a global filehandle, nor will that
produce readable and maintainable code.

A better alternative would be to have one subroutine to open a
filehandle and read from it, and another subroutine to handle the
printing. And if you insist on operating directly on the filehandle, you
should be using a scalar to hold the filehandle--not a bareword--which
could then be passed to another function.
Quote:
My question is: I read somewhere that once you open a filehandle, perl
keeps it open until you close it. So why the different file scenario
doesn't seem to work?
Read documentation on Perl's symbol tables. Until then, stick with
scalars as filehandles.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sending a path to a FILE control ( it has to be possible! ) CHIN@KD answers 20 November 22nd, 2005 05:53 PM
Sending a path to a FILE control ( it has to be possible! ) CHIN@KD answers 20 November 19th, 2005 04:19 AM
Sending a path to a FILE control ( it has to be possible! ) CHIN@KD answers 20 July 21st, 2005 09:48 PM
session_OnEnd dans le global.asa never fired Olivier SOW answers 6 July 21st, 2005 08:45 AM
Reading a file VooDoo answers 29 July 17th, 2005 09:33 AM