Skip to page content or Skip to Accesskey List.

Work

Main Page Content

Easy Perl Persistence Faker

Rated 3.74 (Ratings: 1)

Want more?

  • More articles in Code
 

Michiel Trimpe

Member info

User since: 31 May 1999

Articles written: 1

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 ;

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.org Evolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.