in reply to Converting FILEHANDLE to string

Instead of slurping the file into a scalar (that's what i call converting a filehandle to a string), did you know that Perl has an "in-place editing" mode?

perl -pi.bak -e"s/this/that/g" /var/www/html/acs/new/index.html
This will replace all accurrances of this with that in the specified file and make a backup copy for you (i chose a .bak extension). Doesn't answer your question, but it is a much better way to accomplish the task, IMHO. ;)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: Converting FILEHANDLE to string
by b10m (Vicar) on Nov 27, 2003 at 00:38 UTC
    While we're at it, the OP might as well take a look at the manual of sed ;) Not a Perl'ish way to solve it, but a rather fast and good one.
    --
    B10m
      And while he's at it, he might as well take a look at the manual of X. X can solve the problem, even if it doesn't answer the question or help the op learn anything about perl. Yes, X is the way to go.
        I'm sorry to rain on your parade, mr. Anonymous Monk, but Perl is sometimes not the best solution for a problem. Yes, this is a place where Perl is discussed, but it's also a place where people come to have their problems solved. In this case, Jeffa's solution will probably do wonders, but why launch a bulky Perl interpreter, when you have something like sed at your finger tips?

        Am I allowed to post non-Perl solutions to "problems" on here anyway? If not, please accept my sincere apologies.

        --
        B10m
Re: Re: Converting FILEHANDLE to string
by joev (Novice) on Nov 29, 2003 at 15:44 UTC
    Thanks. I wasn't aware of exactly how to use the "one liners." This will need to be called from with in a CGI script so I'm using system:
    # Backup existing index.html file and then edit it my $edit = "perl -pi.bak -e 's/Old Spotlight/Product Spotlight/' /var/ +www/html/acs/new/index.html"; system ($edit);
    I kept getting sysntax errors when trying to make the call directly from the script. Is this the correct usage?

    This is being developed so a client can make certain changes to his index.html file from his site. I doesn't want to be bothered with or learning how to actually edit any html code.From a security stand point, I'm only going to allow word characters to be passed to the call.Will this be OK?

    Thanks again
    Joe

      Yuck!

      Sorry, had to get that out of the way first. :P Here is what i would do: use a database! All you need to store is a unique identifier for the user and the contents of the page itself. Then, your CGI script accepts the user id as a parameter, fetches the page for that uid, and then displays it in a text area that uses HTMLArea to turn the text area box into a ... yes it really works ... WYSIWYG HTML editor. Now your client doesn't even have to know how to "code" HTML. :)

      I suppose you could achieve the same effects by opening the index file, reading the contents into the text area and re-writing the index file upon submit ... and if you really only need to do this for one user then the only two hard parts you have will be:

      1. setting the permissions so you can overwrite the file
      2. getting HTMLArea up and running and configuring it
      You shouldn't even have to worry about flocking the index file if only one person is going to be editting it. (And this is another reason why i use databases - i don't have to worry about file locking race conditions.)

      Oh, and by the way, your system usage looks wrong. Try passing a list instead:

      system ( 'perl', '-pi.bak', '-e', 's/Old Spotlight/Product Spotlight/', '/var/www/html/acs/new/index.html', );
      But remember that if you don't have the proper permissions set for the index file, you can't write to it. And please, please do consider using HTMLArea instead of search and replace substitution. Your client will love you for it. :)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        AT a quick glance, HTMLArea will give them too much power to potentially screw up the page and it appears to run on Window$ only.
        No Good.

        My system call does work on my production server though I haven't tried it on the actual host. I will give your sample a try.

        So then, is it better to use FILEHANDLE to open the file and make the substitutions needed for the product number and category in the link tags and the desrciption or make them using the inplace-editing mode using system?

        Thanks Again for all the help and suggestions
        Joe