## Forum archive 2000-2006

### Michael Gage - HowTo write a filter

by Arnold Pizer -
Number of replies: 0
 HowTo write a filter topic started 4/27/2003; 1:13:43 PMlast post 4/27/2003; 1:13:43 PM
Michael Gage - HowTo write a filter
4/27/2003; 1:13:43 PM (reads: 669, responses: 0)

### How to write a filter

A filter is a short subroutine with the following structure. It accepts an AnswerHash, followed by a hash of options. It returns an AnswerHash

		$ans_hash = filter($ans_hash, %options);

See the AnswerHash.pm file for a list of entries which can be expected to be found in an AnswerHash, such as 'student_ans', 'score' and so forth. Other entries may be present for specialized answer evaluators.

The hope is that a well designed set of filters can easily be combined to form a new answer_evaluator and that this method will produce answer evaluators which are are more robust than the method of copying existing answer evaluators and modifying them.

Here is an outline of how a filter is constructed:

	sub filter{		my $rh_ans = convert_to_AnswerHash(shift @_); # gets answerHash or converts string to AnswerHash my %options = @_; my$error = 0;		assign_option_aliases(%options,			'alias1'	=> 'option5'			'alias2'	=> 'option7'		);		set_default_options(%options,			'_filter_name'	       =>	'filter',			'option5'		=>  .0001,			'option7'		=>	'ascii',			'allow_unknown_options	=>	0,		)		.... body code of filter .......		if ($error) {$rh_ans->throw_error("FILTER_ERROR", "Something went wrong");			# see AnswerHash.pm for details on using the throw_error method.		}		\$rh_ans;  #reference to an AnswerHash object is returned.	}

<| Post or View Comments |>