Nobumi Iyanaga
You can download MacPerl from:
v. 1.0b4
Tue, Sep 15, 1998
http://err.ethz.ch/~neeri/macintosh/perl.html
If you deal with Japanese text, it is better to use MacJPerl, which can be downloaded from:
http://world.std.com/~habilis/macjperl/
Download Macperl glue table here (80K to download).
Note on this version
I fixed some bugs and added a new tableMacPerl.regexFunctions
, containing
extract (str, pat, delimiter = "\r", ignoreCase = false)
- extractOffsets (str, pat, ignoreCase = false)
I also added a new documentation at
MacPerl.["*Tips*"]
. I hope this will be most useful.
Notes on the version 1.0b3:
I fixed some bugs and added some new features. The changes are noted in MacPer.["*changes*"], but here are the main ones:
- I wrote a new script,
MacPerl.runScripts.executeScript ()
on a suggestion of Christian Huldt.
This script takes six parameters:
scriptNameOrFpath
is a string parameter which may be a MacPerl script itself (then it MUST contain the line "#!perl"), or the path of a file containing a MacPerl script (droplets seem not to work), or the name of MacPerl script stored in the "MacPerl.scripts
" table or "user.MacPerl.scripts
" table (if you write yourself new MacPerl scripts, please store them in the table "user.MacPerl.scripts
", so that if I upgrade this table, I will be sure to not alter the user specifis scripts!).
The script is supposed to not contain explicit line of"@ARGV = (....)"
.
ARGList
is an optional list parameter containing all the arguments that you want to pass to the Perl script. These arguments will be set to the implicit"@ARGV"
list in the Perl script.
syntaxCheck
is an optional boolean parameter. If it is set totrue
, the first element of the list passed as parameter toMacPerl.doScript ()
will be"-c"
. If the Perl script contains any syntax error, this will cause an error (please note that this parameter does not work in the same way as thesyntaxCheck
parameter inMacPerl.runScripts.runScript ()
orMacPerl.runScripts.runEditorScript
!).
DATA
is an optional string parameter. You can append to the Perl script the data to be processed by it with this parameter. The Perl script must use the construct "<DATA>
" to have access to the DATA. The formula "__END__
" at the last of the script is optional (if it is not there, our script will add it...).
display
is an optional boolean parameter. If it is set totrue
, the Perl script will be displayed in a new document in MacPerl.
ifBringToFront
is an optional boolean parameter. If it is set totrue
, MacPerl will be the frontmost application during the execution of the script.
- I added a new script,
MacPerl.utilities.jumpToScriptLine ()
in the tableMacPerl.utilities
.
This script takes two parameters:
linenum
: a required long type parameter, indicating the line number of a Perl script to which you want to jump to.
scriptDocPath
, an optional string parameter. If it is specified, it must be the path name of the script.
Notes on the version 1.0b2:
I fixed some bugs and added some new features. The changes are noted in MacPer.["*changes*"], but here are the main ones:
- I made a new table,
MacPer.lib
, and table in it,MacPerl.lib.libScripts
and wrote a script,MacPerl.lib.runLibScript ()
.
This script takes five parameters:
libScriptName
is a string parameter which must be the name of an existing "lib" wp object either in the tableMacPerl.lib.libScripts
or in the tableuser.MacPerl.lib.libScripts
(if you write new "lib scripts", please store them inuser.MacPerl.lib.libScripts
!).subroutineName = ""
is an optional string parameter. If it is omitted, the same name as thelibScriptName
is given to thesubroutineName
; in that case, there must be a Perl sub-routine of the same name in the wp object indicated bylibScriptName
. If asubroutineName
is specified, the script will call that sub-routine in the wp object indicated bylibScriptName
. (This means that any way, there must be a Perl sub-routine of the name in the wp object indicated bysubroutineName
.)argList = {}
is an optional list parameter. With that parameter, you can pass parameters to the called sub-routine.toList = false
is an optional boolean parameter. You must set it totrue
, if you expect a list value as the result. Default is set tofalse
.display = false
is an optional boolean parameter. If it is set totrue
, the Perl script generated by this script will be displayed in a new document window in MacPerl. It can be useful for debugging.
This script works in the following way:This will generate the followind Perl script:
Suppose that we have this sub-routine inMacPerl.lib.libScripts.strings
:and we call this sub-routine with this script:sub stringInsert { local ($str, $idx, $toInsert) = @_; local ($res); local ($tempStr); if ($idx > length ($str)) { $idx = length ($str); } $tempStr = substr ($str, $idx); substr ($str, $idx) = $toInsert; $res = $str . $tempStr; return $res; }MacPerl.lib.runLibScript ("strings", "stringInsert", {"12345", 2, "abc"})which will return$temp = '12345'; print &stringInsert($temp, 2, 'abc');sub stringInsert { local ($str, $idx, $toInsert) = @_; local ($res); local ($tempStr); if ($idx > length ($str)) { $idx = length ($str); } $tempStr = substr ($str, $idx); substr ($str, $idx) = $toInsert; $res = $str . $tempStr; return $res; }
"12abc345"
.
Thus, you can use Perl sub-routines just like UserTalk verbs, passing directly Frontier variables to them and receive the results as Frontier variables.
MacPerl.runScripts
, i.e. MacPerl.runScripts.runScript ()
and MacPerl.runScripts.runEditorScript ()
. The sub-routine scripts will simply be appended to the Perl scripts generated by these scripts, so that you can make calls to the appended sub-routines in your Perl scripts. It is the last optional parameters in these two scripts, named "libUse = true"
. This is a list value, so that you can append more than one "lib scripts" to your Perl scripts.runEditorScript ("$a = \"abcde\"; $b = \\"12345"; \rprint &stringInsert\ ($a, 3, $b);\r\r", libUse:{"strings"})This returns "abc12345de".
MacPerl.runScripts.runScript ()
and MacPerl.runScripts.runEditorScript ()
, I added a new optional boolean parameter "display = false
". It works just as the same parameter in MacPerl.lib.runLibScript ()
(see above).
MacPerl.utilities
:
getDocWindowContent ()
getSTDOUTContent ()
setWindowTo (perlScript, new = false)
This glue table for MacPerl was created with Frontier's CommercialDevelopers suite. There are however some tweaks that I made:
MacPerl.eventInfo.id
" and "MacPerl.eventInfo.batch
", which I use a lot, are duplicated in the MacPerl table itself.
"MacPerl.doScript ()"
: I added the optional parameter "check = nil"
(and consequently the keyword 'CHCK', boolean (check)
) and I removed the coercing verb "string"
from the first parameter "s"
(the Perl script parameter).
Thus, if you set the "check = nil"
to "true"
, MacPerl will attempt to run in the "check syntax" mode. On the other hand, you can pass a list data as the first parameter for "MacPerl.doScript ()"
. The first element of the script may be a switch, and the second element a Perl script. For example, a script like this:
MacPerl.doScript ({"-c", "print 'hello world';"}, mode:batch)
will be the same thing as the following Perl script:
#!perl -c
print "hello world";
- MacPerl.perlVerbs.deleteSameLines
- MacPerl.perlVerbs.differenceOfTwoLists
- MacPerl.perlVerbs.grep
- MacPerl.perlVerbs.intersectionOfTwoLists
- MacPerl.perlVerbs.simpleGrep
- MacPerl.perlVerbs.sort
- MacPerl.perlVerbs.split
- MacPerl.perlVerbs.unique
MacPerl.perlVerbs.verbScripts
containing itself some wpText objects.
The eight scripts behave like Frontier UCMD's, although, since they call MacPerl, they are certainly slower than UCMD's. You can pass string parameters to these "verbs", and get the return values from MacPerl.
There are examples in each of these scripts, so that it should be easy to use them.
Examining the script "
MacPerl.perlVerbs.deleteSameLines
", and "unique
", you will see how it is possible to write any "MacPerl verbs" within Frontier. The only problem is how to pass the data to be processed by MacPerl to a MacPerl script. It is generally easy to do: we only have to replace the value of some variables in MacPerl script with the data to be passed, or even better, use the DATA filehandle to pass the data...
This technique of using the DATA filehandle is used also in the script "
MacPerl.runScripts.runScript
" (see below)
In this table, the most important script is "MacPerl.runScripts.runScript ()
". This script runs mainly MacPerl scripts that are stored in the table "MacPerl.scripts" or "user.MacPerl.scripts.
The first parameter, "
scriptNameOrFpath
" may be a MacPerl script itself (then it MUST contain the line "#!perl"), or the path of a file containing a MacPerl script, or the name of MacPerl script stored in the "MacPerl.scripts
" table or "user.MacPerl.scripts
" table (if you write yourself new MacPerl scripts, please store them in the table "user.MacPerl.scripts
", so that if I upgrade this table, I will be sure to not alter the user specifis scripts!).
This script is loaded in the variable "perlScript
" as a string.
The second parameter, "
ARGList
" is a list of values to be passed as parameters to the MacPerl script. These parameters are represented in a special way in the MacPerl scripts: they are noted as"1#@#", "2#@#", "3#@#"
..., and so on; and they are replaced by the elements of the list "ARGList
" in the script. If an element of that list is a list itself, it is converted in a MacPerl format of list (or array) by the script "MacPerl.utilities.frontierList2PerlList
".
Here is an example: There is a MacPerl script named "
simplestGrep
" in theMacPerl.scripts
table. It takes two parameters:
and@ARGV = 1#@#; # this is the file path(s)
If you call MacPerl.runScripts.runScript in this way:$term = '2#@#'; # this is the keyword
these parameters will be like this:
MacPerl.runScripts.runScript ("simplestGrep", {{"Macintosh HD:Desktop Folder:test.txt"}, "keyword"})
@ARGV = ('Macintosh HD:Desktop Folder:test.txt');
$term = 'keyword';
Notice the use of single quotes in the MacPerl script (the list parameter [here, "
1#@#
"] is not enclosed between ' and ', while the scalar parameter is enclosed between ' and '...!)
The third parameter,
optionsList
, is another list parameter. The list elements must refer to the contents of the tableMacPerl.scripts.scriptOptions
or the tableuser.MacPerl.scripts.scriptOptions
. For example, there is a MacPerl script named "removeData
" inMacPerl.scripts
table. Its content is:
if you call
#!perl @ARGV = 1#@#;$term = '2#@#' ; #to remove line, ex.: "^.*HEAD.*\n" if ($term eq "") { die ("Canceled...\n"); }
#$replace = '3#@#';
if (length($term)> 1) { for $file (@ARGV){ open (FILE, $file) || die "Couldn't open $file file\n"; while(<FILE>) { s/$term//g; print ; } } }
MacPerl.runScripts.runScript ()
in this way:
runScript ("romoveData", {{"Macintosh HD:Desktop Folder:test.txt"},\ "keyword", "replaceword"}, {"replace"})
The MacPerl script will be changed like this:
#!perl @ARGV = ('Macintosh HD:Desktop Folder:test.txt');$term = 'keyword' ; #to remove line, ex.: "^.*HEAD.*\n" if ($term eq "") { die ("Canceled...\n"); }
$replace = 'replaceword';
if (length($term) > 1) { for $file (@ARGV){ open (FILE, $file) || die "Couldn't open $file file\n"; while(<FILE>) { s/$term/$replace/g; print ; } } }
There is a cell at "
MacPerl.scripts.scriptOptions.removeData.replace
", which contains a list value "{{"#$replace", "$replace"}, {"s/$term//g", "s/$term/$replace/g"}}
". As you can see by the above example, the word "#$replace
" in the MacPerl script is replaced by"$replace
" and the word"s/$term//g"
is replaced by "s/$term/$replace/g
".
With this mechanism, one MacPerl script can be used for multiple purposes.
There is another special option: "
useData
". If the first element of optionsList is "useData
", the scriptMacPerl.scripts.scriptOptions.useData ()
will transform the MacPerl script: it will first delete the line beginning with "@ARGV
"; it will replace "<>
" or "<FILE>
" with "<DATA>
"; it will replace also every occurrence of "FILE
" with "DATA
"; then it will add at the last of the MacPerl script a line: "__END__
", followed by the second element of the list optionsList, which is the "data
".
There are only two MacPerl scripts which can use this technique in MacPerl.scripts table (i.e. "
catScript
" and "dictionaryLookup
"); but I think that you can write yourself more scripts. The advantage of this technique is that instead of reading file(s), MacPerl scripts can use data passed directly from Frontier.
The third parameter, "
scriptModifyList
" expects a list value, and will work just like theoptionsList
parameter. If you callMacPerl.runScripts.runScript
in this way:
you will have exactly the same MacPerl script as the above one. The only difference with the optionsList parameter is that the options are not stored; instead, you can modify MacPerl scripts "on the fly"...MacPerl.runScripts.runScript ("removeData", {{"Macintosh HD: Desktop Folder:test.txt"}, "keyword", "replaceword"}, scriptModifyList:{{"#$replace", "$replace"}, {"s/$term//g", "s/$term/$replace/g"}})
The fourth parameter,
switches
, is an optional parameter expecting Perl switches that will be appended to the line "#!perl
" (for example"#!perl -c
", for the syntax checking, etc.). In this case, the switches parameter must be "-c
". If there is no line "#!perl
" in the script, it will be added at the top. You may of course write needed switches in your script itself, and not use this parameter.
The fifth parameter,
syntaxCheck
, is an optional parameter expecting a boolean (default beingfalse
). If it istrue
, the Perl script will be sent to the scriptMacPerl.runScripts.checkSyntax ()
and the result of the syntax checking will be returned. Set the parametersyntaxCheck
totrue
and set the parameterswitches
to "-c
" will have exactly the same effect.
Finally, the sixth parameter,
ifBringToFront
will bring MacPerl to the front, if it is set totrue
(default beingfalse
. MacPerl is generally very fast; but if it has to do a very big job, it is faster when it is at the front.
The "core" of this script (
MacPerl.runScripts.runScript ()
) is the line:
The optional parameter, "res = doScript (perlScript, mode: batch)
mode: batch
", is the "magic word": without it, MacPerl will execute the given script inside itself; but if it is there, it will return the results to Frontier, so that you can do anything with them.
The script MacPerl.runScripts.runEditorScript ()
allows to use any scriptable editor or word processor as a MacPerl script editor, able to run MacPerl script.
It takes one required parameter, which is a Perl script, and three optional parameters:
switches
,syntaxCheck
andifBringToFront
(see above for these parameters)
If you have for example my glue table for Jedit 2, a simple script like this will make it a MacPerl script editor:
You can write your MacPerl script in a Jedit document; then select it, and choose this command in a OSA Menu item for Jedit -- it will execute the script, and the result will be displayed in a new document in Jedit.on MacPerlEditor () { local (perlScript = Jedit.basicUtilities.getSelection (), res); res = MacPerl.runScripts.runEditorScript (perlScript); jedit.basicUtilities.newDocumentWithStr (res)}
You may use the string cell at
MacPerl.scripts.scriptTemp
to pass MacPerl script to this script.
The script MacPerl.runScripts.checkSyntax ()
is a special script to do a syntax checking of a Perl script. It takes only one parameter, which is a Perl script, and will return the result of the syntax checking in MacPerl. This script is called from MacPerl.runScripts.runScript ()
and MacPerl.runScripts.runEditorScript ()
.
The first script in the table MacPerl.runScripts.otherMethods
, "commandLine doesn't work!! ()
", doesn't work.
The second script, MacPerl_Reply ()
, uses "&MacPerl'Reply ()
" function of MacPerl to return the result of MacPerl script in Frontier. I think this method is less flexible than that of the use of "MacPerl.doScript (perlScript, mode:batch)
".
The script "runScriptFile ()
" and "viaClipboard ()
" use essentially the same method as the one used in "MacPerl.doScript (perlScript, mode:batch)
".
MacPerl.runScripts.tests
is a set of oneliners showing the use of MacPerl scripts stored in the MacPerl.scripts table via the script "MacPerl.runScripts.runScript
".
MacPerl.scripts
" (except some of them, which have been written by other people [cgrep
, and gkk14Script
and gkkScript
...]). These are all very elementary scripts (I am a very bad MacPerl scripter...!).
MacPerl.utilities
table. They can be useful to convert Frontier list format to Perl list (array) format, and are used by some of my scripts.
print ()
" command that I use often in test scripts can be found in the MacPerl.utilities
table. If you use Frontier 4.2.3 or earlier, you can put it in your [user.initials
] table; if you use Frontier 5 or later, you can put it at the top level of suites
table. Thus, it will work without specifiying any table path.
MacPerl.utilities
table is a menu item that I have in my FinderMenu menubar. If you use MacJPerl, it can be useful. But the paths must be replaced with your paths to MacPerl and MacJPerl.
Mail to Nobumi Iyanaga
This page was last built with Frontier on a Macintosh on Tue, Sep 15, 1998 at 13:08:11. Thanks for checking it out! Nobumi Iyanaga