My error log is growing dangerously large...

Discussion in 'Perl Misc' started by Nigel, May 15, 2009.

  1. Nigel

    Nigel Guest

    Hi there,

    I'm by no means a newbie, but I've obviously missed something very
    basic in my learning of Perl (entirely self-taught). I have a problem
    with a web-based database I've written using Perl. The apache
    error_log grows at the rate of several hundred Mb per day! I've looked
    at the log contents and most of the messages refer to the use of
    uninitialized values - for example in concatenations of strings.

    I have three questions:

    1. Do I understand from this that in Perl every variable should be
    initialised - not simply defined - before it is referenced? I use
    strict and all my varaibles are defined thus:

    my $foo;

    Should I be doing this instead:

    my $foo = "";

    2. Given that I've already written over seventy programs, many of
    which are many thousands of lines long, is there a safe way of
    stopping the error log filling up in this way...without going back and
    making major changes to all my programs?

    3. My programs are working fine or at least doing what I would expect
    them to do. So is there actually a problem in refering to
    uninitialized variables - other than the one I am specifically asking
    about?

    Many thanks in advance for your advice/comments (hopefully none too
    rude!)

    Nigel
    Nigel, May 15, 2009
    #1
    1. Advertising

  2. Nigel

    j.keßler Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Nigel wrote:
    > Hi there,
    >
    > I'm by no means a newbie, but I've obviously missed something very
    > basic in my learning of Perl (entirely self-taught). I have a problem
    > with a web-based database I've written using Perl. The apache
    > error_log grows at the rate of several hundred Mb per day! I've looked
    > at the log contents and most of the messages refer to the use of
    > uninitialized values - for example in concatenations of strings.
    >
    > I have three questions:
    >
    > 1. Do I understand from this that in Perl every variable should be
    > initialised - not simply defined - before it is referenced? I use
    > strict and all my varaibles are defined thus:
    >
    > my $foo;
    >
    > Should I be doing this instead:
    >
    > my $foo = "";
    >
    > 2. Given that I've already written over seventy programs, many of
    > which are many thousands of lines long, is there a safe way of
    > stopping the error log filling up in this way...without going back and
    > making major changes to all my programs?
    >
    > 3. My programs are working fine or at least doing what I would expect
    > them to do. So is there actually a problem in refering to
    > uninitialized variables - other than the one I am specifically asking
    > about?
    >
    > Many thanks in advance for your advice/comments (hopefully none too
    > rude!)
    >
    > Nigel

    I can only tell something about the webserver part.

    Normally the log rotationg service which is configured as a service in your
    server should handle the archiving of the log files. The webserver service
    itself does this not.

    So it sounds like that the log handling service is not working.

    such servcies are syslogd and klogd or even metalog

    j.keßler
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.11 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iEUEARECAAYFAkoNdogACgkQE++2Zdc7EtdMPACTBFvI+UKpDVGxXPPNM0e1DTk3
    fwCgqvBrNVkXxd8XICHWDuL0nVzOOGw=
    =z3uD
    -----END PGP SIGNATURE-----
    j.keßler, May 15, 2009
    #2
    1. Advertising

  3. Nigel <> wrote:
    >[...]The apache
    >error_log grows at the rate of several hundred Mb per day! I've looked
    >at the log contents and most of the messages refer to the use of
    >uninitialized values[...]
    >
    >2. Given that I've already written over seventy programs, many of
    >which are many thousands of lines long, is there a safe way of
    >stopping the error log filling up in this way...without going back and
    >making major changes to all my programs?


    As a stop-gap measure you could disable this particular warning in your
    programs until you can fix the actual code problems. That would require
    only one single consistant modification in every program.

    >3. My programs are working fine or at least doing what I would expect
    >them to do. So is there actually a problem in refering to
    >uninitialized variables - other than the one I am specifically asking
    >about?


    Possibly. "Unitialized value" doesn't only come from undefined variables
    but in my experience more often from edge case where all of a sudden a
    variable value is no longer defined in the last loop or first loop or
    failed pattern match or similar logic or coding errors.

    jue
    Jürgen Exner, May 15, 2009
    #3
  4. Glenn Jackman wrote:
    > At 2009-05-15 09:13AM, "Nigel" wrote:
    >> 1. Do I understand from this that in Perl every variable should be
    >> initialised - not simply defined - before it is referenced? I use
    >> strict and all my varaibles are defined thus:
    >>
    >> my $foo;
    >>
    >> Should I be doing this instead:
    >>
    >> my $foo = "";

    >
    > IMO you should either initialize your variables or at least check your
    > variables are defined() before referencing them.
    > print $foo if defined $foo;
    >
    >> 2. Given that I've already written over seventy programs, many of
    >> which are many thousands of lines long, is there a safe way of
    >> stopping the error log filling up in this way...without going back and
    >> making major changes to all my programs?

    >
    > If you don't want to fix your programs, turn off warnings.


    Or maybe just

    no warnings 'uninitialized';

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, May 15, 2009
    #4
  5. Nigel <> wrote:

    > The apache
    > error_log grows at the rate of several hundred Mb per day! I've looked
    > at the log contents and most of the messages refer to the use of
    > uninitialized values - for example in concatenations of strings.



    If you had written the programs to be "warnings clean", then you
    would not be experiencing this problem.

    You should not count development completed until your code is warnings clean.


    > I have three questions:
    >
    > 1. Do I understand from this that in Perl every variable should be
    > initialised - not simply defined - before it is referenced?



    No you should not need to initialize every variable. The fact that
    a variable has not been initialized (defined) is useful for discovering
    edge cases and other unexpected bugs.

    No, unitialized values do not emit warnings when they are referenced:

    ------------------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    my $var;

    if ( $var ) {
    print "$var is true\n";
    }

    my $foo = $var;
    ------------------------

    $var is uninitialized and is referenced twice, yet there are no warnings.

    Uninitialized values (not necessarily in a variable) emit a warning
    when "used as if it were already defined", as it says in perldiag
    for that particular message.


    > I use
    > strict and all my varaibles are defined thus:
    >
    > my $foo;



    That does NOT "define" a variable.

    That "declares" a variable.

    The variable's value is still undef (uninitialized).


    > Should I be doing this instead:
    >
    > my $foo = "";



    Whether you should do that or not depends on what $foo is expected
    to signify.

    Leaving it undef is a common way of signalling some unexpected condition
    for example.


    > 2. Given that I've already written over seventy programs, many of
    > which are many thousands of lines long, is there a safe way of
    > stopping the error log filling up in this way...without going back and
    > making major changes to all my programs?



    Since it would be "major changes" then we should assume that these
    warnings are coming from many different lines in your programs?

    It may be the case that it is only a few lines inside of a loop that
    iterates many times, in which case it would not be very painful to
    fix properly.

    I have written thousands of Perl programs, *all* of them designed to
    never emit an uninitialized value message, so that when I _do_ get
    such a message, I'll realize that something happened that I did not
    consider when I wrote the program (i.e. it announces that I have a bug).


    > 3. My programs are working fine or at least doing what I would expect
    > them to do. So is there actually a problem in refering to
    > uninitialized variables -



    Sometimes there is and sometimes there isn't.

    If, upon inspecting the code that is generating a particular warning,
    you determine that it is not really a problem, that is, you expect
    that there are at least some cases what a value will never be
    assigned, then you should insert code that avoids the warning.

    There are several ways to do this, I will go to quite some trouble to
    avoid the last one:

    my $sum = 0; # define a default value
    ... # here is code that only _might_ modify $sum
    print "sum=$sum\n"; # zero = right answer, empty string = wrong answer

    $sum = 0 unless defined $sum; # same thing, only later in the program
    print "sum=$sum\n";

    { # for some odd reason, a sum of the empty string is reasonable...
    no warnings 'uninitialized';
    print "sum=$sum\n";
    }

    In that case you are never using an undefined value as if it was defined,
    so you get no spurious warnings.

    Simply ignoring the warnings makes in highly likely that a use that
    is NOT what you were expecting will go unnoticed. Your bug will live on...


    > other than the one I am specifically asking
    > about?



    You are not asking about a specific uninitialized value warning,
    you are asking generally about a lot of such warnings.

    If you want a comment on a specific case, then please post code
    containing a specific case for us to comment on.


    > Many thanks in advance for your advice/comments



    You're welcome. I hope it helps.


    > (hopefully none too
    > rude!)



    Your feet stink!

    (I don't take direction very well.)


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, May 15, 2009
    #5
  6. Nigel

    Danish Guest

    On May 15, 5:47 pm, Gunnar Hjalmarsson <> wrote:
    > Glenn Jackman wrote:
    > > At 2009-05-15 09:13AM, "Nigel" wrote:
    > >>  1. Do I understand from this that in Perl every variable should be
    > >>  initialised - not simply defined - before it is referenced? I use
    > >>  strict and all my varaibles are defined thus:

    >
    > >>  my $foo;

    >
    > >>  Should I be doing this instead:

    >
    > >>  my $foo = "";

    >
    > > IMO you should either initialize your variables or at least check your
    > > variables are defined() before referencing them.
    > >     print $foo if defined $foo;

    >
    > >>  2. Given that I've already written over seventy programs, many of
    > >>  which are many thousands of lines long, is there a safe way of
    > >>  stopping the error log filling up in this way...without going back and
    > >>  making major changes to all my programs?

    >
    > > If you don't want to fix your programs, turn off warnings.

    >
    > Or maybe just
    >
    >      no warnings 'uninitialized';
    >
    > --
    > Gunnar Hjalmarsson
    > Email:http://www.gunnar.cc/cgi-bin/contact.pl- Hide quoted text -
    >
    > - Show quoted text -


    I think I know how to turn off warnings : remove the -w in the first
    line of each program, is that right?

    But where/how would I implement: no warnings 'uninitialised'?

    Thanks for all the help so far.

    Nigel
    Danish, May 15, 2009
    #6
  7. Danish <> wrote:
    >I think I know how to turn off warnings : remove the -w in the first
    >line of each program, is that right?


    Rather by commenting out or removing the line

    use warnings;

    "-w" has been obsolete for quite some time ;-)

    >But where/how would I implement: no warnings 'uninitialised'?


    At the beginning of the scope, for which you want to disable the
    warning(s).

    jue
    Jürgen Exner, May 15, 2009
    #7
  8. Tad J McClellan <> wrote:
    >If you had written the programs to be "warnings clean", then you
    >would not be experiencing this problem.
    >You should not count development completed until your code is warnings clean.


    While I agree with that statement in general you should remember that
    those warnings often appear in edge cases which may have been missed
    during regular testing.
    Although, I have to admit that if there are megabytes of those warnings
    being generated then it must have been a very frequent edge case.

    jue
    Jürgen Exner, May 15, 2009
    #8
  9. Nigel

    Dr.Ruud Guest

    Jürgen Exner wrote:

    > use warnings;
    >
    > "-w" has been obsolete for quite some time ;-)


    Not really. I for example like "-w" in the shebang of my scripts,
    because it enforces warnings into the used modules.

    --
    Ruud
    Dr.Ruud, May 15, 2009
    #9
  10. "Dr.Ruud" <> wrote:
    >Jürgen Exner wrote:
    >
    >> use warnings;
    >>
    >> "-w" has been obsolete for quite some time ;-)

    >
    >Not really. I for example like "-w" in the shebang of my scripts,
    >because it enforces warnings into the used modules.


    I agree, sometimes it has its uses if you know what you are doing.

    In your example its e.g. an easy way to force your developers to get
    their modules clean. On the other hand it could also generate a slew of
    nasty warnings if you are using an unclean third-party module where you
    have no control over it.
    It all depends....

    jue
    Jürgen Exner, May 15, 2009
    #10
  11. Nigel

    Dr.Ruud Guest

    Jürgen Exner wrote:

    > [ -w ]
    > its e.g. an easy way to force your developers to get
    > their modules clean. On the other hand it could also generate a slew of
    > nasty warnings if you are using an unclean third-party module where you
    > have no control over it.


    No control?
    ;)

    --
    Ruud
    Dr.Ruud, May 15, 2009
    #11
  12. Nigel

    Thrill5 Guest

    "Danish" <> wrote in message
    news:...
    On May 15, 5:47 pm, Gunnar Hjalmarsson <> wrote:
    > Glenn Jackman wrote:
    > > At 2009-05-15 09:13AM, "Nigel" wrote:
    > >> 1. Do I understand from this that in Perl every variable should be
    > >> initialised - not simply defined - before it is referenced? I use
    > >> strict and all my varaibles are defined thus:

    >
    > >> my $foo;

    >
    > >> Should I be doing this instead:

    >
    > >> my $foo = "";

    >
    > > IMO you should either initialize your variables or at least check your
    > > variables are defined() before referencing them.
    > > print $foo if defined $foo;

    >
    > >> 2. Given that I've already written over seventy programs, many of
    > >> which are many thousands of lines long, is there a safe way of
    > >> stopping the error log filling up in this way...without going back and
    > >> making major changes to all my programs?

    >
    > > If you don't want to fix your programs, turn off warnings.

    >
    > Or maybe just
    >
    > no warnings 'uninitialized';
    >
    > --
    > Gunnar Hjalmarsson
    > Email:http://www.gunnar.cc/cgi-bin/contact.pl- Hide quoted text -
    >
    > - Show quoted text -


    )I think I know how to turn off warnings : remove the -w in the first
    )line of each program, is that right?
    )
    )But where/how would I implement: no warnings 'uninitialised'?
    )
    )Thanks for all the help so far.
    )
    )Nigel

    use warnings;
    no warnings 'uninitialized';

    Note that 'uninitialized' is the American English spelling using a 'z', not
    the Kings English with an 's'.
    Thrill5, May 16, 2009
    #12
    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. Mahesh Prasad
    Replies:
    1
    Views:
    704
    Tom Wells
    Feb 22, 2004
  2. seabass

    reading a growing log file

    seabass, Oct 5, 2003, in forum: Perl
    Replies:
    1
    Views:
    482
    seabass
    Oct 5, 2003
  3. Clement
    Replies:
    7
    Views:
    1,614
    Kevin Spencer
    Jul 3, 2003
  4. Aspiring .NET Programmer
    Replies:
    6
    Views:
    393
    Aspiring .NET Programmer
    Oct 4, 2006
  5. Vear
    Replies:
    0
    Views:
    124
Loading...

Share This Page