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.

In reply to RE: This should be simple but I cant seem to figure it out by Doc Technical
in thread This should be simple but I cant seem to figure it out by perl_newbie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.