use strict; # This file defines functions to read and write different elements of an individual students response to a survey. # A response consists of an alpha number, a lastname, information about when it was submitted, and an array of "answers" to # individual questions. # DEBUG: This magic number is stored in the first index of the main array. # Each function will check that the magic number is valid -- if not, then something # was gone wrong -- most likely this means you called the function wrong. use constant RESPONSE_MAGIC_NUMBER => 87347865; # Some constants used internally. These specify the index in the main array that each # part of the 'response' is stored use constant RESPONSE_INDEX_MAGIC => 0; use constant RESPONSE_INDEX_ANSWERS => 1; use constant RESPONSE_INDEX_SUBMITDATE => 2; use constant RESPONSE_INDEX_ALPHA => 3; use constant RESPONSE_INDEX_LASTNAME => 4; use constant RESPONSE_INDEX_SCORE => 5; # Internal debug function -- if this complains then you probably called the original function wrong sub responseCheckMagic { my ($response, $functionName, $caller_package, $caller_filename, $caller_line) = @_; if ($$response[RESPONSE_INDEX_MAGIC] != RESPONSE_MAGIC_NUMBER) { print "\n\n\n

ERROR! Bad response magic number in call to $functionName from $caller_filename, line $caller_line.\n " . "This probably means you called $functionName wrong.

\n\n\n"; exit(1); } } # Inputs: none # Returns: a new response. # sub responseCreateNew { my @anArray = (); my $response = \@anArray; $$response[RESPONSE_INDEX_MAGIC] = RESPONSE_MAGIC_NUMBER; # init magic number my @emptyAnswerArray = (); $$response[RESPONSE_INDEX_ANSWERS] = \@emptyAnswerArray; # ref to answer array; return $response; } # Internal function -- returns whole array of answers sub helperResponseGetAnswerArray { my ($response) = @_; my $ref_answerArray = $$response[RESPONSE_INDEX_ANSWERS]; my @answerArray = @$ref_answerArray; return @answerArray; } # Inputs: a response # Returns: number of answers in that response sub responseGetNumAnswers() { my($response) = @_; # get the input parameter responseCheckMagic($response, "responseGetNumAnswers()", caller()); my @answerArray = helperResponseGetAnswerArray($response); my $numAnswers = @answerArray; return $numAnswers;; } # Inputs: (response, answer) # where response is the response to add to, # and answer is the answer to be added to end of the answers array. # Returns: nothing (but modifies the response that was provided, # adding the answers to the end of the response) sub responseAddAnswer() { my($response, $answer) = @_; responseCheckMagic ($response, "responseAddAnswer()", caller()); my $ref_answerArray = $$response[RESPONSE_INDEX_ANSWERS]; push @$ref_answerArray, $answer;; } # Inputs: (response, answerNum) # where response is the response to read from, # and answerNum is the number of the answer to return # (indexed from zero) # Outputs: sub responseGetAnswer { my($response, $answerNum) = @_; responseCheckMagic ($response, "responseGetAnswer()", caller()); my @answerArray = helperResponseGetAnswerArray($response); my $answer = $answerArray[$answerNum]; return $answer; } # Inputs: a response # Returns: the submit date of that response, or undef sub responseGetSubmitDate { my($response) = @_; # get the input parameter responseCheckMagic($response, "responseGetSubmitDate()", caller()); return $$response[RESPONSE_INDEX_SUBMITDATE]; } # Inputs: (response, submitDate)) # where response is the response to modify, # and 'submitDate' is the response submit date to be set # Returns: nothing (but modifies the response that was provided, # setting the due date of that response)) sub responseSetSubmitDate() { my($response, $submitDate) = @_; responseCheckMagic($response, "responseSetSubmitDate()", caller()); $$response[RESPONSE_INDEX_SUBMITDATE] = $submitDate; } # Inputs: a response # Returns: the alpha of that response, or undef sub responseGetAlpha { my($response) = @_; # get the input parameter responseCheckMagic($response, "responseGetAlpha()", caller()); return $$response[RESPONSE_INDEX_ALPHA]; } # Inputs: (response, alpha)) # where response is the response to modify, # and 'alpha' is the response alpha to be set # Returns: nothing (but modifies the response that was provided, # setting the alpha of that response)) sub responseSetAlpha() { my($response, $alpha) = @_; responseCheckMagic($response, "responseSetAlpha()", caller()); $$response[RESPONSE_INDEX_ALPHA] = $alpha; } # Inputs: a response # Returns: the lastname of that response, or undef sub responseGetLastname { my($response) = @_; # get the input parameter responseCheckMagic($response, "responseGetLastname()", caller()); return $$response[RESPONSE_INDEX_LASTNAME]; } # Inputs: (response, lastname)) # where response is the response to modify, # and 'lastname' is lastname to be set # Returns: nothing (but modifies the response that was provided, # setting the lastname of that response)) sub responseSetLastname() { my($response, $lastname) = @_; responseCheckMagic($response, "responseSetLastname()", caller()); $$response[RESPONSE_INDEX_LASTNAME] = $lastname; } # Inputs: a response # Returns: the score of that response, or undef sub responseGetScore { my($response) = @_; # get the input parameter responseCheckMagic($response, "responseGetScore()", caller()); return $$response[RESPONSE_INDEX_SCORE]; } # Inputs: (response, score)) # where response is the response to modify, # and 'score' is the response score to be set # Returns: nothing (but modifies the response that was provided, # setting the score of that response)) sub responseSetScore() { my($response, $score) = @_; responseCheckMagic($response, "responseSetScore()", caller()); $$response[RESPONSE_INDEX_SCORE] = $score; } 1;