My error log is growing dangerously large...

N

Nigel

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
 
J

j.keßler

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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

Jürgen Exner

Nigel said:
[...]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
 
G

Gunnar Hjalmarsson

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


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

Or maybe just

no warnings 'uninitialized';
 
T

Tad J McClellan

Nigel said:
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.)
 
D

Danish

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
 
J

Jürgen Exner

Danish said:
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

Jürgen Exner

Tad J McClellan said:
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
 
D

Dr.Ruud

Jürgen Exner said:
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.
 
J

Jürgen Exner

Dr.Ruud said:
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
 
D

Dr.Ruud

Jürgen Exner said:
[ -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?
;)
 
T

Thrill5

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'.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top