in reply to Re^2: code for review?
in thread code for review?
#!/usr/bin/perl use strict; use warnings; use Term::ReadLine; use File::Copy; local *F; my $term = Term::ReadLine->new(); my $DATFILE = "./store.txt"; my (@Values, $reply); my @teams = qw( Arsenal Bolton Villa Chelsea Middlesbro +ugh Newcastle Birmingham Blackburn Palace Fulham Liverpool ManchesterCity Norwich Portsmouth Spurs WBA ManchesterUnited Southampton Charlton Everton ); clearscreen(); # if we don't have a datfile we HAVE to INIT it!!! init_file(\@teams) unless -e $DATFILE; # now we can read the datfile because it exists, 100% guaranteed open F, $DATFILE or die "Can't read $DATFILE, Perl says $!\n"; chomp ( @Values = split('\s+', <F> )); close F; # Show Choice Menu print " ___________________________ 1. Update Teams 2. Check previous Results 3. Backup Storage File 4. Quit ___________________________ Option: "; chomp($reply=$term->readline()); my $menu = { 1 => sub { team_update(\@Values, \@teams) }, 2 => sub { team_find(\@Values) }, 3 => sub { storage_backup() }, default => sub { print "Exiting\n"; clearscreen() }, }; ( $menu->{$reply} || $menu->{default} ) -> (); exit 0; #----------------------------------------------------------------- # subs #----------------------------------------------------------------- sub clearscreen { $^O =~ m/Win32/ ? system('cls') : system('clear') } sub init_file { my ($teams) = @_; open F, ">$DATFILE" or die "Can't write $DATFILE, Perl says $!\n"; print F "$_ S S S S " for @$teams; close F; } sub team_update { my ($Values, $teams) = @_; my ($answer, $team_number, $home_result, $away_result, $score_for, + $score_against, $ae); # clear the screen clearscreen(); # update values with this weeks results and scores for (my $j = 0; $j <= $#teams; $j++) { print "\nDo You have results for $teams[$j] (y\\n)?\n"; chomp($answer=$term->readline()); if ($answer =~ /[Yy](es)*/) { $team_number = $j*5; $Values[$team_number]=$teams[$j]; print "$j\t\t$Values[$team_number]\n"; print "\nHome Result ((W D or L) _ if its an away game +):\n"; chomp($home_result=$term->readline()); $home_result=~s/\s+$//g; $Values[$team_number+1]=~s/($Values[$team_numb +er+1])/$1$home_result/g; print "\nAway Result ((W D or L) _ if its an home game +):\n"; chomp($away_result=$term->readline()); $away_result=~s/\s+$//g; $Values[$team_number+2]=~s/($Values[$team_numb +er+2])/$1$away_result/g; print "\nGoals Scored:\n"; chomp($score_for=$term->readline()); $score_for=~s/\s+$//g; $Values[$team_number+3]=~s/($Values[$team_numb +er+3])/$1$score_for/g; print "\nGoals Conceeded:\n"; chomp($score_against=$term->readline()); $score_against=~s/\s+$//g; $Values[$team_number+4]=~s/($Values[$team_numb +er+4])/$1$score_against/g; } # clear the screen clearscreen(); } # update storage file open F,"+< $DATFILE" or die "Can't write $DATFILE, Perl says $!\n" +;; for ($ae = 0; $ae <= $#Values; $ae++) { print F "$Values[$ae] "; } close F; return 1; } sub team_find { my ($Values) = @_; my ($ans, $team1, $team2); # clear the screen clearscreen(); # find team index $ans = "y"; while ($ans =~ /[Yy]/) { print "\nTeams to be found:\n"; chomp($team1=$term->readline()); chomp($team2=$term->readline()); $team1=~s/\s+$//g; $team2=~s/\s+$//g; for (my $i = 0; $i <= $#Values; $i++) { if ($Values[$i] =~ /$team1|$team2/) { print "\n___________________________\n\nteam:\t$Values +[$i]\n___________________________\n"; for (my $j = 1; $j < 5; $j++) { $Values[$i+$j]=~s/^S//g; } print "\nHome Record is:\n$Values[$i+1]\n"; print "Away Record is:\n$Values[$i+2]\n"; print "\nPreviously scored:\n$Values[$i+3]\n"; print "Previously Conceeded:\n$Values[$i+4]\n"; } } print "\nAgain (y\\n)?\n"; $ans=$term->readline(); chomp($ans); clearscreen(); } return 1; } sub storage_backup { # clear the screen clearscreen(); unless(copy("$DATFILE", "$DATFILE.bak")) { print "Could not copy $DATFILE to $DATFILE.bak\n"; return 0; } print "Successfully copied store.txt to store.txt.bak\n"; sleep 2; return 1; }
|
|---|