perl help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl help
# 1  
Old 07-19-2005
perl help

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

Here is the sample file
Code:
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

Code:
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

Code:
 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.
# 2  
Old 07-19-2005
That seems like a lot of code to parse such a simple file.

Awk:

Code:
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]
}

# 3  
Old 07-19-2005
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

Code:
$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

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

# 4  
Old 07-20-2005
@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;
# 5  
Old 07-20-2005
Quote:
Originally Posted by cbkihong
Also

Code:
$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

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

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 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. Shell Programming and Scripting

Perl :: reading values from Data Dumper reference in Perl

Hi all, I have written a perl code and stored the data into Data structure using Data::Dumper module. But not sure how to retreive the data from the Data::Dumper. Eg. Based on the key value( Here CRYPTO-6-IKMP_MODE_FAILURE I should be able to access the internal hash elements(keys) ... (1 Reply)
Discussion started by: scriptscript
1 Replies

4. UNIX for Advanced & Expert Users

perl and HP-UX : instmodsh in combination with software depot : update inventory for installed Perl

we create a HP-UX software depot with a new perl-modul. after installation of the software depot, the perl module i can't find with instmodsh in the inventory for installed Perl modules. - i have learned of using instmodsh command : i find out what modules are already installed on my system. ... (0 Replies)
Discussion started by: bora99
0 Replies

5. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

6. Shell Programming and Scripting

Hidden Characters in Regular Expression Matching Perl - Perl Newbie

I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense. ... (4 Replies)
Discussion started by: kittyluva2
4 Replies

7. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

8. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

9. Shell Programming and Scripting

Passing date formats in Perl: i.e. Jul/10/2007 -> 20070710 (yyyymmdd) - Perl

Hi , This script working for fine if pass script-name.sh Jul/10/2007 ,I want to pass 20070710(yyyymmdd) .Please any help it should be appereciated. use Time::Local; my $d = $ARGV; my $t = $ARGV; my $m = ""; @d = split /\//, $d; @t = split /:/, $t; if ( $d eq "Jan" ) { $m = 0 }... (7 Replies)
Discussion started by: akil
7 Replies

10. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies
Login or Register to Ask a Question