namishtiwari has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Here is my piece of code.
#!/usr/bin/perl my $logFile = $ARGV[0]; die "usage: $0 <logFile>" unless $logFile; die "Logfile $logFile doesn't exist" unless -f "$logFile"; my %no_of_questions; my %timestamp; open (FP,"<$logFile") or die "unable to open file $!" ; @records=<FP>; chomp(@records); for($index=0;$index<@records;$index++) { if($records[$index] =~ /^(.*)INFO:.*Entering QnAModule::authen +ticate/) { #printf "in INFO:.*Entering QnAModule::authenticate\n"; $Time_Stamp = $1; die "Some error in search_for_username_and_no_of_questions +" if (search_for_username_and_no_of_questions() == -1); die "Some error in end_block" if (end_block() == -1); last; } } sub search_for_username_and_no_of_questions { printf "inside search_for_username_and_no_of_questions\n"; for($index=0;$index<@records;$index++) { if ($records[$index] =~ /userName :\[(..*)\]/) { #printf "in user name identification \n"; $User_Name = $1; $User_Name =~ s/\s+//g; for(;$index<@records;$index++) { if ($records[$index] =~ /Set of questions\(bit +map\) selected : (\d+)/) { #printf "in set of questions\n"; $Bitmap_Number_For_Questions = $1; $no_of_questions{$User_Name}=$Bitmap_Numbe +r_For_Questions; $timestamp{$User_Name}=$Time_Stamp; printf "DETAIL: LINE NO: $index\n"; printf "$Time_Stamp,$User_Name,$Bitmap_Num +ber_For_Questions\n"; } } } } return -1; } sub end_block { printf "Inside end_block\n"; for($index=0;$index<@records;$index++) { if($records[$index] =~ /QNA Step.*AUTH IN PROGRESS/) { printf "DETAIL: auth in progress\n"; for(;$index<@records;$index++) { if ($records[$index] =~ /ArAuthFrameworkImpl::doPostAu +th.*Authentication mechanism returned \[(..*)\] for AuthIdentity \[(. +.*)\]/) { #printf "done one request \n"; #printf "$records[$index]\n"; my $return_code = $1; my $temp_user = $2; $temp_user =~ s/\s+//g; printf "user = $temp_user\n"; if(exists($no_of_questions{$temp_user})) { if ($return_code == 0) { printf "$no_of_questions{$temp_user} , $ti +mestamp{$temp_user} , \"Success\" \n"; } elsif ($return_code > 1) { printf "$no_of_questions{$temp_user} , $ti +mestamp{$temp_user} , \"Failure\" \n"; } for(;$index<@records;$index++) { if ( ($records[$index] =~ /Prepared to Sen +d OK/) || ($records[$index] =~ /Sending Invalid credential/) ) { printf "end of file\n"; } } } } } } } return -1; }
The script is not going in function end_block(). I am not able to understand why it is happening. here is the sample data-
INFO:Entering QnAModule::authenticate userName :[prasanna] Set of questions(bitmap) selected : 13 QNA Step.*AUTH IN PROGRESS ArAuthFrameworkImpl::doPostAuth.*Authentication mechanism returned [he +llo] for AuthIdentity [prasanna] Prepared to Send OK Sending Invalid credential
Thanks NT

Replies are listed 'Best First'.
Re: Problem in script
by derby (Abbot) on Jun 22, 2009 at 12:43 UTC

    The script never goes into the end_block function because your search_for_username_and_no_of_questions function always returns -1 and the conditional where it's called *dies* when the function returns -1.

    -derby
Re: Problem in script
by Transient (Hermit) on Jun 22, 2009 at 12:44 UTC
    To answer your question specifically, you have this chunk of code:
    die "Some error in search_for_username_and_no_of_questions" if (search +_for_username_and_no_of_questions() == -1); die "Some error in end_block" if (end_block() == -1);
    Meaning you exit the program if <deepbreath>search_for_username_and_no_of_questions</deepbreath> returns a -1. Well that's the only thing it will ever return (besides a $@) so there won't ever be a case where the end_block is called.
Re: Problem in script
by cdarke (Prior) on Jun 22, 2009 at 12:49 UTC
    The function search_for_username_and_no_of_questions always returns an error (-1) which is picked-up by
    die "Some error in search_for_username_and_no_of_questions" if (search +_for_username_and_no_of_questions() == -1);
    and executed before your end_block function. This function also has the same problem - you always return an error regardless.

    In your design, what are these functions supposed to return on success?
      Hi, When the fuction works fine it should return 0 or success. How i can modify the code to get rid of the problem. if i remove return -1, what else i can do in that place. Any suggestions. Thanks NT
      I changed the code and i am returning 0 when i am getting success. This is working fine for the snippet of the logfile i sent, but when i tested it on the logfile it does not print any data. The changed code is--
      #!/usr/bin/perl my $logFile = $ARGV[0]; die "usage: $0 <logFile>" unless $logFile; die "Logfile $logFile doesn't exist" unless -f "$logFile"; my %no_of_questions; my %timestamp; open (FP,"<$logFile") or die "unable to open file $!" ; @records=<FP>; chomp(@records); for($index=0;$index<@records;$index++) { if($records[$index] =~ /^(.*)INFO:.*Entering QnAModule::authen +ticate/) { #printf "in INFO:.*Entering QnAModule::authenticate\n"; $Time_Stamp = $1; die "Some error in search_for_username_and_no_of_questions +" if (search_for_username_and_no_of_questions() == -1); die "Some error in end_block" if (end_block() == -1); #last; } } sub search_for_username_and_no_of_questions { printf "inside search_for_username_and_no_of_questions\n"; for($index=0;$index<@records;$index++) { if ($records[$index] =~ /userName :\[(..*)\]/) { #printf "in user name identification \n"; $User_Name = $1; $User_Name =~ s/\s+//g; for(;$index<@records;$index++) { if ($records[$index] =~ /Set of questions\(bit +map\) selected : (\d+)/) { #printf "in set of questions\n"; $Bitmap_Number_For_Questions = $1; $no_of_questions{$User_Name}=$Bitmap_Numbe +r_For_Questions; $timestamp{$User_Name}=$Time_Stamp; #printf "DETAIL: LINE NO: $index\n"; #printf "$Time_Stamp,$User_Name,$Bitmap_Nu +mber_For_Questions\n"; return 0; } } } } return -1; } sub end_block { printf "Inside end_block\n"; for($index=0;$index<@records;$index++) { if($records[$index] =~ /QNA Step.*AUTH IN PROGRESS/) { printf "DETAIL: auth in progress\n"; for(;$index<@records;$index++) { if ($records[$index] =~ /ArAuthFrameworkImpl::doPostAu +th.*Authentication mechanism returned \[(..*)\] for AuthIdentity \[(. +.*)\]/) { #printf "done one request \n"; #printf "$records[$index]\n"; my $return_code = $1; my $temp_user = $2; $temp_user =~ s/\s+//g; #printf "user = $temp_user\n"; if(exists($no_of_questions{$temp_user})) { if ($return_code == 0) { printf "$temp_user,$no_of_questions{$temp_ +user} , $timestamp{$temp_user} , \"Success\" \n"; return 0; } elsif ($return_code > 1) { printf "$temp_user,$no_of_questions{$temp_ +user} , $timestamp{$temp_user} , \"Failure\" \n"; return 0; } for(;$index<@records;$index++) { if ( ($records[$index] =~ /Prepared to Sen +d OK/) || ($records[$index] =~ /Sending Invalid credential/) ) { #printf "end of file\n"; } } } } } } } return -1; }
      I ran this on this snippet logfile
      monday 12:55:66 INFO:Entering QnAModule::authenticate monday 12:55:66 userName :[prasanna] monday 12:55:66 Set of questions(bitmap) selected : 13 monday 12:55:66 QNA Step.*AUTH IN PROGRESS monday 12:55:66 ArAuthFrameworkImpl::doPostAuth.*Authentication mechan +ism returned [4] for AuthIdentity [prasanna] monday 12:55:66 Prepared to Send OK monday 12:55:66 Sending Invalid credential
      and i got this result which is as expected-
      inside search_for_username_and_no_of_questions Inside end_block DETAIL: auth in progress prasanna,13 , monday 12:55:66 , "Failure"
      but when i ran it on my actual log file i ran it like this-- C:\Perl Script>perl per_user_qna_detail.pl arcotwebfort_29May09_19_17_47.log>res ults.txt Some error in end_block at per_user_qna_detail.pl line 19, <FP> line 67209. i got this result--
      inside search_for_username_and_no_of_questions Inside end_block DETAIL: auth in progress
      why it is happening? please give me some suggestions. Thanks NT