hi ho:

> Hi there... can one of you Perl wizards out there shoot me
> along a script I could use such that fields entered by a user
> on a form will be plugged into a corresponding HTML page?

this is how i do that kind of thing..

the basic operation of the program is to open a tagged file,
read the input from the form, and then substitute the user's
form input wherever it finds a corresponding tag.   then it
prints the result as a webpage.. pretty much what you spec'd, i
think.

any tags which are undefined in the form will be eliminated, and
i tossed a couple of dingi into the regular expression that
looks for tags to make them case-insensitive and
whitespace-tolerant.. i.e., the data from:

    <input type="text" name="family">

will replace any of the following tags:

    %family%
    %FAMILY%
    % family %
    % FAMILY %


this script does require my own library of CGI routines, which
i've posted before and will post again.   all you have to do is
put that file in the same directory as this script, and the
perl interpreter will know how to handle the rest.




mike




----  edit_style.pl  ----
#!/usr/local/bin/perl

require CGI;


$STYLE_TEMPLATE  = "/path/to/the/tagged_file.html";


&CGI::read_stdin();
print "content-type: text/html\n\n";
print &edit_template( read_file ( $STYLE_TEMPLATE ) );

exit (0);


####  SUBROUTINES  #############################################


#   edit_template ( template )
#
#   takes a string which contains tags of the form:
#
#       %tag_name%
#
#   as input, and calls a library routine to see what form variables
#   have been defined by the user.   if there are tags in the file
#   whose name match a form variable, the value the user defined for
#   that variable will replace those tags.   once all the variables
#   have been checked, any remaining tags are eliminated, and the
#   formatted string is returned.

sub edit_template {
    my $tmpl = shift;

    my @list = &CGI::get_names;

    for $item (@list) {
        $val  = &CGI::get_values ($item);
        $tmpl =~ s/%\s+$item\s+%/$val/gi;
    }
    $tmpl =~ s/%\s+\w+\s+%//g;

    return $tmpl;
}



#   read_file ( filepath )
#
#   takes a filepath as input, and tries to read the contents into a
#   single, scalar variable.   if it succeeds, it returns that
#   scalar.   if it can't open the file, it calls a library routine
#   to report the error in a way the web browser can display.

sub read_file {
    my $file = shift;

    my $tmp = $/;
    undef $/;

    open (FILE, "./$file") or
        &CGI::error ( 'File', qq( Couldn't open "$file": $!) );
    my $data = <FILE>;
    close FILE;

    $/ = $tmp;

    return $data;
}


1;


----  EOF  ----







mike stone  <[EMAIL PROTECTED]>   'net geek..
been there, done that,  have network, will travel.

Reply via email to