All Perl programmers have to spend some time on making

semi-persistent pages. A lot of this time is spent on writing the code

and the form. Now to make your life just a tad easier I've written a

simple subroutine that will parse a form against some simple preset

criteria. This is the code :

sub ParseForm {

    # Get parameters

    my $formtext = shift ;

    my $formref = shift ;

    # Now get the real hash form the reference

    my %form = %{$formref} ;

    # Now we will parse the form for complex

    # replacements

    $formtext =~ s/\%(.+?)\=(.+?)\=\>(.+?)\<\>(.+?)\%/$form{$1} eq $2 ? $3 : $4/ego ;

    # And now we will do the same for simple

    # replacements

    $formtext =~ s/\%(.+?)\%/$form{$1}/ego ;

    # And return the result

    return $formtext ;

}

This code will replace %varname% with the appropriate variable from

the form hash and %varname=text to check for=>one value<>or the other%

will be replaced by "one value" if varname is text to check for or

"or the other" if not.

An amusing example in which this can be used is the following :

(application of this to a form is up to you)

# define some dumb variables<

%form = ( firstword => "marry" , secondword => "you" , ) ;

# Automatically flush the buffer after each write

$| = 1 ;

# Print the conversation

print "Will you marry?

Are you cheating on me ?

Answer: " ;

# Get the cheating question

$cheat = <STDIN> ;

chomp $cheat ; $cheat = lc($cheat) ;

# And get the answer into the form

$form{'cheating'} = $cheat ;

# Define the text to parse

$parsetext = qq~%cheating=no=>I will<>I will not% %firstword% %secondword%.~ ;

# And do the mumbo jumbo

print &ParseForm( $parsetext , \%form ) ;

# And get out of here

exit ;