[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Novice question regards subroutines



To get back to Tim (PwrSurge): You've probably followed the commentary on
the Operator Problem (eq or == for string or number comparisons; = to
assign values). There were a few other things in your script that needed
minor fixes.

1. Don't use die to quit. Just let it go, ending with a true value or an
explicit exit(0).
2. You had a reversed, non-syntactical assignment for your output line:
   $Number . ":" . $PayableTo . ":" . $Amount = push(@Account);  #no!
Try:
   $Entry = "$Number:$PayableTo:$Amount";   #yes!
If you wanted to add a new entry to your list variable, try:
   push(@AcctData, $Entry);  #look up push for more info
3. You didn't really need to read the whole existing file into a list
variable at the start and then print the whole variable back to the file,
just to add one line at the end. Just open the file in append mode and
print the new line:
    open(DST,">>$AcctFile") || die "Can't Open DST:  $!";
    print DST "$NewEntry", "\n";
    close(DST);
4. However, if you were sure you can keep control of things (like aborts),
you could loop your action subroutines, pushing new data lines onto a list
variable, and then print the lines of that variable to your file at the end
when the user is finished. This would mean fewer file accesses, slightly
quicker action, but more risk of entries not being recorded if something
goes wrong.
5. Overall, the whole script could be simplified using loop flow control
instead of conditional flow control, but that's up to you...

Good luck!


Try this revision of your script:

#!perl -w

$AcctFile = "Checking.txt";

&MainMenu;

sub MainMenu {
  $MainMenu = "";
  print "Main Menu\n";
  print "\n";
  print "1: Check\n";
  print "2: Debit Card\n";
  print "3: ATM Withdrawl\n";
  print "4: Deposit\n";
  print "5: Quit System\n";
  print "\n";
  print "Selection Please:  ";
  chop($MainMenu = <STDIN>);
  if ($MainMenu == 1) {
    &CheckMenu;
  }
  elsif ($MainMenu == 2) {
    &DebitCardMenu;
  }
  elsif ($MainMenu == 3) {
    &ATMWithdrawlMenu;
  }
  elsif ($MainMenu == 4) {
    &DepositMenu;
  }
  elsif ($MainMenu == 5) {
     print "Goodbye.";    #don't use die to quit
  }
  else {
    print "Invalid  option; try again.\n";
    &MainMenu;
  }
}

sub CheckMenu {
  $CheckMenu = "";
  print "Check Menu\n";
  print "\n";
  print "0: Main Menu\n";
  print "1: Normal Check\n";
  print "2: Void Check\n";
  print "\n";
  print "Selection Please:  ";
  chop($CheckMenu = <STDIN>);
  if ($CheckMenu == 0) {
    &MainMenu;
  }
  elsif ($CheckMenu == 1) {
    &NormalCheckMenu;
  }
  elsif ($CheckMenu == 2) {
    &VoidCheckMenu;
  }
}

sub NormalCheckMenu {
  print "Check Number:  ";
  chop($Number = <STDIN>);
  print "Check Payable To:  ";
  chop($PayableTo = <STDIN>);
  print "Check Amount:  \$";
  chop($Amount = <STDIN>);
  $Entry = "$Number:$PayableTo:$Amount";
   &WriteOut($Entry);
  &CheckMenu;
}


sub WriteOut {
  $NewEntry = $_[0];
  #open file in append mode (assumes the file is there):
  open(DST,">>$AcctFile") || die "Can't Open DST:  $!";
  print DST "$NewEntry", "\n";
  close(DST);
}

#make sure everything ends happily (i.e., true):
1;       #Could also use exit(0)

#########END OF SCRIPT########

 - Bruce

"Pessimism of the Mind; Optimism of the Will."   - Gramsci



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch