home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Applications / Alpha.5.96 folder / Help / MacPerl Help < prev    next >
Encoding:
Text File  |  1994-09-19  |  17.9 KB  |  406 lines  |  [TEXT/ALFA]

  1. -------------------------
  2. MacPerl Support in Alpha
  3. -------------------------
  4.  
  5. This document describes the MacPerl menu in Alpha.  The MacPerl menu was 
  6. written to allow Alpha to act as a front end for the Matthias 
  7. Neeracher's standalone MacPerl application and to provide a way to use 
  8. Perl scripts to process text buffers in Alpha.
  9.  
  10. If you aren't familiar with the Perl language, a descriptive 
  11. introduction is provided in the help file "Perl Commands"; that file 
  12. also contains a reasonably complete language reference.
  13.  
  14. Instructions for obtaining the MacPerl application are given further 
  15. below.
  16.  
  17.  
  18. Installation
  19. ------------
  20.  
  21. To use Perl mode and the MacPerl menu, they must first be installed by 
  22. selecting "MacPerl" from "Utils:Install" menu.  Once installed, Perl 
  23. mode can used to edit Perl script files, and the MacPerl menu will 
  24. become active whenever you enter Perl mode.
  25.  
  26. During installation, you'll be prompted to locate your copy of the 
  27. MacPerl application.  Also, you'll be asked whether you want to install 
  28. a sample collection of Perl text-filter scripts in the MacPerl 
  29. application folder.  Unless you already have a "Text Filters" folder 
  30. there, you should probably choose to install the sample files, since 
  31. they provide a good demonstration of how the text filter mechanism can 
  32. be used.   Finally, you'll be asked whether to make the MacPerl menu 
  33. active in Text mode.  You should say "yes" if you intend to use the text 
  34. filter mechanism, since text filter scripts are typically most useful
  35. when you're editting ordinary text files. 
  36.  
  37.  
  38. Perl Mode
  39. ---------
  40.  
  41. Perl mode is entered when you open a file with a ".pl" suffix or when
  42. it is explicitly chosen from the modes menu on the sidebar.  The prefix
  43. character (used by the "Convert:Strings" menu commands) is then set to the 
  44. Perl comment character ("#") and keyword colorization is turned on for
  45. the Perl operators and predefined names.  Also, choosing "Mark File"
  46. from the marks menu on the sidebar will create index markers for any Perl 
  47. subroutines found in the current file.
  48.  
  49.  
  50. MacPerl Menu
  51. ------------
  52.  
  53. Alpha's MacPerl menu allows the following actions:
  54.  
  55. MacPerl actions
  56.  
  57.  •  You can switch over to the MacPerl application, launching it
  58.     if necessary.
  59.  
  60.  •  The current window in Alpha can be opened under MacPerl.
  61.      The file may first be saved, if desired.
  62.  
  63.  •  The contents of the MacPerl output window can be retrieved 
  64.     and written into a new window under Alpha.
  65.  
  66.  •  You can quit the MacPerl application remotely from Alpha. 
  67.  
  68. Executing Perl Scripts from Alpha
  69.  
  70.  •  Selected text (or the entire buffer) may be executed as a Perl
  71.     script by the MacPerl application.
  72.  
  73.  •  A selected Perl script file (on disk) may be executed.
  74.  
  75.     Scripts run in the aforementioned ways will run as though they 
  76.     were executed from within the MacPerl application, itself.  That 
  77.     is, standard input is taken from an interactive window created 
  78.     automatically under MacPerl, and standard output is written to 
  79.     the MacPerl output window.
  80.     
  81.  •  Flags controlling Alpha's behavior when executing ordinary scripts 
  82.      may be set:
  83.      
  84.      Retrieve Output        When checked, any output written to the MacPerl
  85.                          output window is captured and written into a
  86.                          new window under Alpha.
  87.                          
  88.      Auto Switch            When checked, MacPerl is made the frontmost 
  89.                          application when the script is executed.
  90.                          Otherwise, Alpha remains frontmost while 
  91.                          waiting for the script to complete.
  92.  
  93.      Prompt For Args        When checked, Alpha will prompt you for 
  94.                          command-line arguments to be passed to
  95.                          the script.
  96.  
  97. Applying Text Filter Scripts
  98.  
  99.  •  Perl scripts that read from standard input and write to standard 
  100.     output may be used as text filters within Alpha.
  101.     
  102.     The script can be chosen from a set of "pre-attached" scripts in the 
  103.     "Text Filters" folder, from a file, or from another text buffer in 
  104.     Alpha.  This is all explained in more detail below.
  105.     
  106.  •  The last text filter used (from any source) may be run again.
  107.  
  108.     It's the source that's reused, actually.  If you change the script
  109.     in between uses, the new script will always be used. 
  110.  
  111.  •  Flags controlling the behavior of the text filter mechanism may be
  112.      set.  There are only two at present:
  113.      
  114.      Apply To Buffer        When checked, the entire current text window is
  115.                          used as "standard" input for the text filter script;
  116.                          otherwise, only the selected text is used.
  117.                          
  118.      Overwrite Selection    When checked, the output from the text filter 
  119.                          script will replace the input text in the original
  120.                          window.  Otherwise, the output is written into
  121.                          a new window.
  122.  
  123.  •  The temporary i/o files used by the text filter mechanism (STDIN, 
  124.     STDOUT, and SCRIPT) may be opened for debugging purposes. 
  125.  
  126. Other
  127.  
  128.  •  The MacPerl menu may be rebuilt.  This is useful when files are 
  129.     added to or removed from the "Text Filters" folder after Alpha has
  130.     started up.
  131.  
  132.  
  133. Running Scripts
  134. ---------------
  135.  
  136. Running scripts using the MacPerl menu is pretty straightforward.  You 
  137. can send the current selection, the entire current buffer, or a disk 
  138. file to MacPerl for execution as a Perl script; the result will be the 
  139. same as if you ran the script from MacPerl itself.  Unless you 
  140. explicitly arrange otherwise, this means that standard input is taken 
  141. from the keyboard (under the MacPerl application) and standard output 
  142. goes to MacPerl's output window.  
  143.  
  144. If the menu flag "Prompt For Args" is checked, then the user is prompted 
  145. for command-line arguments at the time the script is run.  These will be 
  146. available inside the script through the @ARGV array, as usual.  They are 
  147. also saved in the Perl-mode variable "perlCmdlineArgs", and become the 
  148. default arguments the next time the script is executed.
  149.  
  150. By default, Alpha will switch over to the MacPerl application while the 
  151. script is running, and then switch back to Alpha when it's done.  This 
  152. allows you interact with the script, if required, and see terminal 
  153. output as it appears.  This is necessary if you want to run your script 
  154. under the Perl debugger, for instance.  You may also prefer not to 
  155. switch over to the MacPerl application while the script is running; this 
  156. results in much less distracting window-swapping, etc., and is 
  157. probably a bit faster.  If you uncheck the flag "Auto Switch", then 
  158. Alpha will _not_ switch to MacPerl while the script runs.  Alpha, in 
  159. either case, will wait until the script has completed before 
  160. doing anything else.
  161.  
  162. When the script has completed, any output to MacPerl's standard output 
  163. window is captured by Alpha and written into a new window.  You can 
  164. suppress this by unchecking the "Retrieve Output" flag on the MacPerl menu.  
  165. If for some reason the output _isn't_ retrieved when you wanted it, you can 
  166. always grab it directly using the "Get Output Window" menu command.
  167.  
  168. If the script failed for some reason, Alpha will read the error messages 
  169. from the MacPerl output window and copy them back into a new window for 
  170. you to examine.  The script that generated the error is brought up and 
  171. the first line specifically referenced in an error message is 
  172. highlighted.  Note that error-trapping remains active whether "Retrieve 
  173. Output" is set or not.
  174.  
  175. Sometimes you may find that a script hangs.  For instance, if MacPerl is 
  176. configured to check for "#!" lines in scripts and yours doesn't have 
  177. one, it will put up a dialog asking whether or not to procede.  If you 
  178. didn't switch over to MacPerl when the script was run, you have know way 
  179. of knowing this, and so you and Alpha may end up just sitting there 
  180. waiting.
  181.  
  182. While annoying, a hung script is usually not fatal.  You can always tell 
  183. Alpha to stop waiting for a script to finish by hitting "Cmd-.".  You 
  184. may still have to switch over to MacPerl to terminate the script there, 
  185. as well.
  186.  
  187.  
  188. Text Filters
  189. ------------
  190.  
  191. Perl is, among other things, a powerful tool for rewriting and 
  192. extracting data from text files.  On a Unix system, one would typically 
  193. write text-processing scripts to read from "standard input" and write to 
  194. "standard output", taking advantage of command-line i/o redirection to 
  195. specify the actual input and output files used at any given time.  On 
  196. the Mac, the general absence of a command-line interface makes it harder 
  197. to use this elegant method.
  198.  
  199. The MacPerl menu in Alpha makes it possible to use scripts that read 
  200. from standard input and write to standard output to process text buffers 
  201. in Alpha directly.  Any text window can be used as standard input, and 
  202. standard output can either be directed back to that same window or to a 
  203. newly created one.  The script used may either be a disk file or an 
  204. Alpha text window; it is selected interactively using the "Text Filters" 
  205. and "Other Text Filters" submenus of the MacPerl menu.
  206.  
  207. Note that Alpha will remember the last filter script you use (from any 
  208. source) and let you run it again using the menu command 
  209. "repeatLastFilter".  You can see the name of this script by examining 
  210. the variable "perlLastFilter" under the flags menu in Perl mode.
  211.  
  212. The input for a filter script is always taken from the text 
  213. window that's current when you call the script.  Whether the entire 
  214. buffer or just the selected text is used is controlled by the menu flag 
  215. "ApplyToBuffer".  If this flag is checked, then the entire text window 
  216. is used as standard input, otherwise only the selected (highlighted) 
  217. text is used.
  218.  
  219. The output from the filter script either replaces the input text or else 
  220. is written into a new text window.  The choice is controlled by the 
  221. other menu flag, "OverwriteSelection".  If the flag is checked, then the 
  222. input text is deleted from its buffer and the Perl output is inserted in 
  223. its place; otherwise a new window is created to hold the output text.  
  224. As always, Alpha's unlimited undo capability let's you recover if you 
  225. accidently overwrite the input text when you didn't want to.
  226.  
  227. If the script triggers an error, then the filter operation is aborted.  
  228. Any error messages are retrieved from MacPerl's output window and 
  229. displayed in Alpha, as described above.
  230.  
  231. The ability to take the script itself from a text window allows simple 
  232. one-time scripts to be created and applied on the fly.  This can be very 
  233. useful because, even with the overhead to start up MacPerl, large-scale 
  234. global search-and-replace operations (hundreds of replaces) can be 
  235. substantially faster in MacPerl than in Alpha.  Also, you might find 
  236. it easier to apply a series of regular expression substitutions using a 
  237. single, short Perl script, rather than a number of separate "Find" and 
  238. "Replace All" commands in Alpha.
  239.  
  240.  
  241. Preattached Scripts
  242. -------------------
  243.  
  244. Frequently used text filter scripts can be conveniently accessed by 
  245. placing them in a folder called "Text Filters" within the MacPerl 
  246. application folder.  When the MacPerl menu is first created, it looks in 
  247. this folder and builds a hierarchical submenu from the names of the 
  248. scripts that it finds.  Note that it will also create a "Text Filters" 
  249. folder if one doesn't already exist.
  250.  
  251. As mentioned above, a sampling of useful :-) Perl scripts is distributed 
  252. with Alpha in the folder ":Tcl:UserCode:Text Filters".  When the MacPerl 
  253. menu is installed, you have the option of copying this folder to your 
  254. MacPerl folder to get yourself started; if you already have a "Text 
  255. Filters" folder there, you might not want to do this, since files with 
  256. the same names as the sample files would be overwritten.
  257.  
  258. Of the scripts in the sample collection, the "Text Munging" scripts 
  259. ("shuffle", "sort lines", "travesty", and "wordcount") were taken from 
  260. the Camel book ("Programming Perl").  The "s2p" script is my adaptation 
  261. of the standard code that converts Unix "sed" scripts to Perl (it was 
  262. modified to work without using the C preprocessor.) "Strip Mail Headers" 
  263. takes e-mail files and edits out any header lines but the few that I 
  264. typically care about ("From:, "Date:", etc..).  "CC To BibTeX" is a 
  265. script I use that takes listings from the online "Current Contents" 
  266. database and rewrites them as BibTeX database entries.  To try it out, 
  267. select (highlight) the sample Current Contents citation below and choose 
  268. "CC To BibTeX" from the "Text Filters" submenu (make sure that "Apply 
  269. To Buffer" is _not_ checked before you do!)
  270.  
  271. 288. VOS MH; LAMBRY JC; ROBLES SJ; YOUVAN DC; and others.
  272.        FEMTOSECOND SPECTRAL EVOLUTION OF THE EXCITED STATE OF BACTERIAL
  273.      REACTION CENTERS AT 10-K.
  274.        PROCEEDINGS OF THE NATIONAL ACADEMY OF SCIENCES OF THE UNITED STATES OF
  275.      AMERICA, 1992 JAN 15, V89 N2:613-617.
  276.  
  277.  
  278. Technical Details
  279. -----------------
  280.  
  281. In case something doesn't work the way you expect it to, it may be 
  282. useful to know exactly what's going on behind the scenes when you 
  283. run a text filter from the MacPerl menu.
  284.  
  285. The "magic" that allows a script written as a Unix command-line filter 
  286. to be used in Alpha is achieved by rewriting that script on the fly 
  287. before passing it on to MacPerl.  Basically, the script is modified to 
  288. take standard input from a file called STDIN, created in the MacPerl 
  289. folder, and send standard output to another file called STDOUT.  This 
  290. just involves adding a handful of lines at the beginning of the script, 
  291. to open and select those files, and a few more lines at the end to close 
  292. them.  If there's a #! line in the script, then the new commands are 
  293. added after that line; if there was no #! line in the first place, one 
  294. is added anyway in case your MacPerl preferences are set up to require 
  295. it.
  296.  
  297. So, when you call a text filter, the input text is first copied to the 
  298. STDIN file.  Then, the script you selected is read in by Alpha, 
  299. modified, and written to another file, SCRIPT, also in the MacPerl 
  300. folder.  Next, MacPerl is sent a "DoScript" AppleEvent directing it to 
  301. run the script in the SCRIPT file.  When it's finished, the output text 
  302. in the STDOUT file is read back in by Alpha and written out 
  303. appropriately (in place of the input text or into a new window.)
  304.  
  305. You should never have to worry about the STDIN, STDOUT, and SCRIPT 
  306. files.  However, if something funny happens, you can see exactly what 
  307. input text was used and what the modified script looked like by 
  308. examining the STDIN and SCRIPT files.  These, together with the STDOUT 
  309. file, can be opened using the "Open Special" submenu of the MacPerl 
  310. menu.  Also, note that if you get an error message from MacPerl, the 
  311. line numbers refered to are those of the SCRIPT file, and not of the 
  312. original script.
  313.  
  314.  
  315. Bugs, etc.
  316. ----------
  317.  
  318. Comments and suggestions regarding this package are always welcome.  If 
  319. there's something that bothers you, or some additional capability that 
  320. you'd like to see, let me know and I'll see what I can do.
  321.  
  322. Bug reports and any other comments should be directed to
  323.  
  324. Tom Pollard
  325.  
  326. e-mail:            pollard@chem.columbia.edu
  327.  
  328. snail-mail:        544 W 114th St #2R
  329.                 New York, NY 10025
  330.                 USA
  331.  
  332.  
  333. Version History
  334. ---------------
  335.  
  336. This package was written by Tom Pollard <pollard@chem.columbia.edu> 
  337. and Martijn Koster <m.koster@nexor.co.uk>
  338.  
  339. 1.5  9/94  -  MacPerl menu rearranged somewhat.
  340.               Explicit "Get Output Window" command added to menu.
  341.               Reading "#!" line for args is incompatible w/ standard,
  342.                   so it's been dropped.
  343.               Only scan the first 40 output lines for error messages (faster)
  344.               "wrapFilterScript" no longer opens STDIN
  345.               Text filters may now use command-line args
  346.               STDIN for text filters passed as explicit cmd-line arg 
  347. 1.4  9/94  -  The "#!" line of every script is read for command-line args.
  348.               "PromptForArgs" menu flag added.
  349.               "perlCmdlineArgs" modeVar holds default command-line args.
  350.               Scripts are sent using custom "perlDoScript2" proc.
  351. 1.3  9/94  -  When any script generates a compilation error, the file 
  352.                   containing the script is brought up with the offending 
  353.                   line highlighted; all error output is also written to
  354.                   a "Perl Error Messages" window.
  355.               'repeatLastFilter' runs again the last text-filter script used.
  356.               'perlLastFilter' modeVar holds pathname of last filter.
  357.               Menu flags now mirrored as modeVars, so they can be saved and
  358.                   restored between sessions.
  359.               Minor bug fixes.
  360. 1.2  8/94  -  'retrieveOutput' and 'autoSwitch' flags added.
  361.               'openInMacperl' added.
  362.               MacPerl output window now closed before new scripts are sent.
  363.               Filters now abort if there are compilation errors, and
  364.                    MacPerl diagnostic output retrieved and displayed.
  365. 1.1  8/94  -  'quitMacperl' added.
  366.               perl-mode file-marking updated for Alpha 5.90
  367.               Simplified installation via 'loadMacperl'(Pete Keleher).
  368. 1.0  7/94  -  perl-mode setup updated for Alpha 5.85:
  369.                    keyword colorization supported
  370.                    custom file-marking added
  371.               #! lines in filter scripts now handled correctly
  372.               Workarounds installed for AppleEvent problem in MacPerl 4.1.3
  373. 0.9  3/94  -  perl-mode stuff added, and
  374.               highlighted 'Perl commands' file (man page) prepared
  375.               minor bug fixes, too
  376. 0.8  3/94  -  flags are now check-marked
  377. 0.7  3/94  -  nested Text Filters folder now supported
  378.               menu format modified somewhat
  379. 0.6  3/94  -  'applyToBuffer' flag added
  380.               scripts in Alpha buffers can now be used as filters
  381. 0.5  2/94  -  'filters', 'open special' submenu added
  382.               'overwrite' flag added
  383. 0.2  1/94  -  menu support added (MK)
  384.               'execute selection', 'execute buffer' commands added
  385. 0.1  9/93  -  text filter functionality created (WTP)
  386.  
  387.  
  388. MacPerl application
  389. -------------------
  390.  
  391. If you don't already have MacPerl, it's available by anonymous ftp from
  392. the umich archive site,
  393.  
  394.   mac.archive.umich.edu    [141.211.165.34]    mac/development/languages
  395.  
  396. and its mirrors.  Also, MacPerl's home site is
  397.  
  398.   ftp.switch.ch            [130.59.1.40]       software/mac/src/mpw_c
  399.  
  400. MacPerl was written (ported to the Mac) by
  401.  
  402.        Matthias Neeracher <neeri@iis.ee.ethz.ch> , and
  403.        Tim Endres <time@ice.com>.
  404.        
  405. ---------------------------------------------------------------------------
  406.