Perl script not executing

Hi All,
I have been given a perl script to modify but which is not running completely.And it is not showing any errors also.

The script is :

 
#!/usr/local/bin/perl
print "Which transaction? ";
print "\n";
print "1 - Inquiry";
print "\n";
print "2 - Delete Bank";
print "\n";
print "3 - Edit Bank";
print "\n";
print "4 - Enroll and Add Bank";
print "\n";
print "5 - Modify Default Bank";
print "\n";
print "6 - Submit Payment";
print "\n";
$goodChoice = "false";
while ($goodChoice eq "false") {
$choice = <STDIN>;
chomp $choice;
if ($choice eq "1") {
  $agent = "com.example.as.payment.app.agent.InquiryAgent";
  $goodChoice = "true";
}
elsif ($choice eq "2") {
  $agent = "com.example.as.payment.app.agent.DeleteBankAgent";
  $goodChoice = "true";
}
elsif ($choice eq "3") {
  $agent = "com.example.as.payment.app.agent.EditBankAgent";
  $goodChoice = "true";
}
elsif ($choice eq "4") {
  $agent = "com.example.as.payment.app.agent.EnrollAddBankAgent";
  $goodChoice = "true";
}
elsif ($choice eq "5") {
  $agent = "com.example.as.payment.app.agent.ModifyDefaultBankAgent";
  $goodChoice = "true";
}
elsif ($choice eq "6") {
  $agent = "com.example.as.payment.app.agent.SubmitPaymentAgent";
  $goodChoice = "true";
}
else {
  print "Please type a number, 1 - 6";
  print "\n";
}
}
$filename = "report.log";
open(FILEHANDLE, $filename);
$foundAgent = "false";
$printMetrics = "false";
while ($line = <FILEHANDLE>) {
  chomp $line;
  if ($printMetrics eq "true") {
    print $line;
    print "\n";
    $foundAgent = "false";
    $printMetrics = "false";
  }
  elsif ($foundAgent eq "true") {
    $printMetrics = "true";
  } 
  elsif ( $line =~ /$agent/ ) {
     $foundAgent = "true";
  } 
}
close (FILEHANDLE);

Can anyone please explain by what all is happening in this script.I am a beginner and couldn't understand all these commands.
Also i need to collect the output of this script in some output file.How to do that.

Thanks in advance.
U

The first half of the program is a menu driven type where user has benn given various options menu to to choose one of them - from 1 to 6 depending on his need.
The second half of the program seems to be opening a report log file and searching for a line containing the word 'agent' and then printing the line and repeating the same for all the lines thr out the file...

It basically prints the the 2nd line after each line that it finds that matches the menu item selected. So whatever you select it looks it compares a line to that selection If that selection is in the input line it goes to the next line in the file. It then sets the variable to print the line and goes to the next line. Then it prints this next line and resets the variable and starts over again. So each time it finds a match it skips the next line and prints the line after that. It does this until you get to the end of file.

To output the lines to a file you would have to open an output file and intead of just print $line you would use print outfile $line.
So you would add a line after the open (FILEHANDE, $filename); as

open (OUTFILE, ">/temp/outputfile");

Then add that to the print line such as

print OUTFILE $line;

Then close the OUTFILE after you close the input file such as

close(FILEHANDLE);
close(OUTFILE);

Hi All,
Thanks for your quick replies.
I just have one doubt ,if in place of report.log in the above code if i have a file which is zipped (i.e with .Z extension ) then this perl script will fail.
Is there any way i can unzip this file inside the script itself (like using zcat or something) so that i can run this script on a zipped file??

Thanks
U

Perl doesn't really handle the Z compression but you can use the operating system commands on the files. So you could use zcat to output the data to the input file report.log. Or you could use a list to replace the input file with the list of data from the zcat command. So you could use somehting like

@inputdata=`zcat inputfile.Z`;

or use the system command like

system("zcat inputfile.Z > report.log");

Anything like that would work out for you.

Hi Bubbajoe,
Thanks again for your reply and apologies for my ignorance.
How exactly to use the specified line in above code.The problem is i cant try executing the code until it is completely perfect.

Is it like this:

@inputdata=`zcat inputfile.Z`;
open(FILEHANDLE, @inputdata);
open (OUTFILE, ">/temp/outputfile");
$foundAgent = "false";
$printMetrics = "false";
while ($line = <FILEHANDLE>) {
  chomp $line;
  if ($printMetrics eq "true") {
    print OUTFILE $line;
    print "\n";
    $foundAgent = "false";
    $printMetrics = "false";
  }
  elsif ($foundAgent eq "true") {
    $printMetrics = "true";
  } 
  elsif ( $line =~ /$agent/ ) {
     $foundAgent = "true";
  } 
}
close (FILEHANDLE);
close(OUTFILE);

or is it like this:

system("zcat inputfile.Z > report.log");
$filename = "report.log";
open(FILEHANDLE, @inputdata);
open (OUTFILE, ">/temp/outputfile");
$foundAgent = "false";
$printMetrics = "false";
while ($line = <FILEHANDLE>) {
  chomp $line;
  if ($printMetrics eq "true") {
    print OUTFILE $line;
    print "\n";
    $foundAgent = "false";
    $printMetrics = "false";
  }
  elsif ($foundAgent eq "true") {
    $printMetrics = "true";
  } 
  elsif ( $line =~ /$agent/ ) {
     $foundAgent = "true";
  } 
}
close (FILEHANDLE);
close(OUTFILE);

Thanks in advance

First do this, only this:

@inputdata=`zcat inputfile.Z`;
print "$_\n" for @inputdata;

report back what gets printed. Solve the problems one step at a time instead of trying to do too many things at once.

Hi Kevin,
Thanks for your reply.
But this line:

print "$_\n" for @inputdata;

What exactly it is doing,it is printing right.But my requirement is eachline from that .Z file should be read one by one according to the particular condition and then print...
Can you help??
Thanks

It will just print the content of the array so you can see what is in the array. Its just to verify that this code works and the array holds data you need to proceed further with your program. So, run this code and report back what gets printed:

@inputdata=`zcat inputfile.Z`;
print "$_\n" for @inputdata;