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

Hi brethren, I have a problem coming to grips with writing changes to an HTML file via a filehandle. Hope the Abbey can cast light upon my coding darkness...what am I doing wrong?
#!/usr/bin/perl # write mods to HTML file.plx # Program will read in an html file, remove the img tag and rewrite HT +ML on E-drive. # 1. No need for file variable yet: open (INFILE, "<".$htmlFile) or di +e("Can't read source file!\n"); # 2. Alternative: m/<A\s+HREF=[^>]+>(.*?)<\/A>/ - Will not remove clo +sing tag though - why? # 3. Why is interpreter flipping-out over an 'undefined variable', whe +n # original regexp, m/<A\s+HREF=[^>]+>(.*?)<\/A>/, is known to work. + What am I missing? use warnings; use diagnostics; use strict; # Declare and initialise variables. my $pattern1 = '<IMG\s+(.*)>'; my $pattern2 = '<A\s+HREF\s*=[^>]+>'; my $pattern3 = '</A>'; my @htmlLines; my @htmlFile; # Open HTML test file and read into array. open INFILE, "E:/Documents and Settings/Richard Lamb/My Documents/HTML +/test1InDocCSS.html" or die "Sod! Can't open this file.\n"; @htmlLines = <INFILE>; close (INFILE); scrapImageTag(); scrapAnchorTag(); # Removes image tag elements in array sub scrapImageTag { foreach my $line (@htmlLines) { # replace <IMG ...> with nothing. $line =~ s/$pattern1//ig; # case insensitivity and global search +for pattern } } # Removes anchor tag elements in array sub scrapAnchorTag { foreach my $line (@htmlLines) { # replace <A HREF ...> with nothing. $line =~ s/$pattern2//ig; # case insensitivity and global search +for pattern $line =~ s/$pattern3//ig; # case insensitivity and global search +for pattern } } # Am I deleting the contents of the list with this? Not sure... open (OUTFILE, ">@htmlLines") or die("Can't rewrite the HTML file.\n") +; print OUTFILE "@htmlLines\n"; close (OUTFILE);
I'm trying to write the changed code back to the file on the hard-drive, by writing on a filehandle, so I can re-open the html document. I use a print operator? I have to come clean and say that the file writing's confusing the Hell out of me. Are file tests the answer, assign to a new list variable? Time to try. Here's the code I've written so far - the file won't open for writing (yet). Confusion!!! Rich

Replies are listed 'Best First'.
Re: File opening problems
by Abigail-II (Bishop) on Aug 19, 2003 at 12:00 UTC
    open (OUTFILE, ">@htmlLines")

    Are you sure about that second argument? Does the file name you want to write to indeed come from joining the elements of that array with spaces? Are you sure you don't want to use a regular file name?

    or die("Can't rewrite the HTML file.\n");

    For anything other than run-once programs, I include the filename I used in the open statement in the error message.

    Abigail

      You mean replace:
      open (OUTFILE, ">@htmlLines") or die("Can't rewrite the HTML file.\n") +;
      with this? The actual file name?
      open (OUTFILE, ">E:/Documents and Settings/Richard Lamb/My Documents/H +TML/test1InDocCSS.html") or die ("Can't open E:/Documents and Setting +s/Richard Lamb/My Documents/HTML/test1InDocCSS.html\n");
      So I should be passing the file path as an argument, not the array variable I'm using to store the HTML? Many thanks, Richard
        open needs a filename, not the content of the file to be. If you wouldn't give the file, how is Perl supposed to know which file to open?
        my $file = "whatever"; open my $out, ">", $file or die "Failed to open $file: $!"; print $out @htmlLines; close $out or die "Failed to close $file: $!";

        Abigail

Re: File opening problems
by Not_a_Number (Prior) on Aug 19, 2003 at 12:06 UTC

    A couple of lines up from the bottom of your code:

    open (OUTFILE, ">@htmlLines") or die...

    Spot the typo?

    Say your array @htmlLines contained (Hello, world): you are opening a file named 'Hello world'!

    dave

      Ahh, so I should be replacing the array variable name with the actual file path, as in: open (OUTFILE, ">E:/Documents and Settings/Richard Lamb/My Documents/HTML/test1InDocCSS.html" or die... Simple mistakes. Doh.