File Handle Problem

L

Louis

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.
 
L

Louis

Louis said:
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.
Thanks for your suggestions...with those I finally realized that the
error iss I was so concentrating on keeping the package names properly
on the functions but not on the filehandle FH1. Once I added the package
name in front of FH1 (packagename::FH1), the script works.

But now I have to figure out how to use the same prtmess function for
all the reports if some of them are in different packages.

I thought that yuou cannot pass the filehandle directly as a parameter
to a function. Maybe I can pass a reference of FH1 to the function.

Anyway, thanks very much for your input.
 
J

John W. Krahn

Louis said:
Thanks for your suggestions...with those I finally realized that the
error iss I was so concentrating on keeping the package names properly
on the functions but not on the filehandle FH1. Once I added the package
name in front of FH1 (packagename::FH1), the script works.

But now I have to figure out how to use the same prtmess function for
all the reports if some of them are in different packages.

I thought that yuou cannot pass the filehandle directly as a parameter
to a function. Maybe I can pass a reference of FH1 to the function.

Sure you can, see the FAQ:

perldoc -q "How do I pass filehandles between subroutines"



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top