in reply to This should be simple but I cant seem to figure it out

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.

Replies are listed 'Best First'.
RE: RE: This should be simple but I cant seem to figure it out
by japhy (Canon) on Apr 14, 2000 at 15:10 UTC
    You do a bit of extra hocus here, and your justification of the /s modifier to your regex isn't quite right.
    $marker = "<!-- NEW URL HERE -->"; # let's use perl's built-in in-place editing # please check the 'perlrun' documentation for # explanation of the -i switch, and the 'perlvar' # documentation for the corresponding $^I variable { # $^I = "" <-- no backup file # @ARGV = ("foo.html") <-- magical <> works on this file local ($^I, @ARGV) = ("", "foo.html"); while (<>) { # \Q will escape any special regex chars in $marker # if any exist -- good safety measure! s/\Q$marker/$url<BR>\n$marker/; print; } }
    If you want to be a sneaky little guy, you could use a positive look-ahead assertion, to avoid having to substitute $marker with something AND $marker:
    # stick "$url<BR>" RIGHT BEFORE $marker s/(?=\Q$marker)/$url<BR>\n/;
    Yadda yadda.