Any secret escape sequence code that only escape forward slashes?
s{/}{\\/}g
Don't try to do it all at once. Write a simple function that escape forward slashes, and call that in the replacement part of your substitution. You can use s{}{}ge to evaluated the replacement part, and comfortably call functions there.
| [reply] [Watch: Dir/Any] [d/l] [select] |
I don't know much about PHP, but here is a straightforward idea that might make some sense or I hope at least give you something to work from. The general idea is to break this down into smaller "chunks". I assume that the code that your Perl program "re-writes" does compile and that parens match up, etc. Compressing the spaces is also a simplifying assumption.
#!/usr/bin/perl -w
use strict;
my $line = q{ split( '/', $string ) };
$line =~ s/ //g; #compress spaces
#now just "split('/',$string)"
if ($line =~ m/^split/) # check for the split keyword
{
# get (parm1, parm2) of the split, ie the two
# things separated by commas within the parens
my ($parm1,$parm2) = ($line =~ m/\((.*?),(.*?)\)/)[0,1];
# now get stuff between quotes in parm 1
my $inside_quote = ($parm1 =~ m|'(.*?)'|)[0];
# change any / to \/
$inside_quote =~ s|/|\\/|g;
#now just print back out
print "preg_split('/$inside_quote/',$parm2)\n";
}
__END__
Prints:
preg_split('/\//',$string)
| [reply] [Watch: Dir/Any] [d/l] |
s/
(?<!_)
( split \s* \( \s* )
(
" (?:[^\\"]+|\\.)* "
|
' (?:[^\\']+|\\.)* '
)
/
my @x = ($1,$2);
$x[1] =~ s{/}{\\/}g;
"preg_$x[0]$x[1]"
/xesg
Untested.
Will create an error if the "/" is already escaped. | [reply] [Watch: Dir/Any] [d/l] [select] |
‘Dumb’ search-and-replaces (and I mean that as a comment on the code, not on you!) always strike fear in my heart; if we can't even parse such a rigid language as XML with regexes (which we can't, right? Or at least no sane person would?), how can we expect correctly to parse the rich grammar of a programming language? I always think of clbuttic.
If this were my job, the first thing I'd do would be to look at some means of getting at the internal, not textual, representation of a PHP program. The first result for PHP + AST is php-ast; I'm not sure if it does what you want, but you might be able to fold, spindle, and mutilate it, or else just look further down in the results.
| [reply] [Watch: Dir/Any] |
Dumb search-and-replaces always strike fear in my heart
I do dumb search-and-replaces a hundred times a day, mostly with my editor's search and replace box. There's nothing wrong with it if the process is supervised.
In the OP's case, he can use a visual diff tool — I love Beyond Compare — to compare the pre- and post-change versions of his files and fix the mistakes.
And then he has to do a manual search to change the ones the tool didn't catch.
| [reply] [Watch: Dir/Any] |
Thanks a LOT for the answers.
It's now working as I wanted here:
http://nthinking.net/scripts/php5-3Migration/repDeprec.php
Basically as you said I had to separate the job in easier chunks.
As for the search and replace "paradigm", I do agree it's not the best solution...It is actually a very stressful way to go. I have been bumping into automatic code modification for few times now and the AST approach is definitely the way to go.
My solution can't deal with cases like:
split($myreg,$string)
where:
$myreg = '/'
But I guess this approach will do enough for what it is intend for...
Thanks Again!
| [reply] [Watch: Dir/Any] [d/l] [select] |