Re: perl scripting in AppleScript
Re: perl scripting in AppleScript
- Subject: Re: perl scripting in AppleScript
- From: John Fowler <email@hidden>
- Date: Tue, 23 Dec 2003 23:33:28 -0600
On Dec 23, 2003, at 3:03 PM, email@hidden
wrote:
>
your perl script is reading "lines" of a file which has line endings
>
it doesn't accept. The result is that perl reads the whole file as one
>
line. Set $/ to "\r" for Mac files. \n is the default. You can also
>
use the -octal switch in perl. It's really a dash-zero-followed by the
>
octal representation of the line end character.
Thanks for the lead.
The perlrun manpage gives me the switch -0777 which allows the perl
script to as the page puts it "slurp" the whole file not just lines.
The perlre manpage gives the s match modifier which modifies the
behavior of the .* wildcard to match line endings as well as regular
characters.
Thus the following returns multiline matches if the input thestring is
a regular expression including a parenthesized regular expression.
set theFile to "Macintosh HD:Users:johnfowler:Desktop:myactivity.txt"
set thestring to "x(.*)y"
perlregexfind((theFile), thestring)
on perlregexfind(theFile, thestring)
set theFile to convertFolder(theFile)
try
set shellscript to "/usr/bin/perl -0777e 'open (INFILE,\"" & theFile
& "\")||die (\"here\");$thisvar=<INFILE>;$thisvar=~m/" & thestring &
"/s;print $1;'"
set theResult to (do shell script shellscript)
return theResult
on error thiserror
return thiserror
end try
end perlregexfind
on convertFolder(AplFolderString)
set unixFolderString to perlreplace((item 2 of
getEveryTextItem(AplFolderString, "Macintosh HD:")), ":", "/")
return unixFolderString
end convertFolder
on getEveryTextItem(thestring, thedelimiter)
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to thedelimiter
set theVarList to every text item of thestring
set AppleScript's text item delimiters to oldDelims
--display dialog (theVarList as text)
return theVarList
end getEveryTextItem
on perlreplace(inputstring, targetstring, replacementstring)
log ("/usr/bin/perl -e '$thisvar=" & inputstring & ";$thisvar=~s/" &
targetstring & "/" & replacementstring & "/;print $thisvar;'")
set theResult to (do shell script "/usr/bin/perl -e '$thisvar=\"" &
inputstring & "\";$thisvar=~s/\\" & targetstring & "/\\" &
replacementstring & "/g;print $thisvar;'")
end perlreplace
Now that I understand certain aspects of this I believe I might be able
to accomplish the same end using an inline variable, which is where I
started, because my target files are as far as I can see never in
excess of the limit on file size.
Appreciate all the feedback and larnin.
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.