in reply to Massive File Editing

Problem #1 : You are invoking opendir(DIR) recursively, meaning that closedir(DIR) will generate system errors on the way back up. Still, this should not present a problem, as you are invoking readdir(DIR) in list context before recursing, so your results should not be affected. Either move the closedir() immediately after the readdir(), or use a lexical (my $dir) instead of a global (DIR).

Problem #2 : The regexp /.shtml/i is not anchored to the end of string (/.shtml$/i or /.shtml\z/i), and the '.' is not escaped to make it literal (/\.shtml$/i or /\.shtml\z/i). Meaning - any file that contains the string "shtml" after the first character will match. Again, this is not probable, and the problem would be that too many files were processed, instead of too few, so this is not likely to be your problem.

Sorry... I don't see any obvious errors other than the above two. What behaviour are you seeing that you find to be unexpected?

Replies are listed 'Best First'.
Re: Re: Massive File Editing
by Kage (Scribe) on Dec 15, 2002 at 07:17 UTC
    Nothing unexpected, I just can't make any good way to replace every instance of an anchor href of "main.php?page=..." to "/?id=..." in every .shtml file.
    “A script is what you give the actors. A program is what you give the audience.” ~ Larry Wall

      The easiest solution (although not the most efficient) would be to invoke Perl from within Perl. For example, use the loops you already have defined to gather a set of path names into an array. Then, invoke:

      system('perl', '-I.bak', '-pe', 's[main.php\?page=][/?id=]g', @pathnam +es) == 0 or die "Subcommand failed with return code $?.\n";

      Try to make the regexp as accurate and complete as possible to avoid incorrect alterations. If you want a longer term solution that is a bit more efficient, see the perlrun manpage to see an example of the code that approximates the behaviour of -pI.bak.

      NOTE: If the above system() invocation does not work, try changing 'perl' to be $^X ($EXECUTABLE_NAME with 'use English'). If the regular expression becomes very complicated, it may be easier to store the command in a perl script, and use the sub-script name instead of -e '...' in the system() invocation.

      First open the file and read the content into a scalar
      Then do a
      $fileContent =~ s/(<a href=")main\.php\?page\=/$1\/\?id\=/g;
      And then write the new contents back to the same file (or a new one with an added extension like .new or so