簡體   English   中英

未定義的子程序&package ::子程序在行調用<of script>

[英]Undefined subroutine &package::subroutine called at line <of script>

我正在調試此腳本,在工作中-老板說該腳本曾經在Solaris上運行,但是由於他們切換到了Linux,因此該腳本停止了工作。 我必須用嚴格的警告來重寫它。 運行它時出現錯誤:

Undefined subroutine &Logging::openLog called at /path/to/script line 27

這是腳本(部分內容)

 1  #!/usr/local/bin/perl
 2
 3  unshift @INC, "/production/fo/lib";
 4  use strict;
 5  use warnings;
 6  use Sys::Hostname;
 7  use Getopt::Long qw(:config bundling auto_version);
 8  use File::Path;
 9
10  require "dbconfig2.pl";
11  require "logging2.pl";
12  require "hpov.pl";
13
14  # global variables
15  my $parseDate   = "";
16  my @fileList    = "";
17  my @transList   = "";
18  my $mLogDate    = "";
19  my $lHost                       = hostname;
20  my $corefiles_dir="/production/log/corefiles";
21  my $default_Threshold=90;
22
23  # do stuff
24
25  parseOptions();
26  Dbconfig::readconfigFile("$config");
27  Logging::openLog("$Dbconfig::prefs{logFile}","overwrite");
28  # msglog actions  TODO logs, compress only, data files
29          my $check_shdw=`ls -l /etc/motd | awk '{print \$11}' | grep 'motd.shdw'`; #Check if hostname is shadow
30          $check_shdw =~ y/\n//d; #remove new line if any
31  if ( $check_shdw eq "motd.shdw" )
32  {
33          Logging::printLog("INFO","Enviroment is Shadow, triggering core files compressing");
34          if (is_folder_empty($corefiles_dir)) {
35              print "Corefile Directory is EMPTY......! \n";
36          }
37          else {
38          gzip_corefiles() ; #Execute compress core files
39          }
40  }
41

該腳本使用require語句,我猜想調用腳本創建者構建的例程。 對於此腳本的目標-dbconfig只是將其插入到配置文件中並將其分解為值。 就像“ $ Dbconfig :: prefs {logFile}”一樣等於日志文件位置/prod/logs/script.log-就是這樣。

#!/usr/local/bin/perl

package Dbconfig;
#use warnings;
use DBI;
use DBD::Oracle;

%prefs          = "";
@$dbPrefs       = "";
$raiseError     = 0;
%startupItem    = "";

# readconfigFile(file) - read in a configuration file.
sub readconfigFile { 
    my $file = shift;
    if ( ! -e $file ) {
        $errorTxt   = "Error: $file does not exist.\n";
        $raiseError = 1;
    }
# read in the cfg variables
   open(CFGFILE,"<","$file") or die "Cannot open $file for reading: $!\n";
   while(<CFGFILE>) {
    chomp;    # kill newlines
    s/#.*//;  # ignore comments
    s/^\s+//; # ignore leading whitespace
    s/\s+$//; # ignore trailing whitespace
    next unless length;
    my($var,$value) = split(/\s*=\s*/, $_, 2);
    $prefs{$var} = $value;
   }
   close(CFGFILE);
}

然后是此日志記錄程序包。 在腳本的第27行(出現錯誤的位置)中,我看到了“覆蓋”調用,但是在logging.pl程序包中沒有看到任何引用覆蓋的內容-但不確定是否很重要。 父腳本似乎未寫入任何日志文件。 我什至不確定是否已創建文件句柄LOGFILE。

#!/usr/local/bin/perl

 package Logging;

use File::Copy;
use warnings;
use strict; 

my $timestamp = "";
my $filestamp = "";

# openLog(logfile name) - opens a log file
sub openLog {
    my $file   = shift;
    my $rotate = shift;
 # force a rotation if it exists.
    if ( -e $file && $rotate eq "rotate" ) {
        print "Warning: $file exists.  Rotating.\n";
        rotateLog($file);
    }
    getTime(); 
    open(LOGFILE,">","$file") or warn "Error: Cannot open $file for writing: $!\n";
    print LOGFILE "[$timestamp] - Normal - Opening log for $file.\n"; 
}
# rotateLog(log file) - rotate a log.
sub rotateLog {
    my $file = shift;
    getTime();
    openLog("$file");
    print LOGFILE "[$timestamp] - Warning - Rotating $file to $file.$filestamp.log";
    closeLog($file);
    move($file,$file-"$filestamp.log");
    openLog($file);
}
time() - grab timestamp for the log.
sub getTime {
    undef $timestamp;
    undef $filestamp;
    ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time))[0,1,2,3,4,5];
    $sec    = sprintf("%02d",$sec);
    $min    = sprintf("%02d",$min);
    $hour   = sprintf("%02d",$hour);
    $mday   = sprintf("%02d",$mday);
    $year   = sprintf("%04d",$year +1900);
    $mon    = sprintf("%02d",$mon +1);
    $timestamp  = "$mon-$mday-$year $hour:$min:$sec";
    $filestamp  = "$year$mon$mday$hour$min$sec";
}

只是想知道-logging.pl在第27行中從dbconfig.pl調用某些東西有問題嗎? 就像一個模塊可以從另一個模塊調用值一樣? 除了使用嚴格和警告以及許多打印語句外,我不知道下一步是什么調試步驟。 我不知道如何檢查並查看是否正在創建LOGFILE文件句柄-如果沒有出錯,我只能假設是。 就像我需要做一些額外的工作才能使模塊相互通信嗎?

我不是腳本之王-只是我這一行中唯一一個甚至可以開始理解這些內容的人。

不知道這是否會影響事情,但....

1)包需要返回true,正常過程是用以下行結束文件:

1; 

以確保。

2)記錄程序包中有注釋,但沒有前導#,這會導致編譯失敗:

time() - grab timestamp for the log.

3)這行:

unshift @INC, "/production/fo/lib"; 

正在將目錄添加到模塊的搜索路徑,請確保您的logging2.pl文件實際上位於該位置(很可能是否則會得到不同的錯誤,但值得仔細檢查)

那看起來一切正常。

由於某些原因,盡管要求“ logging2.pl”有效(否則會出現錯誤),但其中的子例程無法加載並可用。 與正常工作的DBconfig2.pl加載不同(否則,對Dbconfig :: readconfigFile()的調用將首先失敗)。

我只能看到的區別是Logging2.pl中package命令的前導空格,但是不要緊。

可以嘗試調用不帶包前綴(logging::)的openLog來查看它是否由於某種原因被加載到main中,並在require語句之后打印%INC的內容以確保其正確加載?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM