File Handle Problem

Discussion in 'Perl Misc' started by Louis, Sep 9, 2006.

  1. Louis

    Louis Guest

    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.
     
    Louis, Sep 9, 2006
    #1
    1. Advertising

  2. Louis

    Louis Guest

    Louis wrote:
    > 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.
     
    Louis, Sep 10, 2006
    #2
    1. Advertising

  3. Louis wrote:
    > 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
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Sep 10, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dietrich
    Replies:
    1
    Views:
    656
    Joe Smith
    Jul 22, 2004
  2. Apollyon

    File pointer to file handle

    Apollyon, May 2, 2004, in forum: C Programming
    Replies:
    5
    Views:
    952
    CBFalconer
    May 2, 2004
  3. Leon
    Replies:
    2
    Views:
    544
  4. =?ISO-8859-1?Q?KLEIN_St=E9phane?=
    Replies:
    3
    Views:
    461
    hanumizzle
    Oct 6, 2006
  5. loial

    Get file name from file handle

    loial, Feb 17, 2009, in forum: Python
    Replies:
    1
    Views:
    425
Loading...

Share This Page