joev has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to convert a FILEHANDLE to a string so I can search and replace text and then rewrite the file.

#!/usr/bin/perl -w use strict; my $file = "/var/www/html/acs/new/index.html"; my $file_bu = "/var/www/html/acs/new/index_bu.html"; open (FILE, "+<$file") or die "Can't open file: $!"; open (FILE_BU, ">$file_bu") or die "Can't open file: $!"; # Save a back up of the original file my @file = <FILE>; #print "@file\n"; print FILE_BU "@file"; close FILE_BU; # Convert file to string for search and replace my $string = do {local $/; <FILE>}; if (defined $string) {print "defined\n"}; print "$string\n"; close FILE;

Results:

[joev@localhost test]$ ./file_handle.pl defined [joev@localhost test]$

Why does nothing get printed out for $string even though it is defined? I expected $string to be printed the same way as print "@file\n";

Is there a better way to accomplish this?

Thanks
Joe

Replies are listed 'Best First'.
Re: Converting FILEHANDLE to string
by jeffa (Bishop) on Nov 27, 2003 at 00:28 UTC

    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)
    
      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.
      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)
        
Re: Converting FILEHANDLE to string
by Roger (Parson) on Nov 26, 2003 at 23:57 UTC
    You could do better if you rewind the file with seek FILE, 0, 0; before trying to read it again.

Re: Converting FILEHANDLE to string
by ysth (Canon) on Nov 27, 2003 at 03:21 UTC
    Reading a file in slurp mode sometimes (always?) returns an empty string at end of file, instead of the more expected undef. I believe there are perlbug reports open on this.

    Your problem is that you have read all the data in the file, so there is nothing left to read. Either rewind the file (seek FILE, 0, 0, or read it into $string originally instead of into @file. There is no problem with saying print FILE_BU $string; and then modifying $string and writing it back out to FILE (after rewinding it yet again, or you will just be appending to the original data.)

Re: Converting FILEHANDLE to string
by b10m (Vicar) on Nov 27, 2003 at 00:07 UTC
    Maybe I'm seeing this wrong, but why not search and replace instead of backing the file up? So, something like:
    open (FILE, "<$file") or die "Can't open $file"; open (FILE_BU, ">$file_bu") or die "Can't open $file_bu"; while (<FILE>) { $_ =~ s/search/replace/g; print FILE_BU "$_"; } close FILE_BU; close FILE;
    Then you could copy (or move) the new file over the old one, or check the result visually and do that manually.

    Update: Especially with huge files, I would actually prefer this method over the "my @file = <FILE>" part.

    --
    B10m
Re: Converting FILEHANDLE to string
by quinkan (Monk) on Nov 29, 2003 at 23:27 UTC
    Check CPAN for modules. File::Slurp does this sort of thing, and has just been modified to extend its functions.
    use File::Slurp; my $string = read_file("yourfile"); write_file("yourfile.bak", $string); $string =~ s/everything/otherthings/g; $string .= "endbit\n"; write_file("newfile", $string);
    This has problems if your file/string is huge. There is also a Tie::Slurp module or somesuch but I admit I haven't had it working yet.