Четверг (05/28/09)

/dev/pts/0
09:45:15
#cat start-quiz
#!/usr/bin/perl
$id = $ENV{"SSH_CLIENT"};
$id = (split(/\s+/, $id))[0];
if ($id) { $id .= "-"};
$quiz_file = "quiz";
$log_file = ".quiz/$id".time().'.log';
@Questions = ();
$Score = 0;
sub ask_question($)
{
    my $question = $_[0];
    my %answer_score;
    $i=1;
    $question =~ s{^(#!?)}{$1 eq '#!' and $answer_score{$i}=1;$i++}gme;
    print $question;
    $answer=<>;
    chomp $answer;
    open(LOG, ">>$log_file")
        or die "Can't open $log_file for writing";
    print LOG $question;
    if ($answer_score{$answer}) {
        print LOG "+++ (", $answer,")\n";
    }
    else {
        print LOG "--- (", join(" ", sort(keys(%answer_score))), ")\n";
    }
    close(LOG);
    return $answer_score{$answer};
}
open(QUIZ, $quiz_file);
$question="";
while(<QUIZ>)
{
    s/<!--.*-->//g;
    if (/^\s*$/) {
        if ($question) {
            push @Questions, $question;
            $question = "";
        }
    }
    else {
        $question .= $_;
    };
}
close(QUIZ);
for $question (@Questions) {
    $Score += ask_question($question);
}
print "Your score: $Score\n";