The way I've handled this in the past is to place a textual marker in the HTML form that can be ferretted out with a regex, and then do a subtitution of the marker with the new URL followed by the same marker - so that the next time you need to add another URL to the form, the marker is at the point where another new URL can be added.
I usually create the marker in the form file as an HTML comment, i.e.
<!-- NEW URL GOES HERE -->
Let's say that in my Perl script I've got the new URL in scalar variable $url. The following code snippet would suck in the entire file as a single record, then perform the substitution and write it back out:
# these two lines knock out Perl's default behavior of reading in
# a file as separate lines of data delimited by newline chars (\n):
local $/;
$/ = undef;
# now we can bring the entire file in and store it as a scalar
# with a single read instruction... the newlines are buried
# in with the text, but since Perl treats this file as one big
# long record now, it doesn't care:
open(FORMFILE,"form.html");
$formtext = (<FORMFILE>);
close(FORMFILE);
# for convenience's sake, we have the marker text in a scalar:
$marker = "<!-- NEW URL GOES HERE -->";
# we put the new URL and the marker in a string, separated by an
# HTML linebreak and a standard newline:
$swapin = $url . "<BR>\n" . $marker;
# now perform a regex substitution, swapping in the new URL and
# the marker where before there was just the marker by itself:
$formtext =~ s/$marker/$swapin/s;
# (the "s" at the end of the regex tells the Perl regex engine to
# ignore newline chars in handling $formtext - otherwise the regex
# engine will only perform this regex statement on the text from
# the beginning of $formtext to the first occurrence of a newline.)
# Now we can write $formtext back out:
open(FORMFILE,">form.html");
print FORMFILE $formtext;
close FORMFILE;
# if you're going to be doing any normal (i.e. newline-delimited)
# file I/O after this point, be sure to reset $/ to be the newline
# character:
$/ = "\n";
That should do it.
|