capser
capser

Reputation: 2635

Uninitialized error in Perl package

I keep on getting an uninitialized error on line 35. i rewrote the script to strict and warnings.

[casper@casperbox .wjohnson]$  /production/fo/scr/shipLogs.pl -c /production/fo/cfg/sendLogs.cfg -r > /production/fo/log/shipLogs.log
Use of uninitialized value $rotate in pattern match (m//) at /production/fo/lib/logging2.pl line    35.
[casper@casperbox .wjohnson]$

The numbers are not consecutive (these are the modules that the first package uses) There is a 1 at the bottom of the package script and the logging2.pl file is in the correct path. The comments are me trying to put the roate in different areas to make it work.

I have put the rotate at the top of the loop, inside the loop - changed it from a comparison string to a matching statement. Still get the unintalized error.

 20 package Logging;
 21 
 22 use File::Copy;
 23 use warnings;
 24 use strict;
 25 use diagnostics;
 26
 27 my $timestamp = "";
 28 my $filestamp = "";
 29 my $rotate = ""; 
 30 # openLog(logfile name) - opens a log file
 31 sub openLog {
 32     my $file   = shift;
 33     my $rotate = shift;
 34 # force a rotation if it exists.
 35     if ( -e $file && "$rotate" =~ 'rotate' ) {
 36     #if ( -e $file && $rotate eq 'rotate' ) {
 37     #if ( -e $file && $rotate eq "rotate" ) {
 38         print "Warning: $file exists.  Rotating.\n";
 39         rotateLog($file);
 40     }
 41     getTime();
 42     open(LOGFILE,">","$file") or warn "Error: Cannot open $file for writing: $!\n";
 43     print LOGFILE "[$timestamp] - Normal - Opening log for $file.\n";
 44 }

 54 # getTime() - grab timestamp for the log.
 55 sub getTime {
 56     undef $timestamp;
 57     undef $filestamp;
 58     my ($sec,$min,$hour,$mday,$mon,$year) = " ";
 59     ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time))[0,1,2,3,4,5];
 60     $sec    = sprintf("%02d",$sec);
 61     $min    = sprintf("%02d",$min); 
 62     $hour   = sprintf("%02d",$hour);
 63     $mday   = sprintf("%02d",$mday);
 64     $year   = sprintf("%04d",$year +1900);
 65     $mon    = sprintf("%02d",$mon +1);
 66     $timestamp  = "$mon-$mday-$year $hour:$min:$sec";
 67     $filestamp  = "$year$mon$mday$hour$min$sec";
 68 }
 77
 78 # rotateLog(log file) - rotate a log.
 79 sub rotateLog {
 80     my $file = shift;
 81     getTime();
 82     openLog("$file");
 83     print LOGFILE "[$timestamp] - Warning - Rotating $file to $file.$filestamp.log";
 84     closeLog($file);
 85     move("$file","$file-$filestamp.log");
 86     openLog($file);
 87 }



130 1

I get this error on use diagnostics:

Use of uninitialized value $rotate in pattern match (m//) at
        /production/fo/lib/logging2.pl line 35 (#1)
    (W uninitialized) An undefined value was used as if it were already
    defined.  It was interpreted as a "" or a 0, but maybe it was a mistake.
    To suppress this warning assign a defined value to your variables.

    To help you figure out what was undefined, perl will try to tell you the
    name of the variable (if any) that was undefined. In some cases it cannot
    do this, so it also tells you what operation you used the undefined value
    in.  Note, however, that perl optimizes your program and the operation
    displayed in the warning may not necessarily appear literally in your
    program.  For example, "that $foo" is usually optimized into "that "
    . $foo, and the warning will refer to the concatenation (.) operator,
    even though there is no . in your program.

there are two parameters passed to the open log subroutine from the parent script:

parseOptions();
Dbconfig::readconfigFile("$config");
#Logging::openLog("$Dbconfig::prefs{logFile}","overwrite");
Logging::openLog("$Dbconfig::prefs{logFile}","rotate");
# msglog actions  TODO logs, compress only, data files
        my $check_shdw=`ls -l /etc/motd | awk '{print \$11}' | grep 'motd.shdw'`; #Check if hostname is shadow
        $check_shdw =~ y/\n//d; #remove new line if any
if ( $check_shdw eq "motd.shdw" )
{
        Logging::printLog("INFO","Enviroment is Shadow, triggering core files compressing");
        if (is_folder_empty($corefiles_dir)) {
            print "Corefile Directory is EMPTY......! \n";
        }

I have checked the Dbconfig parent calls with print statements - they work. what i do not understand is the rotate and overwirte statements. The only thing that gets passed to to the log file /production/fo/log/shipLogs.log is the string : Corefile Directory is EMPTY......! That's it - nothing else from the parent script get passed - no timestamps, no strings - nothing. The log file gets unlinked after about a minute - the log file shows up, has one line in it, them disappears.

Upvotes: 1

Views: 158

Answers (3)

toolic
toolic

Reputation: 62047

In the openLog sub, $rotate is the 2nd parameter passed to the function, but in the 2 calls to openLog, you only pass 1 parameter:

 openLog("$file");

You need to pass a 2nd parameter to openLog. Or, you need to provide a default value in the sub.

The my declaration inside the sub creates a new $rotate variable at a different scope.

UPDATE: Now that you have shown more code, the problem is that when openLog calls rotateLog, rotateLog calls openLog with only 1 parameter.

Upvotes: 4

MarcoS
MarcoS

Reputation: 17711

function openLog expects 2 parameters, not 1 as passed to it on line 86.

So second parameter ($rotate) is undefined at runtime... :-)

Upvotes: 1

Toto
Toto

Reputation: 91415

Line 35 should be:

35     if ( -e $file && $rotate =~ m'rotate' ) {

or

35     if ( -e $file && $rotate =~ /rotate/ ) {

Upvotes: 1

Related Questions