<!-- NEW URL GOES HERE -->
# 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";
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |