Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Shell-Perl-AppleScript scripting



According to Chris Nebel:

> Getting the quoting of the substitution
> command right for all possible input strings is left as an exercise for
> the reader.

Boy, is it ever.

Here is a pretty good version of a perl-based find-and-replace handler.

on perlreplace(inputstring, targetstring, replacementstring)
set shellscript to "/usr/bin/perl -e '$rpl=q|" & replacementstring &
"|;$trgt=q|" & targetstring & "|;$thisvar=q|" & inputstring &
"|;$thisvar=~s/$trgt/$rpl/sg;print $thisvar;'"
log shellscript
set theResult to (do shell script shellscript)
return theResult
end perlreplace

The remaining problem appears to be that the quote character (| in this
case, but others are legal in perl) can't be part of the target or
replacement strings. As far as I can tell, no combination of
quotemeta() or "quoted version of" completely solves this problem. In
my trials, one version, with quotemeta() applied to the replacement
string, yielded output complete with the backslashes rather than
de-backslashed. Any suggestions would be welcomed.

John Fowler
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.