Go Back   Velocity Reviews > Newsgroups > PERL
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

PERL - File Handle Issue

 
Thread Tools Search this Thread
Old 09-09-2006, 07:38 PM   #1
Default File Handle Issue


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:rtmess();, 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.


Louis
  Reply With Quote
Old 09-10-2006, 11:16 AM   #2
Joe Smith
 
Posts: n/a
Default Re: File Handle Issue

Louis wrote:

> 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".
  Reply With Quote
Old 09-12-2006, 09:25 AM   #3
Michael Wehner
 
Posts: n/a
Default Re: File Handle Issue

Louis wrote:
> NOW: I put the calling function in another file. When it calls the print
> function it uses &main:rtmess();, 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.

> 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.
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump