[ale] perl problems...

David S. Jackson dsj at dsj.net
Sat May 15 17:38:07 EDT 1999


--zhXaljGHf11kAtnf
Content-Type: text/plain; charset=us-ascii

Hi,

As time permits, I've been crawling along with my perl studies, and I've
been working on my little "todo manager".  

My current problem is when I call &edit_task and exit it through the
PROMPTUSER block ( in the unless statement ), I always seem to fall through
to the next subroutine in the Main Menu, which is &remove_task.  

I had put a EXITSUB: block in the &edit_task subroutine and then used a goto
statement to branch to that for early exit from the subroutine.  I didn't
have a problem with that, but I changed it so I didn't have to use a goto
statement.  Now I have the "fall through" problem.  

Can anyone help me with why it behaves this way?

--
David S. Jackson                           http://www.dsj.net
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
"To give your sheep or cow a large, spacious meadow is the way
to control him."            --Shunryu Suzuki

--zhXaljGHf11kAtnf
Content-Type: application/x-perl
Content-Disposition: attachment; filename="newplanner.pl"

#!/usr/bin/perl -w

###  Variables

$count = 0;      		# initialize to zero
my $datafile = "/home/dsj/.planner/data.txt";
my (@description, @priority, @done, @line);


### Get new task descriptions; initialize 'done' and 'priority' variables

sub get_task {
  while (1) {
     system( "clear" );
     print "\nEnter new task description:  ";
     chomp($description[$count]=ucfirst(<STDIN>));
     $done[$count] = "";
     print "\nPriority A, B, or C?  ";
     chomp($priority[$count]=uc(<STDIN>));
     $line = "$done[$count]  $priority[$count]\t$description[$count]\n";
     push @tasks, $line;
     $count++;
     print "\nContinue?  ";
     chomp($continue=<STDIN>);
     if ($continue =~ /y/i) { next; }
     else { last; }
  }
  foreach $entry (sort @tasks) { 
     open DATAFILE, ">>$datafile" or die "can't open $datafile: $!\n";
     print DATAFILE $entry;
     close DATAFILE;
  }
  
}

### Here is where you edit your existing tasks (redefine, prioritize, etc).

sub edit_task {
	system( "clear");
	OPENDATA: {
	   $numtasks = 0; $count2 = 0; @description = 0;
	   open DATAFILE, "<$datafile" or die "\nCan't open $datafile: $!";
	   foreach $line (<DATAFILE>) {
	       if ( $line =~ /[ABCD]/ ) {  
	       	 $_ = $line;
	         ($priority[$count2], $description[$count2]) = /(\w+)\W+(\w.*)/;
	         $count2++;
	       }  # end of if
	   }     # end of foreach
	}      # end of OPENDATA

	SHOWDATA: {
	  print "\nNum  Pri Description\n===============================\n";
	  for ($count3 = 0; $count3 < @description; $count3++) {
	      printf "\n$count3    $priority[$count3]\t $description[$count3] ";
	  }  # end of for loop
	}    # end of SHOWDATA

	PROMPTUSER: { 
	  print "\nChange a task?  ";
	  chomp($fixornot=<STDIN>); 
	  unless ( $fixornot =~ /y/i ) { 
	      $numtasks = 0;
	      @description = ();
	      close DATAFILE;
	      return 0; 
	  }
          print "\nWhat task do you want to change?  ";
	  chomp($tofix=<STDIN>);
	}
	
	CHGDESC: {
	   print "\nOld Description: $description[$tofix]";
	   print "\nNew Description:  ";
	   chomp($description[$tofix] = ucfirst(<STDIN>));
	}
	
	CHANGEPRI: {
	   print "\nOld Priority: $priority[$tofix]";
	   print "\nNew Priority:  ";
	   chomp($priority[$tofix] = uc(<STDIN>));
	}

	WRITEFIX: {
	   open WDATAFILE, ">$datafile" or die "\nCan't open $datafile: $!\n";
	   for ($count4 = 0; $count4 < @description; $count4++) {
	       print WDATAFILE "$done[$count4]  $priority[$count4]\t$description[$count4]\n";
	   }
	   close WDATAFILE;
	}

	close DATAFILE;

}


### Display tasks
sub print_task {
  system( "clear" );
  open(DATAFILE, "<$datafile") or die "\nCan't open $datafile: $!\n";
  print "\n  Pri.\tDescription\n";
  print "==========================================================\n\n";
  while (<DATAFILE>) { push @lines, "$_\n";  }
  foreach $line (sort @lines) { print $line;  }
  print "\nHit Enter to continue...";
  close DATAFILE;  @lines="";
  <STDIN>;
}


#### Remove completed tasks ############################

sub remove_task {
   my ($num, @line); $num = 0;
   system( "clear" );
   open DATAFILE, "<$datafile" or die "\nCan't open $datafile: $!\n";
   print "\nNo.  Pri.\tDescription\n";
   print "=========================================================\n\n";
   while (<DATAFILE>) { push @line, "$_"; }
   foreach $entry (sort @line) {
      print (int($num + 1), " $entry");
      $num++;
   }
   print "\nWhat task number is complete?  ";
   close DATAFILE;  
   chomp($donetask = <STDIN>);
   open OUTFILE, ">$datafile" or die "\nCan't open $datafile for writing: $!\n";
   splice @line, $donetask, 1;
   for ( $num = 0; $num < @line; $num++ ) {
      print OUTFILE $line[$num];
   }
   close OUTFILE;  $entry = "";
}

#### Just edit the sucker by hand!  ####

sub vi_it {

    system( "vim $datafile" );
}

###########################################
##########     Main
###########################################

while (1) { 
  system( "clear" );
  print <<"EOF";

                   Linux Planner

	E)nter Tasks
	ED)it Tasks
	R)emove Completed Tasks
	V)iew Tasks
	ViM) Task File
	EX)it Planner

EOF
  print "\tYour choice?  ";
  chomp($mainansw=<STDIN>);
  SWITCHMAIN: {
     $_ = $mainansw;
     if (/e/i) { &get_task; }
     if (/d/i) { &edit_task; }
     if (/r/i) { &remove_task; }
     if (/v/i) { &print_task; }
     if (/m/i) { &vi_it; }
     if (/x/i) { system( "clear" ); print "\nBye for Now!\n"; exit 0; }
  }
}
  

__END__

Problems:

1)  Remove Completed Tasks doesn't always remove tasks.  The $num variable 
    sometimes doesn't reset properly.  Fix that lil' puppy.
2)  @done isn't really being used yet.
3)  $datafile is not always formatted the same by each subroutine.
4)  On EDit Tasks, you drop right into Remove tasks for some reason.  
    This is very disconcerting.



Notes for improvement:

1)  On the EDit Tasks menu, let the first entry accept not just a 
    yes or no, but also the number of a task to edit.

2)  Use a Tk interface and print to Franklin Planning paper.

3)  Add a values and goals section.

--zhXaljGHf11kAtnf--






More information about the Ale mailing list