hmbscully has asked for the wisdom of the Perl Monks concerning the following question:
to<!--#include virtual="/ssi/edfooter.txt"-->
The included file paths can remain the same, just what's wrapped around the paths needs to change. There are some pages that will have more than one include to be updated.<?php include($_SERVER['DOCUMENT_ROOT'].'/ssi/edfooter.txt'); ?>
I understand that a command line solution for this will be suggested, I was unsuccessful when trying to get the regex to not throw errors for unescaped characters when trying a command line solution. I'd rather have the code in script form. I think what I am doing now is one semi-correct way of doing it, but I'm never entirely sure.
What I'm not doing right is editing the file, either in place or with a temporary file. I keep either wiping out the file completely or not doing anything to it. This isn't a script that is going to be called on any regular basis, so I'm not worried about it being efficient in the long-term, though I would like to understand some of the better ways of doing this.
Thanks!
#!/usr/bin/perl use warnings; use strict; use File::Find::Rule; #find all html files in specified directory #this is for a specific directory right now for testing, but #will eventually be going through all the subdirectories under /htdocs my $dir = "/home/devcorp/htdocs/plan/norms"; my $rule = File::Find::Rule->file->name("*.html")->start( $dir ); #keep track of the changed files in a file open(OUTFILE,">changed_files.txt") || die "cant open changed_files.txt +, $!\n"; while ( my $html_file = $rule->match ) { #open file to replace string in open FILE, "<$html_file"; my @lines = <FILE>; for (@lines) { #replace <!--#include virtual="[document path]"--> #with <?php include($_SERVER['DOCUMENT_ROOT'].'[document path] +'); ?> if (s/<!--#include virtual="(.*)"-->/<?php include(\$\_SER +VER['DOCUMENT_ROOT'].'$1');?>/){ my $result = $1; #print the file changed and the document path for the inc +luded file print OUTFILE "$html_file: $result\n"; } } close FILE; } close OUTFILE;
Output returned in changed_files.txt is
/home/devcorp/htdocs/plan/norms/index.html: /ssi/edfooter.txt
but obviously nothing is changed in the file itself because I'm not doing that part right.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: modify file in place in script? Regex for changing includes from SSI to PHP
by ikegami (Patriarch) on Oct 26, 2007 at 18:32 UTC | |
by hmbscully (Scribe) on Oct 26, 2007 at 19:23 UTC | |
by ikegami (Patriarch) on Oct 27, 2007 at 16:07 UTC | |
by hmbscully (Scribe) on Oct 26, 2007 at 20:03 UTC | |
|
Re: modify file in place in script? Regex for changing includes from SSI to PHP
by tuxz0r (Pilgrim) on Oct 26, 2007 at 19:33 UTC | |
|
Re: modify file in place with perl -i
by Anonymous Monk on Oct 26, 2007 at 22:19 UTC | |
|
Re: modify file in place in script? Regex for changing includes from SSI to PHP
by Your Mother (Archbishop) on Oct 27, 2007 at 03:52 UTC |