Marknel has asked for the wisdom of the Perl Monks concerning the following question:

Being realtively new to Perl as a whole I ask your paitence with this request for guidance. I'm attempting to log things at certian points of a program. This program contains a subroutine that is called at least twice during this program. At the end of each run of the subroutine, I would like to print the results to the programs logfile. The problem I'm running into is that when I open the filehandle to the logfile in the main body of the program, the subroutine does not seem to be able to access that open file handle. I've read the perl doc on open, but either I didn't find anything relating to the Global use of a open filehandle. Where should I be looking for the solution?

Replies are listed 'Best First'.
Re: Global filehandles?
by pizza_milkshake (Monk) on Oct 19, 2004 at 18:25 UTC
    could you provide some pared-down example code?

    incidentally, i have found the Log::LogLite module helpful for logging

    perl -e"\$_=qq/nwdd\x7F^n\x7Flm{{llql0}qs\x14/;s/./chr(ord$&^30)/ge;print"

Re: Global filehandles?
by pg (Canon) on Oct 19, 2004 at 18:25 UTC

    Should not have a problem. (Obviously you can wrap this in a class.)

    use strict; use warnings; open_log(); a(); b(); close_log(); sub a { logging("Entering a\n"); logging("Entering a\n"); } sub b { logging("Entering b\n"); logging("Entering b\n"); } sub logging { print LOG shift; } sub open_log { open(LOG, ">", "log"); } sub close_log { close LOG; }

    This gave me:

    Entering a Entering a Entering b Entering b

      You ought to check open(), print(), and close() for success. Also, did you mean the second logging() calls in the subs to be "Leaving ..." rather than "Entering ..."?

      ihb

      See perltoc if you don't know which perldoc to read!
      Read argumentation in its context!

Re: Global filehandles?
by sth (Priest) on Oct 19, 2004 at 18:26 UTC

    It would be easier to help if we could see some code. Are sure that you didn't make a lexical filehandle?

    i.e. open(my $fh, ">logfile")

    ...or did you localize the filehandle, 'local IN', ?

      After typing out an example, I noticed what my problem was. I had the open nested in an IF that rarely would be used.] God knows what I was thinking and I'm sorry for wasting your time. :)

        If it helped you solve the problem, even indirectly, it wasn't wasted time. Sometimes it just takes a second set of eyes or a question from somebody else to help you see the solution/problem. Funny how that works... =8^)