PERL question (right forum?)

This is for my web site where I’m trying to create a program that sends form input to a page where I can edit its contents if need be then send that to another page (HTML) that I choose to send it to.

I’ve got the admin thing set and ready, and I can get it to go to the page I want it to, but part of the “send to another page I choose” thing is editing the contents of the page (removing </body> and the like tags) before I write to it.

First I thought:

“Ok, I’ll put the page into an array, then search/replace the item in the array I want gone.”

Then I find that you can’t search/replace in an array. (Or so my book tells me, if I can, please tell me how)

So I try to figure out how to convert an array to a normal string variable. But I can’t. This is where you come in.

How do I switch an array to a normal string variable?
If there’s a better way to do what I want done, I’d also like to know that.

Thank you very much.

If you have the strings in array, you can just loop through the array:



for (@myarray) {
    s/<body>//gi;
    s/</body>//gi;
    # etc.
}


Depends what you mean by “search/replace in an array.”

You can search/replace each element in an array by simply looping through it:



s/foo/bar/g for @array


To turn an array into a scalar you can use the concatination operator thusly:



my $stuff;
$stuff .= $_ for @array;


Or you can read the file into a scalar one line at a time:



my $stuff;
$stuff .= $_ while (<FILE>);


Or you can locally set the input record seperator to undef to slurp the file all at once:



local $/;
my $stuff = <FILE>;


See perlopentut, perlvar and perlsyn for more information.

Finally, a stylistic note that really irks me: It’s Perl, not PERL! Note the initial capitalization, and the lack of capitalization thereafter. Larry dreamed up the name first (it was originally Pearl, BTW) and then made up an expansion to fit it. Therefore, it isn’t a true acronym and should not be capitalized as such.

(If you want to avoid confusion in a context where confusion over such things might arise, Perl is the abstract language that is implemented by the program perl (which is usually launched in a *nix-y environment by typing the command perl at a shell). Note the capitalization.)

Thanks a bunch Friedo! The s/foo/bar/g was the thing I needed.

Note to Derleth:

Y’know, that was the first time I ever wrote it as PERL…thanks for the explaination though.