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

I am writing a simple class to write debug info to a log (I realize there probably is a great one already, but this is the way I am learning about objects ;-)

My goal is when the "new" method is called I create a new file...then every "writelog" method, I output some data and I want it flushed.

Then when the class goes out of scope (DESTROY is called), the file is saved.

(I figure this is the best tradeoff between opening/closing the file constantly).

Do I want to put "local $|" in the "new" constructor? or should it be a global at the top of the .pm file?

Does this make sense ?

thanks !

ps

I will have 2 debug variables one in the main program writing to main.log and the other in a module.pm, writing to module.log (this is why I am learning about objects...)

Replies are listed 'Best First'.
Re: Do I need local $
by ehdonhon (Curate) on Feb 18, 2002 at 02:45 UTC

    You don't need to local $| at all. After you open your filehandle, if you want to turn on autoflush for that filehandle, you need to do something like this:

    my $oldhandle = select(FILEHANDLE); $|=1; select( $oldhandle );

    Another option (probably better) is to look at FileHandle, which includes an autoflush() method.

Re: Do I need local $
by chromatic (Archbishop) on Feb 18, 2002 at 02:24 UTC
    $| (see perlvar) affects the currently selected filehandle. You might have better luck with IO::File, which is in the core as of 5.6.1 (or earlier).

    I'm not sure what you're asking, if that doesn't help.

      If you were to use IO::File, you will find that the object that you get for the filehandle has a method available called autoflush(). This will set that property on the filehandle. Also, in your DESTROY method for your log class, you can simply undef the object reference, and let its own DESTROY method handle the closing of the file and the requisite clean-up.

      --rjray

        Infact I would be inclined to subclass IO::File ... that way you get to use all the methods of IO::File in your class :)

        /J\

        Thanks for your answer, I saw this post in a newsgroup...

        Does this mean that the file will only close when the application closes or if I manually call it?

        -----------------------------------------------------------

        > You mentioned the IO::File DESTROY method trying to cleanup the file
        > descriptors ...
        >
        > .. DESTROY method is coded to be a no-op, leaving any clean up to
        > perl's handling of the closing of a file.

        Oops, busted.

        IO::File's DESTROY is a no-op in perl 5.6 (inherited from IO::Handle).

        I didn't actually look at the DESTROY method for IO::File. I had just
        assumed that the object was being destructed properly. (Well, it wasn't
        all assumption ... there was a little empirical testing going on too.
        :-) My phrasing was mainly meant to express, "high-level cleanup seems
        to be coded correctly".

        Sorry for the confusion.



        -- Robert Trace