perl help

Apologize for the long post. Need to resolve this error in the perl program

Here is the sample file

NAME             ; M0511514944
CLASS            ; DRAW
DATETIME       ; 20050425104230
FROMBOXNO    ; XYZA
FROMLOC        ; XYZ
FROMPERSON   ; Jane Doe
FROMEMAIL     ; xyz@xyz.com
FROMPHONE    ; 999-999-9999
TOBOXNO       ; ABCDEF
TOCOMPANY   ; ABC Inc.
TOPERSON      ; John Doe
TOEMAIL         ; abc@abc.com
SENDNOTIFICATION ; Y
SENDCTRLFILE  ; Y

Here is the function that parses the file

sub GetControlInfo {
my $Cmd;
my $Log;
my $Name;
my $Class;
my $Aprf;
my $WkDir;
my $UniqId;
my $Sender;
my $Notifcn;
my $ErrFlag;
my $RetValue;
my @TmpStore;
my $TempFile;
my $Receiver;
my $CtrlFile;
my $SendCtrl;
my $SenderEmail;
my $ReceiverEmail;
        ($CtrlFile,$Name,$Sender,$Receiver,$Notifcn,$SendCtrl,$Log,$UniqId,$WkDir,$ErrFlag,$Aprf,$SenderEmail,$ReceiverEmail)=@_;
        chdir "$WkDir" or die "${CtrlFile}:Error changing to the WorkDir  $! \n";
        #Run the staging script to update document status info for Drawings
        $TempFile="/entH/temp/${CtrlFile}.tmp";
        $Cmd = "/entH/bin/OBStagingDwg.sh $CtrlFile 2>&1 $Log \n";
        $RetValue = system ("$Cmd");
        if ($RetValue != 0)
        {
             rename "$WkDir$CtrlFile", "$WkDir${CtrlFile}.err";
             $ErrFlag = 1;
             $_[8] = $ErrFlag;
             return 0;
        }
        open (TMPFILE, "$TempFile") or die ("Error opening the temp file to read Config File  $! \n");
        while (<TMPFILE>)
        {
                @TmpStore=$_;
        }
        $UniqId = splice (@TmpStore,0);
        close (TMPFILE);
        print "Value of Temp File is      :- $TempFile \n";
        unlink $TempFile;

        open (CTRFILE, "$CtrlFile") or die (" Error opening the control file  $! \n");
        while (<CTRFILE>)
        {
                #Set the default values of
                $Notifcn = "Y";
                $SendCtrl = "Y";
                if ( $_ =~ 'NAME' )
                {
                        @TmpStore=split (';',$_);
                        $Sender=splice(@TmpStore,1);
                        #trim leading spaces and trailing spaces
                        $Name =~ s/^\s*(.*?)\s*$/$1/; # Line number 158 in original code
                        print "Value of Name is           :- $Name \n";
                }
                if ( $_ =~ 'CLASS' )
                {
                        @TmpStore=split (';',$_);
                        $Class=splice(@TmpStore,1);
                        #trim leading spaces and trailing spaces
                        $Class =~ s/^\s*(.*?)\s*$/$1/;
                        $Aprf = $1;
                        print "Value of Class is          :- $Class \n";
                        print "Value of APRF is           :- $Aprf \n";
                }
                if ( $_ =~ 'FROMBOXNO' )
                {
                        @TmpStore=split (';',$_);
                        $Sender=splice(@TmpStore,1);
                        #trim leading spaces and trailing spaces
                        $Sender =~ s/^\s+//; chomp ($Sender);
                        print "Value of Sender is         :- $Sender \n";
                }
                if ( $_ =~ 'TOBOXNO' )
                {
                        @TmpStore=split (';',$_);
                        $Receiver=splice(@TmpStore,1);
                        #trim leading spaces and trailing spaces
                        $Receiver =~ s/^\s+//; chomp ($Receiver);
                        print "Value of Receiver is       :- $Receiver \n";
                }
                if ( $_ =~ 'SENDNOTIFICATION' )
                {
                        @TmpStore=split (';',$_);
                        $Notifcn=splice(@TmpStore,1);
                        #trim leading spaces and trailing spaces
                        $Notifcn =~ s/^\s+//; chomp ($Notifcn);
                        print "Value of Notification is   :- $Notifcn \n";
                }
                if ( $_ =~ 'SENDCTRLFILE' )
                {
                        @TmpStore=split (';',$_);
                        $SendCtrl=splice(@TmpStore,1);
                        #trim leading spaces and trailing spaces
                        $SendCtrl =~ s/^\s+//; chomp ($SendCtrl);
                        print "Value of Send Control File :- $SendCtrl \n";
                }

                if ( $_ =~ 'FROMEMAIL' )
                {
                        @TmpStore=split (';',$_);
                        $SenderEmail=splice(@TmpStore,1);
                        $SenderEmail =~ s/^\s//; chomp($SenderEmail);
                        print "Value of Senders Email is  :- $SenderEmail \n";
                }
                if ( $_ =~ 'TOEMAIL' )
                {
                        @TmpStore=split (';',$_);
                        $ReceiverEmail=splice(@TmpStore,1);
                        $ReceiverEmail =~ s/^\s//; chomp($ReceiverEmail);
                        print "Value of Receivers Email is:- $ReceiverEmail \n";
                }

          }#End of while()
          close (CTRFILE);

          $_[1]=$Name;
          $_[2]=$Sender;
          $_[3]=$Receiver;
          $_[4]=$Notifcn;
          $_[5]=$SendCtrl;
          $_[7]=$UniqId;
          $_[9]=$ErrFlag;
          $_[10]=$Aprf;
          $_[11]=$SenderEmail;
          $_[12]=$ReceiverEmail;
}#End of GetControlInfo

From the main program a call to the function GetControlInfo is placed as follows

 GetControlInfo ($CtrFile, $Name, $Sender, $Receiver, $SendMail, $PostCtrl, $LogFile, $Snrf, $CurDir, $ErrFileFlag, $Aprf
, $SenderEmail, $ReceiverEmail);

When this function is called I get the warning/error message

Use of uninitialized value in substitution (s///) at test.pl line 158, <CTRFILE> line 1.
Use of uninitialized value in substitution (s///) at test.pl line 158, <CTRFILE> line 1.

How can this be overcome. Any help is much appreciated. Thanks.

That seems like a lot of code to parse such a simple file.

Awk:

function GetControlInfo( file, array )
{ split( "", array )
  while ((getline <file ) > 0)
    array[$1] = $3
  close(file)
  if (!("SENDNOTIFICATION" in array))
    array["SENDNOTIFICATION"] = "Y"
  if (!("SENDCTRLFILE" in array))
    array["SENDCTRLFILE"] = "Y"
}
BEGIN {
  GetControlInfo( "data", info )
  for (k in info)
    print k, info[k]
}

I can't really much understand what you are trying to do actually.

What do you pass to the subroutine? Do the parameter variables carry any values when you call it? It seems to me that you would like to extract the fields in the subroutine, but why pass them in as parameters? It seems to me like the C pass-by-pointer scheme, but please don't do that. You will create a lot of scalability problems with this, and is very dangerous. If all parameters passed in are undef, you'd better drop the input parameters, return the extracted fields as a list and get them back at caller instead of modifying input parameters passed by reference. In Perl, this is just so simple to do. If you do this, you will save a lot of assigning to
@_. The best practice: avoid pass by reference except for passing object references or arrays/hash/other composite complex data structure that would otherwise be deformed if passed in in the normal way.

Also

$Name =~ s/^\s*(.*?)\s*$/$1/; # Line number 158 in original code

So, what is the value of $Name at that point? I don't see its value assigned before that but you are performing a regex on it! You would like to trim some string here, but if the string is undef, it must display that warning (it's not an error)!

Seems like this is what you intend

$Sender =~ s/^\s*(.*?)\s*$/$1/; # Line number 158 in original code

@TmpStore=split (';',$_);
$Sender=splice(@TmpStore,1);
#trim leading spaces and trailing spaces
$Name =~ s/^\s*(.*?)\s*$/$1/;

Line 2 of this paragraph surely should be

$Name = splice ( @TmpStore , 1 );

otherwise $Name == undef;

Thanks to all of you guys. Yes, I was not assigning the value to $Name before performing the regex on it as correctly pointed out by cbkihong and ERNci caught on to it in his post also.
Futurelet, I know it is a lot of code, I am dealing with code that was written by another programmer and I shall use your style in the future.

Thanks Jerardfjay :slight_smile: