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

I typically open a log file for writing at the top of my program and close it on error or on normal program exit. PerlCritic complains that I am not closing the file soon after opening. Surely I should not
open the file write the text close the file
to satisfy PerlCritic

Replies are listed 'Best First'.
Re: leave log files open
by toolic (Bishop) on Jun 12, 2013 at 13:58 UTC
    If you find Perl::Critic useful in general, and you want to continue to use it, keep in mind that it is highly configurable. You can disable policies that you disagree with using the .perlcriticrc file, for example.

    I think the policy in question is Perl::Critic::Policy::InputOutput::RequireBriefOpen. The POD describes the reason for the policy, some alternative coding styles and how to configure it differently. You can decide whether it applies to your case or not.

      settles that! thanks
Re: leave log files open
by BrowserUk (Patriarch) on Jun 12, 2013 at 13:28 UTC
    Surely I should not open the file write the text close the file to satisfy PerlCritic

    No, you shouldn't. You should not do anything dumb in order to satisfy that dumb tool.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Please note, when I use the phrase "dumb tool", I mean that the tool itself is dumb, as in has no intelligence built in.

      Using the tool is not dumb if you use it as it is intended to be used; which is to say you chose the policies that you find useful and use the tool to double check yourself.

      Where it goes wrong is when people modify their code to "satisfy" policies that they obviously disagree with.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      that's one for, one against

        Perhaps this will swing it for you?


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: leave log files open
by Happy-the-monk (Canon) on Jun 12, 2013 at 13:13 UTC

    Why not?

    I close my log files often on the basis of it not requiring that much IO...
    but providing safety of the information therein in case of unforseeable behavior of external components*.

    Cheers, Sören

    *) like databases running out of space, admins killing my processes and other havoc.

    (hooked on the Perl Programming language)

      Why not?

      Here's one reason:

      cmpthese -1, { a=>q[ open O, '>', 'log'; for(1..10000){ print O chr(0)x80; } close O; ], b=>q[ for(1..10000){ open O, '>>','log'; print O chr(0)x80; close O; } ] };; (warning: too few iterations for a reliable count) s/iter b a b 1.30 -- -99% a 8.42e-003 15300% --

      A second reason is that the first consumes almost no resource; whereas the second consumes prodigious amounts of it.

      The times before and after the first:

      a 5.546|5.343|0|0 a 5.546|5.343|0|0

      And the second:

      b 5.578|5.343|0|0 b 5.734|6.375|0|0

      Whether you are just impacting every other process on your local server; or being billed for all that addition CPU & IO when running your process in the cloud; incurring 1000's of times more resource use in order to satisfy a dumb tool that is easily silenced is .... Its your dollar, you pick the adjective.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.