summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/perltex/perltex.dtx
blob: 670009bbe87cdd67d592117b6297ddf2eba3ca48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
% \iffalse meta-comment
%
% Copyright (C) 2006, Scott Pakin <scott+pt@pakin.org>
% ----------------------------------------------------
%
% This file may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, either version 1.3c of this license
% or (at your option) any later version.  The latest version of this
% license is in:
%
%    http://www.latex-project.org/lppl.txt
%
% and version 1.3c or later is part of all distributions of LaTeX
% version 2006/05/20 or later.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{perltex.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{perltex}
%<*package>
    [2006/06/24 v1.3 LaTeX macros for use with PerlTeX]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\makeatletter
  \def\plmac@tag{FakingBeingRunFromPerlTeX}
  \def\plmac@tofile{\jobname.topl}
  \def\plmac@fromfile{\jobname.frpl}
  \def\plmac@toflag{\jobname.tfpl}
  \def\plmac@fromflag{\jobname.ffpl}
  \def\plmac@doneflag{\jobname.dfpl}
\makeatother
\usepackage{perltex}
\usepackage{varioref}
\usepackage{flafter}
\usepackage{textcomp}
\IfFileExists{hyperref.sty}{%
  \usepackage{hyperref}
  \hypersetup{%
    hyperindex=false,
    bookmarksopen,
    pdftitle={PerlTeX: Defining LaTeX macros in terms of Perl code},
    pdfauthor={Scott Pakin, scott+pt@pakin.org},
    pdfsubject={Using Perl to define LaTeX macros},
    pdfkeywords={programming, LaTeX, macros, Perl}
  }
}{}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\setcounter{IndexColumns}{2}
\begin{document}
  \DocInput{perltex.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{511}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v1.0}{2003/07/13}{Initial version}
% \changes{v1.2}{2004/10/07}{Renamed \texttt{perlmacros.sty} to
%   \texttt{perltex.sty} for consistency.}
%
% \GetFileInfo{perltex.sty}
%
% \DoNotIndex{\$, \', \(, \), \., \\, \{, \}, \^, \", \@}
% \DoNotIndex{\@empty, \@ifnextchar, \@ifundefined, \@makeother, \@ne}
% \DoNotIndex{\@permittedops}
% \DoNotIndex{\active, \advance}
% \DoNotIndex{\b, \begin, \begingroup, \btrapped}
% \DoNotIndex{\catcode, \csname}
% \DoNotIndex{\d, \def}
% \DoNotIndex{\edef, \else, \end, \endcsname, \endgroup, \expandafter}
% \DoNotIndex{\fi}
% \DoNotIndex{\gdef, \global}
% \DoNotIndex{\ifeof, \ifnum, \ifx, \immediate}
% \DoNotIndex{\let, \loop}
% \DoNotIndex{\makeatletter, \makeatother, \MessageBreak}
% \DoNotIndex{\n, \newcount, \newif, \newread, \newtoks, \newwrite}
% \DoNotIndex{\noexpand, \number}
% \DoNotIndex{\protect}
% \DoNotIndex{\r, \relax, \repeat}
% \DoNotIndex{\s, \space, \string}
% \DoNotIndex{\TeX, \the}
% \DoNotIndex{\W}
%
% \newcommand{\PerlTeX}{Perl\TeX{}}           ^^A  Name of the complete system
% \newcommand{\perltex}{\texttt{perltex.pl}}  ^^A  Name of the Perl script
% \newcommand{\perlmac}{\texttt{perltex.sty}} ^^A  Name of the LaTeX package
%
% ^^A  Define an environment just like macro but for Perl scalars or lists.
% \makeatletter
% \newenvironment{perlmacro}[1]{^^A
%   \begingroup
%     \catcode`\_=11
%     \def\PrintMacroName##1{\strut\MacroFont\string##1\ }^^A
%     \def\SpecialIndex@##1##2{^^A
%       \@bsphack
%       \special@index{\expandafter\@gobble\string##1\actualchar
%         \string\verb\quotechar*\verbatimchar\string##1\verbatimchar##2}^^A
%       \@esphack
%     }^^A
%     \begin{macro}{#1}^^A
% }{^^A
%     \end{macro}^^A
%   \endgroup
% }
% \makeatother
%
% \title{\PerlTeX: \\ Defining \LaTeX{} macros in terms of Perl code^^A
%   \thanks{This document corresponds to \PerlTeX~\fileversion, dated
%   \filedate.}}
% \author{Scott Pakin \\ \texttt{scott+pt@pakin.org}}
%
% \maketitle
% \sloppy
%
% \begin{abstract}
%   \PerlTeX{} is a combination Perl script (\perltex) and \LaTeXe{}
%   style file (\perlmac) that, together, give the user the ability to
%   define \LaTeX{} macros in terms of Perl code.  Once defined, a Perl
%   macro becomes indistinguishable from any other \LaTeX{} macro.
%   \PerlTeX{} thereby combines \LaTeX's typesetting power with Perl's
%   programmability.
% \end{abstract}
%
% \section{Introduction}
%
% \TeX{} is a professional-quality typesetting system.  However, its
% programming language is rather hard to use for anything but the most
% simple forms of text substitution.  Even \LaTeX, the most popular
% macro package for \TeX, does little to simplify \TeX{} programming.
%
% Perl is a general-purpose programming language whose forte is in text
% manipulation.  However, it has no support whatsoever for typesetting.
%
% \PerlTeX's goal is to bridge these two worlds.  It enables the
% construction of documents that are primarily \LaTeX-based but contain
% a modicum of Perl.  \PerlTeX{} seamlessly integrates Perl code into a
% \LaTeX{} document, enabling the user to define macros whose bodies
% consist of Perl code instead of \TeX{} and \LaTeX{} code.
%
% As an example, suppose you need to define a macro that reverses a set
% of words.  Although it sounds like it should be simple, few \LaTeX{}
% authors are sufficiently versed in the \TeX{} language to be able to
% express such a macro.  However, a word-reversal function is easy to
% express in Perl: one need only |split| a string into a list of words,
% |reverse| the list, and |join| it back together.  The following is how
% a |\reversewords| macro could be defined using \PerlTeX:
%
% \begin{verbatim}
%    \perlnewcommand{\reversewords}[1]{join " ", reverse split " ", $_[0]}
% \end{verbatim}
%
% \noindent
% Then, executing ``\texttt{\string\reversewords\string{Try doing this
% without Perl!\string}}'' in a document would produce the text ``Perl!
% without this doing Try''.  Simple, isn't it?
%
% As another example, think about how you'd write a macro in \LaTeX{} to
% extract a substring of a given string when provided with a starting
% position and a length.  Perl has an built-in |substr| function and
% \PerlTeX{} makes it easy to export this to \LaTeX:
%
% \begin{verbatim}
%    \perlnewcommand{\substr}[3]{substr $_[0], $_[1], $_[2]}
% \end{verbatim}
%
% |\substr| can then be used just like any other \LaTeX{} macro---and as
% simply as Perl's |substr| function:
%
% \bigskip
%
% \noindent|   |^^A
% \begin{tabular}{@{}l@{}}
%    |\newcommand{\str}{superlative}| \\
%    |A sample substring of ``\str'' is ``\substr{\str}{2}{4}''.| \\[1ex]
%    \multicolumn{1}{@{}c@{}}{\Huge$\Downarrow$} \\[2ex]
%    \multicolumn{1}{@{}c@{}}{A sample substring of ``superlative'' is ``perl''.}
% \end{tabular}
%
% \bigskip
%
% To present a somewhat more complex example, observe how much
% easier it is to generate a repetitive matrix using Perl code than
% ordinary \LaTeX{} commands:
%
% \begin{verbatim}
%    \perlnewcommand{\hilbertmatrix}[1]{
%      my $result = '
%    \[
%    \renewcommand{\arraystretch}{1.3}
%    ';
%      $result .= '\begin{array}{' . 'c' x $_[0] . "}\n";
%      foreach $j (0 .. $_[0]-1) {
%        my @row;
%        foreach $i (0 .. $_[0]-1) {
%          push @row, ($i+$j) ? (sprintf '\frac{1}{%d}', $i+$j+1) : '1';
%        }
%        $result .= join (' & ', @row) . " \\\\\n";
%      }
%      $result .= '\end{array}
%    \]';
%      return $result;
%    }
%
%    \hilbertmatrix{20}
% \end{verbatim}
%
% \begin{center}
% {\Huge$\Downarrow$}
% \end{center}
%
% \[
% \renewcommand{\arraystretch}{1.3}
% \begin{array}{ccccccccccccccc}
% 1 & \frac{1}{2} & \frac{1}{3} & \frac{1}{4} & \frac{1}{5} & \frac{1}{6} & \frac{1}{7} & \frac{1}{8} & \frac{1}{9} & \frac{1}{10} & \frac{1}{11} & \frac{1}{12} & \frac{1}{13} & \frac{1}{14} & \frac{1}{15} \\
% \frac{1}{2} & \frac{1}{3} & \frac{1}{4} & \frac{1}{5} & \frac{1}{6} & \frac{1}{7} & \frac{1}{8} & \frac{1}{9} & \frac{1}{10} & \frac{1}{11} & \frac{1}{12} & \frac{1}{13} & \frac{1}{14} & \frac{1}{15} & \frac{1}{16} \\
% \frac{1}{3} & \frac{1}{4} & \frac{1}{5} & \frac{1}{6} & \frac{1}{7} & \frac{1}{8} & \frac{1}{9} & \frac{1}{10} & \frac{1}{11} & \frac{1}{12} & \frac{1}{13} & \frac{1}{16} & \frac{1}{17} & \frac{1}{18} & \frac{1}{19} \\
% \frac{1}{6} & \frac{1}{7} & \frac{1}{8} & \frac{1}{9} & \frac{1}{10} & \frac{1}{11} & \frac{1}{12} & \frac{1}{13} & \frac{1}{14} & \frac{1}{15} & \frac{1}{16} & \frac{1}{17} & \frac{1}{18} & \frac{1}{19} & \frac{1}{20} \\
% \frac{1}{7} & \frac{1}{8} & \frac{1}{9} & \frac{1}{10} & \frac{1}{11} & \frac{1}{12} & \frac{1}{13} & \frac{1}{14} & \frac{1}{15} & \frac{1}{16} & \frac{1}{17} & \frac{1}{18} & \frac{1}{19} & \frac{1}{20} & \frac{1}{21} \\
% \frac{1}{8} & \frac{1}{9} & \frac{1}{10} & \frac{1}{11} & \frac{1}{12} & \frac{1}{13} & \frac{1}{14} & \frac{1}{15} & \frac{1}{16} & \frac{1}{17} & \frac{1}{18} & \frac{1}{19} & \frac{1}{20} & \frac{1}{21} & \frac{1}{22} \\
% \frac{1}{9} & \frac{1}{10} & \frac{1}{11} & \frac{1}{12} & \frac{1}{13} & \frac{1}{14} & \frac{1}{15} & \frac{1}{16} & \frac{1}{17} & \frac{1}{18} & \frac{1}{19} & \frac{1}{20} & \frac{1}{21} & \frac{1}{22} & \frac{1}{23} \\
% \frac{1}{10} & \frac{1}{11} & \frac{1}{12} & \frac{1}{13} & \frac{1}{14} & \frac{1}{15} & \frac{1}{16} & \frac{1}{17} & \frac{1}{18} & \frac{1}{19} & \frac{1}{20} & \frac{1}{21} & \frac{1}{22} & \frac{1}{23} & \frac{1}{24} \\
% \frac{1}{11} & \frac{1}{12} & \frac{1}{13} & \frac{1}{14} & \frac{1}{15} & \frac{1}{16} & \frac{1}{17} & \frac{1}{18} & \frac{1}{19} & \frac{1}{20} & \frac{1}{21} & \frac{1}{22} & \frac{1}{23} & \frac{1}{24} & \frac{1}{25} \\
% \frac{1}{12} & \frac{1}{13} & \frac{1}{14} & \frac{1}{15} & \frac{1}{16} & \frac{1}{17} & \frac{1}{18} & \frac{1}{19} & \frac{1}{20} & \frac{1}{21} & \frac{1}{22} & \frac{1}{23} & \frac{1}{24} & \frac{1}{25} & \frac{1}{26} \\
% \frac{1}{13} & \frac{1}{14} & \frac{1}{15} & \frac{1}{16} & \frac{1}{17} & \frac{1}{18} & \frac{1}{19} & \frac{1}{20} & \frac{1}{21} & \frac{1}{22} & \frac{1}{23} & \frac{1}{24} & \frac{1}{25} & \frac{1}{26} & \frac{1}{27} \\
% \frac{1}{14} & \frac{1}{15} & \frac{1}{16} & \frac{1}{17} & \frac{1}{18} & \frac{1}{19} & \frac{1}{20} & \frac{1}{21} & \frac{1}{22} & \frac{1}{23} & \frac{1}{24} & \frac{1}{25} & \frac{1}{26} & \frac{1}{27} & \frac{1}{28} \\
% \frac{1}{15} & \frac{1}{16} & \frac{1}{17} & \frac{1}{18} & \frac{1}{19} & \frac{1}{20} & \frac{1}{21} & \frac{1}{22} & \frac{1}{23} & \frac{1}{24} & \frac{1}{25} & \frac{1}{26} & \frac{1}{27} & \frac{1}{28} & \frac{1}{29} \\
% \end{array}
% \]
%
% \bigskip
%
% In addition to |\perlnewcommand| and |\perlrenewcommand|, \PerlTeX{}
% supports |\perlnewenvironment| and |\perlrenewenvironment| macros.
% These enable environments to be defined using Perl code.  The
% following example, a |spreadsheet| environment, generates a |tabular|
% environment plus a predefined header row.  This example would have
% been much more difficult to implement without \PerlTeX:
%
% \begin{verbatim}
% \newcounter{ssrow}
% \perlnewenvironment{spreadsheet}[1]{
%   my $cols = $_[0];
%   my $header = "A";
%   my $tabular = "\\setcounter{ssrow}{1}\n";
%   $tabular .= '\newcommand*{\rownum}{\thessrow\addtocounter{ssrow}{1}}' . "\n";
%   $tabular .= '\begin{tabular}{@{}r|*{' . $cols . '}{r}@{}}' . "\n";
%   $tabular .= '\\multicolumn{1}{@{}c}{} &' . "\n";
%   foreach (1 .. $cols) {
%     $tabular .= "\\multicolumn{1}{c";
%     $tabular .= '@{}' if $_ == $cols;
%     $tabular .= "}{" . $header++ . "}";
%     if ($_ == $cols) {
%       $tabular .= " \\\\ \\cline{2-" . ($cols+1) . "}"
%     }
%     else {
%       $tabular .= " &";
%     }
%     $tabular .= "\n";
%   }
%   return $tabular;
% }{
%   return "\\end{tabular}\n";
% }
%
% \begin{center}
%   \begin{spreadsheet}{4}
%     \rownum &  1 &  8 & 10 & 15 \\
%     \rownum & 12 & 13 &  3 &  6 \\
%     \rownum &  7 &  2 & 16 &  9 \\
%     \rownum & 14 & 11 &  5 &  4
%   \end{spreadsheet}
% \end{center}
% \end{verbatim}
%
% \begin{center}
% {\Huge$\Downarrow$}
% \end{center}
%
% \DeleteShortVerb{\|}
% \begin{center}
%   \begin{tabular}{@{}r|*{4}{r}@{}}
%     \multicolumn{1}{@{}c}{} &
%     \multicolumn{1}{c}{A} &
%     \multicolumn{1}{c}{B} &
%     \multicolumn{1}{c}{C} &
%     \multicolumn{1}{c@{}}{D} \\ \cline{2-5}
%     1 &  1 &  8 & 10 & 15 \\
%     2 & 12 & 13 &  3 &  6 \\
%     3 &  7 &  2 & 16 &  9 \\
%     4 & 14 & 11 &  5 &  4
%   \end{tabular}
% \end{center}
% \MakeShortVerb{\|}
%
%
% \section{Usage}
%
% There are two components to using \PerlTeX\@.  First, documents must
% include a ``|\usepackage{perltex}|'' line in their preamble in
% order to define |\perlnewcommand|, |\perlrenewcommand|,
% |\perlnewenvironment|, and |\perlrenewenvironment|.  Second, \LaTeX{}
% documents must be compiled using the \perltex{} wrapper script.
%
% \subsection{Defining and redefining Perl macros}
%
% \DescribeMacro{\perlnewcommand}
% \DescribeMacro{\perlrenewcommand}
% \DescribeMacro{\perlnewenvironment}
% \DescribeMacro{\perlrenewenvironment}
% \DescribeMacro{\perldo}
% \perlmac{} defines five macros: |\perlnewcommand|,
% |\perlrenewcommand|, |\perlnewenvironment|, |\perlrenewenvironment|,
% and |\perldo|.  The first four of these behave exactly like their
% \LaTeXe{} counterparts---|\newcommand|, |\renewcommand|,
% |\newenvironment|, and |\renewenvironment|---except that the macro
% body consists of Perl code that dynamically generates \LaTeX{} code.
% \perlmac{} even includes support for optional arguments and the
% starred forms of its commands (i.e.~|\perlnewcommand*|,
% |\perlrenewcommand*|, |\perlnewenvironment*|, and
% |\perlrenewenvironment*|).  |\perldo| immediately executes a block of
% Perl code without (re)defining any macros or environments.
%
% A \PerlTeX-defined macro or environments is converted to a Perl
% subroutine named after the macro/environment but beginning with
% ``|latex_|''.  For example, a \PerlTeX-defined \LaTeX{} macro called
% |\myMacro| internally produces a Perl subroutine called
% |latex_myMacro|.  Macro arguments are converted to subroutine
% arguments.  A \LaTeX{} macro's |#1| argument is referred to as |$_[0]|
% in Perl; |#2| is referred to as |$_[1]|; and so forth.
%
% Any valid Perl code can be used in the body of a macro.  However,
% \PerlTeX{} executes the Perl code within a secure sandbox.  This means
% that potentially harmful Perl operations, such as |unlink|, |rmdir|,
% and |system| will result in a run-time error.  (It is possible to
% disable the safety checks, however, as is explained in
% Section~\ref{sec:perltex-man}.)  Having a secure sandbox implies that
% it is safe to build \PerlTeX{} documents written by other people
% without worrying about what they may do to your computer system.
%
% A single sandbox is used for the entire |latex| run.  This means that
% multiple macros defined by |\perlnewcommand| can invoke each other.
% It also means that global variables persist across macro calls:
%
% \bigskip
%
% \noindent|   |^^A
% \begin{tabular}{@{}l@{}}
%    |\perlnewcommand{\setX}[1]{$x = $_[0]; return ""}| \\
%    |\perlnewcommand{\getX}{'$x$ was set to ' . $x . '.'}| \\
%    |\setX{123}| \\
%    |\getX| \\
%    |\setX{456}| \\
%    |\getX| \\
%    |\perldo{$x = 789}| \\
%    |\getX| \\[1ex]
%    \multicolumn{1}{@{}c@{}}{\Huge$\Downarrow$} \\[2ex]
%    \multicolumn{1}{@{}c@{}}{^^A
%      $x$ was set to 123.  $x$ was set to 456.  $x$ was set to 789.^^A
%    } \\
% \end{tabular}
%
% \bigskip
%
% Macro arguments are expanded by \LaTeX{} before being passed to Perl.
% Consider the following macro definition, which wraps its argument
% within |\begin{verbatim*}|\dots\linebreak[0]|\end{verbatim*}|:
%
% \begin{verbatim}
%     \perlnewcommand{\verbit}[1]{
%       "\\begin{verbatim*}\n$_[0]\n\\end{verbatim*}\n"
%     }
% \end{verbatim}
%
% \noindent
% An invocation of
% ``|\verbit{\TeX}|'' would therefore typeset the \emph{expansion} of
% ``|\TeX|'', namely ``|T\kern| |-.1667em\lower| |.5ex\hbox| |{E}\kern|
% |-.125emX\spacefactor| |\@m|'', which might be a bit unexpected.  The
% solution is to use |\noexpand|:
% |\verbit{\noexpand\TeX}|~$\Rightarrow$ |\TeX|\@.  ``Robust'' macros as
% well as |\begin| and |\end| are implicitly preceded by |\noexpand|.
%
%
% \subsection{Invoking \perltex}
% \label{sec:perltex-man}
%
% The following pages reproduce the \perltex{} program documentation.
% Key parts of the documentation are excerpted when \perltex{} is
% invoked with the |--help| option.  The various Perl
% |pod2|\meta{something} tools can be used to generate the complete
% program documentation in a variety of formats such as \LaTeX, HTML,
% plain text, or Unix man-page format.  For example, the following
% command is the recommended way to produce a Unix man page from
% \perltex:
%
% \enlargethispage{4ex}
% \begin{verbatim}
%   pod2man --center=" " --release=" " perltex.pl > perltex.1
% \end{verbatim}
%
% \clearpage
% \begingroup
% \def\index#1{}
% \def\section#1{}
% \let\subsection=\subsubsection
%
% ^^A  ***************************************************************
% ^^A  * The following was generated using pod2latex and touched up  *
% ^^A  * slightly to make it better match the rest of this document. *
% ^^A  ***************************************************************
%
% \def\C++{{\rm C\kern-.05em\raise.3ex\hbox{\footnotesize ++}}}
% \def\underscore{\leavevmode\kern.04em\vbox{\hrule width 0.4em height 0.3pt}}
% \setlength{\parindent}{0pt}
%
% \section{PERLTEX.PL}%
% \index{PERLTEX.PL}
%
% \subsection*{NAME}
% perltex --- enable \LaTeX{} macros to be defined in terms of Perl code
%
% \subsection*{SYNOPSIS}
% perltex
% {\tt [}{\bf {\tt --}help}{\tt ]}
% {\tt [}{\bf {\tt --}latex}={\em program\/}{\tt ]}
% {\tt [}{\bf {\tt --}}{\tt [}{\bf no}{\tt ]}{\bf safe}{\tt ]}
% {\tt [}{\bf {\tt --}permit}={\em feature\/}{\tt ]}
% {\tt [}{\em latex options\/}{\tt ]}
%
% \subsection*{DESCRIPTION}
% \LaTeX{} --- through the underlying \TeX{} typesetting system --- produces
% beautifully typeset documents but has a macro language that is
% difficult to program.  In particular, support for complex string
% manipulation is largely lacking.  Perl is a popular general-purpose
% programming language whose forte is string manipulation.  However, it
% has no typesetting capabilities whatsoever.
%
% \bigskip
%
% Clearly, Perl's programmability could complement \LaTeX{}'s typesetting
% strengths.  {\bf perltex} is the tool that enables a symbiosis between
% the two systems.  All a user needs to do is compile a \LaTeX{} document
% using {\bf perltex} instead of {\bf latex}.  ({\bf perltex} is actually a
% wrapper for {\bf latex}, so no {\bf latex} functionality is lost.)  If the
% document includes a {\tt \char`\\usepackage\{perltex\}} in its preamble, then
% {\tt \char`\\perlnewcommand} and {\tt \char`\\perlrenewcommand} macros will be made
% available.  These behave just like \LaTeX{}'s {\tt \char`\\newcommand} and
% {\tt \char`\\renewcommand} except that the macro body contains Perl code instead
% of \LaTeX{} code.
%
% \subsection*{OPTIONS}%
% \index{OPTIONS}
%
% {\bf perltex} accepts the following command-line options:
%
% \begin{description}
%
% \item[{\bf {\tt --}help}]%
% \index{--help@{\bf {\tt --}help}}%
% \hfil\\
% Display basic usage information.
%
% \item[{\bf {\tt --}latex}={\em program\/}]%
% \index{--latex=program@{\bf {\tt --}latex}={\em program\/}}%
% \hfil\\
% Specify a program to use instead of {\bf latex}.  For example,
% {\tt --latex=pdf{}latex} would typeset the given document using
% {\bf pdf{}latex} instead of ordinary {\bf latex}.
%
% \item[{\bf {\tt --}}{\tt [}{\bf no}{\tt ]}{\bf safe}]%
% \index{--[no]safe@{\bf {\tt --}}{\tt [}{\bf no}{\tt ]}{\bf safe}}%
% \hfil\\
% Enable or disable sandboxing.  With the default of {\tt --safe},
% {\bf perltex} executes the code from a {\tt \char`\\perlnewcommand} or
% {\tt \char`\\perlrenewcommand} macro within a protected environment that
% prohibits ``unsafe'' operations such as accessing files or executing
% external programs.  Specifying {\tt --nosafe} gives the \LaTeX{} document
% {\em carte blanche\/} to execute any arbitrary Perl code, including that
% which can harm the user's files.  See the {\em Safe\/} manpage for more information.
%
% \item[{\bf {\tt --}permit}={\em feature\/}]%
% \index{--permit=feature@{\bf {\tt --}permit}={\em feature\/}}%
% \hfil\\
% Permit particular Perl operations to be performed.  The {\tt --permit}
% option, which can be specified more than once on the command line,
% enables finer-grained control over the {\bf perltex} sandbox.  See
% the {\em Opcode\/} manpage for more information.
%
% \end{description}
%
% These options are then followed by whatever options are normally
% passed to {\bf latex} (or whatever program was specified with
% {\tt --latex}), including, for instance, the name of the {\em .tex\/} file to
% compile.
%
% \subsection*{EXAMPLES}
% In its simplest form, {\bf perltex} is run just like {\bf latex}:
% \begin{verbatim}
%     perltex myfile.tex
% \end{verbatim}
%
% To use {\bf pdf{}latex} instead of regular {\bf latex}, use the {\tt --latex}
% option:
% \begin{verbatim}
%     perltex --latex=pdflatex myfile.tex
% \end{verbatim}
%
% If \LaTeX{} gives a ``{\tt trapped by operation mask}'' error and you
% trust the {\em .tex\/} file you're trying to compile not to execute
% malicious Perl code (e.g., because you wrote it yourself), you can
% disable {\bf perltex}'s safety mechansisms with {\tt --nosafe}:
% \begin{verbatim}
%     perltex --nosafe myfile.tex
% \end{verbatim}
%
% The following command gives documents only {\bf perltex}'s default
% permissions ({\tt :browse}) plus the ability to open files and invoke the
% {\tt time} command:
% \begin{verbatim}
%     perltex --permit=:browse --permit=:filesys_open
%       --permit=time myfile.tex
% \end{verbatim}
%
% \subsection*{ENVIRONMENT}
% {\bf perltex} honors the following environment variables:
%
% \begin{description}
%
% \item[PERLTEX]%
% \index{PERLTEX@PERLTEX}%
% \hfil\\
% Specify the filename of the \LaTeX{} compiler.  The \LaTeX{} compiler
% defaults to ``{\tt latex}''.  The {\tt PERLTEX} environment variable
% overrides this default, and the {\tt --latex} command-line option (see
% the {\tt OPTIONS} entry elsewhere in this document) overrides that.
%
% \end{description}
%
% \subsection*{FILES}
% While compiling {\em jobname.tex\/}, {\bf perltex} makes use of the following
% files:
%
% \begin{description}
%
% \item[{\em jobname.lgpl\/}]%
% \index{jobname.lgpl@{\em jobname.lgpl\/}}%
% \hfil\\
% log file written by Perl; helpful for debugging Perl macros
%
% \item[{\em jobname.topl\/}]%
% \index{jobname.topl@{\em jobname.topl\/}}%
% \hfil\\
% information sent from \LaTeX{} to Perl
%
% \item[{\em jobname.frpl\/}]%
% \index{jobname.frpl@{\em jobname.frpl\/}}%
% \hfil\\
% information sent from Perl to \LaTeX{}
%
% \item[{\em jobname.tfpl\/}]%
% \index{jobname.tfpl@{\em jobname.tfpl\/}}%
% \hfil\\
% ``flag'' file whose existence indicates that {\em jobname.topl\/} contains
% valid data
%
% \item[{\em jobname.ffpl\/}]%
% \index{jobname.ffpl@{\em jobname.ffpl\/}}%
% \hfil\\
% ``flag'' file whose existence indicates that {\em jobname.frpl\/} contains
% valid data
%
% \item[{\em jobname.dfpl\/}]%
% \index{jobname.dfpl@{\em jobname.dfpl\/}}%
% \hfil\\
% ``flag'' file whose existence indicates that {\em jobname.ffpl\/} has been
% deleted
%
% \end{description}
%
% \subsection*{NOTES}
% {\bf perltex}'s sandbox defaults to what the {\em Opcode\/} manpage calls ``{\tt :browse}''.
%
% \subsection*{SEE ALSO}
% {\em latex\/}(1), {\em pdf{}latex\/}(1), {\em perl\/}(1), {\em Safe\/}(3pm), {\em Opcode\/}(3pm)
%
% \subsection*{AUTHOR}
% Scott Pakin, {\em scott+pt@pakin.org\/}
%
% ^^A  ***********************************
% ^^A  * End of generated pod2latex text *
% ^^A  ***********************************
% \endgroup
% \clearpage
%
%
% \StopEventually{^^A
%   \section{License agreement}
%   \label{sec:license}
%
%   Copyright \textcopyright{} 2006, Scott Pakin \texttt{<scott+pt@pakin.org>}
%
%   \providecommand{\url}[1]{\texttt{##1}}
%
%   \noindent
%   These files may be distributed and/or modified under the conditions of
%   the \LaTeX{} Project Public License, either version~1.3c of this
%   license or (at your option) any later version.  The latest version of
%   this license is in \url{http://www.latex-project.org/lppl.txt} and
%   version~1.3c or later is part of all distributions of \LaTeX{} version
%   2006/05/20 or later.
%
%   \PrintChanges
%   \PrintIndex
% }
%
% \section{Implementation}
% \label{sec:implementation}
%
% Users interested only in \emph{using} \PerlTeX{} can skip
% Section~\ref{sec:implementation}, which presents the complete
% \PerlTeX{} source code.  This section should be of interest primarily
% to those who wish to extend \PerlTeX{} or modify it to use a language
% other than Perl.
%
% Section~\ref{sec:implementation} is split into two main parts.
% Section~\ref{sec:perlmacros} presents the source code for \perlmac,
% the \LaTeX{} side of \PerlTeX, and Section~\ref{sec:perltex} presents
% the source code for \perltex, the Perl side of \PerlTeX\@.
% \makeatletter
% \@ifundefined{perlmaclines}{}{^^A
%   \newcount\perltex@lines
%   \perltex@lines=\perltexlines
%   \advance\perltex@lines by -\perlmaclines
%   In toto, \PerlTeX{} consists of a relatively small amount of code.
%   \perlmac{} is only \perlmaclines{} lines of \LaTeX{} and \perltex{}
%   is only \the\perltex@lines{} lines of Perl.
% }
% \makeatother
% \perltex{} is fairly straightforward Perl code and shouldn't be too
% difficult to understand by anyone comfortable with Perl programming.
% \perlmac, in contrast, contains a bit of \LaTeX{} trickery and is
% probably impenetrable to anyone who hasn't already tried his hand at
% \LaTeX{} programming.  Fortunately for the reader, the code is
% profusely commented so the aspiring \LaTeX{} guru may yet learn
% something from it.
%
% After documenting the \perlmac{} and \perltex{} source code, a few
% suggestions are provided for porting \PerlTeX{} to use a backend
% language other than Perl (Section~\ref{sec:porting}).
%
% \subsection{\perlmac}
% \label{sec:perlmacros}
%
% \iffalse
%<*package>
% \fi
%
% Although I've written a number of \LaTeX{} packages, \perlmac{} was
% the most challenging to date.  The key things I needed to learn how to
% do include the following:
%
% \begin{enumerate}
%   \item storing brace-matched---but otherwise not valid \LaTeX---code
%   for later use
%
%   \item iterating over a macro's arguments
% \end{enumerate}
%
% Storing non-\LaTeX{} code in a variable involves beginning a group in
% an argumentless macro, fiddling with category codes, using
% |\afterassignment| to specify a continuation function, and storing the
% subsequent brace-delimited tokens in the input stream into a token
% register.  The continuation function, which also takes no arguments,
% ends the group begun in the first function and proceeds using the
% correctly |\catcode|d token register.  This technique appears in
% |\plmac@haveargs| and |\plmac@havecode| and in a simpler form
% (i.e.,~without the need for storing the argument) in
% |\plmac@write@perl| and |\plmac@write@perl@i|.
%
% Iterating over a macro's arguments is hindered by \TeX's requirement
% that ``|#|'' be followed by a number or another ``|#|''.  The
% technique I discovered (which is used by the Texinfo source code) is
% first to |\let| a variable be |\relax|, thereby making it
% unexpandable, then to define a macro that uses that variable followed
% by a loop variable, and finally to expand the loop variable and |\let|
% the |\relax|ed variable be ``|#|'' right before invoking the macro.
% This technique appears in |\plmac@havecode|.
%
% I hope you find reading the \perlmac{} source code instructive.
% Writing it certainly was.
%
%
% \subsubsection{Package initialization}
% \label{sec:package-init}
%
% \PerlTeX{} defines six macros that are used for communication between
% Perl and \LaTeX\@.  |\plmac@tag| is a string of characters that should
% never occur within one of the user's macro names, macro arguments, or
% macro bodies.  \perltex{} therefore defines |\plmac@tag| as a long
% string of random uppercase letters.  |\plmac@tofile| is the name of a
% file used for communication from \LaTeX{} to Perl.  |\plmac@fromfile|
% is the name of a file used for communication from Perl to \LaTeX.
% |\plmac@toflag| signals that |\plmac@tofile| can be read safely.
% |\plmac@fromflag| signals that |\plmac@fromfile| can be read safely.
% |\plmac@doneflag| signals that |\plmac@fromflag| has been deleted.
% Table~\ref{tbl:variables} lists all of these variables along with the
% value assigned to each by \perltex.
%
% \begin{table}[htbp]
%   \centering
%   \caption{Variables used for communication between Perl and \LaTeX}
%   \label{tbl:variables}
%   \begin{tabular}{@{}lll@{}}
%     \hline
%     Variable & Purpose & \perltex{} assignment \\
%     \hline
%
%     |\plmac@tag| & |\plmac@tofile| field separator &
%     (20 random letters) \\
%
%     |\plmac@tofile| & \LaTeX{} $\rightarrow$ Perl communication &
%     |\jobname.topl| \\
%
%     |\plmac@fromfile| & Perl $\rightarrow$ \LaTeX{} communication &
%     |\jobname.frpl| \\
%
%     |\plmac@toflag| & |\plmac@tofile| synchronization &
%     |\jobname.tfpl| \\
%
%     |\plmac@fromflag| & |\plmac@fromfile| synchronization &
%     |\jobname.ffpl| \\
%
%     |\plmac@doneflag| & |\plmac@fromflag| synchronization &
%     |\jobname.dfpl| \\
%
%     \hline
%   \end{tabular}
% \end{table}
%
% \begin{macro}{\ifplmac@have@perltex}
% \begin{macro}{\plmac@have@perltextrue}
% \begin{macro}{\plmac@have@perltexfalse}
% The following block of code checks the existence of each of the
% variables listed in Table~\ref{tbl:variables}.  If any variable is not
% defined, \perlmac{} gives an error message and---as we shall see on
% page~\pageref{page:define-dummies}---defines dummy versions of
% |\perl|[|re|]|newcommand| and |\perl|[|re|]|newenvironment|.
%    \begin{macrocode}
\newif\ifplmac@have@perltex
\plmac@have@perltextrue
\@ifundefined{plmac@tag}{\plmac@have@perltexfalse}{}
\@ifundefined{plmac@tofile}{\plmac@have@perltexfalse}{}
\@ifundefined{plmac@fromfile}{\plmac@have@perltexfalse}{}
\@ifundefined{plmac@toflag}{\plmac@have@perltexfalse}{}
\@ifundefined{plmac@fromflag}{\plmac@have@perltexfalse}{}
\@ifundefined{plmac@doneflag}{\plmac@have@perltexfalse}{}
\ifplmac@have@perltex
\else
  \PackageError{perltex}{Document must be compiled using perltex}
    {Instead of compiling your document directly with latex, you need
     to\MessageBreak use the perltex script.  \space perltex sets up
     a variety of macros needed by\MessageBreak the perltex
     package as well as a listener process needed for\MessageBreak
     communication between LaTeX and Perl.}
\fi
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsubsection{Defining Perl macros}
% \label{sec:perl-mac}
%
% \PerlTeX{} defines five macros intended to be called by the author.
% Section~\ref{sec:perl-mac} details the implementation of two of them:
% |\perlnewcommand| and |\perlrenewcommand|.
% (Section~\ref{sec:perl-env} details the implementation of the next
% two, |\perlnewenvironment| and |\perlrenewenvironment|; and,
% Section~\ref{sec:perl-run} details the implementation of the final
% macro, |\perldo|.)  The goal is for these two macros to behave
% \emph{exactly} like |\newcommand| and |\renewcommand|, respectively,
% except that the author macros they in turn define have Perl bodies
% instead of \LaTeX{} bodies.
%
% The sequence of the operations defined in this section is as follows:
%
% \begin{enumerate}
%   \item The user invokes |\perl|[|re|]|newcommand|, which stores
%   |\|[|re|]|newcommand| in |\plmac@command|.  The
%   |\perl|[|re|]|newcommand| macro then invokes |\plmac@newcommand@i|
%   with a first argument of ``|*|'' for |\perl|[|re|]|newcommand*| or
%   ``|!|'' for ordinary |\perl|[|re|]|newcommand|.
%
%   \item |\plmac@newcommand@i| defines |\plmac@starchar| as ``|*|'' if
%   it was passed a ``|*|'' or \meta{empty} if it was passed a ``|!|''.
%   It then stores the name of the user's macro in |\plmac@macname|, a
%   |\write|able version of the name in |\plmac@cleaned@macname|, and
%   the macro's previous definition (needed by |\perlrenewcommand|) in
%   |\plmac@oldbody|.  Finally, |\plmac@newcommand@i| invokes
%   |\plmac@newcommand@ii|.
%
%   \item |\plmac@newcommand@ii| stores the number of arguments to the
%   user's macro (which may be zero) in |\plmac@numargs|.  It then
%   invokes |\plmac@newcommand@iii@opt| if the first argument is
%   supposed to be optional or |\plmac@newcommand@iii@no@opt| if all
%   arguments are supposed to be required.
%
%   \item |\plmac@newcommand@iii@opt| defines |\plmac@defarg| as the
%   default value of the optional argument.
%   |\plmac@newcommand@iii@no@opt| defines it as \meta{empty}.  Both
%   functions then call |\plmac@haveargs|.
%
%   \item |\plmac@haveargs| stores the user's macro body (written in
%   Perl) verbatim in |\plmac@perlcode|.  |\plmac@haveargs| then invokes
%   |\plmac@havecode|.
%
%   \item By the time |\plmac@havecode| is invoked all of the
%   information needed to define the user's macro is available.  Before
%   defining a \LaTeX{} macro, however, |\plmac@havecode| invokes
%   |\plmac@write@perl| to tell \perltex{} to define a Perl subroutine
%   with a name based on |\plmac@cleaned@macname| and the code contained
%   in |\plmac@perlcode|.  Figure~\ref{fig:tofile-define} illustrates
%   the data that |\plmac@write@perl| passes to \perltex.
%
% \begin{figure}[htbp]
%   \centering
%   \DeleteShortVerb\|
%   \begin{tabular}{|l|}
%     \hline
%     \verb|DEF|                    \\ \hline
%     \verb|\plmac@tag|             \\ \hline
%     \verb|\plmac@cleaned@macname| \\ \hline
%     \verb|\plmac@tag|             \\ \hline
%     \verb|\plmac@perlcode|        \\ \hline
%   \end{tabular}
%   \MakeShortVerb\|
%   \caption{Data written to \texttt{\string\plmac@tofile} to define a
%     Perl subroutine}
%   \label{fig:tofile-define}
% \end{figure}
%
%   \item |\plmac@havecode| invokes |\newcommand| or |\renewcommand|, as
%   appropriate, defining the user's macro as a call to
%   |\plmac@write@perl|.  An invocation of the user's \LaTeX{} macro
%   causes |\plmac@write@perl| to pass the information shown in
%   Figure~\ref{fig:tofile-use} to \perltex.
%
% \begin{figure}[htbp]
%   \centering
%   \DeleteShortVerb\|
%   \begin{tabular}{|l|}
%     \hline
%     \verb|USE|                    \\ \hline
%     \verb|\plmac@tag|             \\ \hline
%     \verb|\plmac@cleaned@macname| \\ \hline
%     \verb|\plmac@tag|             \\ \hline
%     \verb|#1|                     \\ \hline
%     \verb|\plmac@tag|             \\ \hline
%     \verb|#2|                     \\ \hline
%     \verb|\plmac@tag|             \\ \hline
%     \verb|#3|                     \\ \hline
%     \multicolumn{1}{c}{$\vdots$}  \\ \hline
%     \verb|#|\meta{last}           \\ \hline
%   \end{tabular}
%   \MakeShortVerb\|
%   \caption{Data written to \texttt{\string\plmac@tofile} to invoke a
%     Perl subroutine}
%   \label{fig:tofile-use}
% \end{figure}
%
%   \item Whenever |\plmac@write@perl| is invoked it writes its argument
%   verbatim to |\plmac@tofile|; \perltex{} evaluates the code and
%   writes |\plmac@fromfile|; finally, |\plmac@write@perl| |\input|s
%   |\plmac@fromfile|.
% \end{enumerate}
%
% An example might help distinguish the myriad macros used internally by
% \perlmac.  Consider the following call made by the user's
% document:\label{text:example-cmd}
%
% \begin{center}
% |\perlnewcommand*{\example}[3][frobozz]{join("---", @_)}|
% \end{center}
%
% \noindent
% Table~\ref{tbl:example-cmd} shows how \perlmac{} parses that command
% into its constituent components and which components are bound to
% which \perlmac{} macros.
%
% \begin{table}[htbp]
%   \centering
%   \caption{Macro assignments corresponding to an sample
%     \texttt{\string\perlnewcommand*}}
%   \label{tbl:example-cmd}
%   \begin{tabular}{@{}lll@{}}
%     \hline
%     Macro & \multicolumn{2}{l@{}}{Sample definition} \\
%     \hline
%     |\plmac@command|          & |\newcommand|                    \\
%     |\plmac@starchar|         & |*|                              \\
%     |\plmac@macname|          & |\example|                       \\
%     |\plmac@cleaned@macname|  & |\example| & (catcode~11)        \\
%     |\plmac@oldbody|          & |\relax| & (presumably)          \\
%     |\plmac@numargs|          & |3|                              \\
%     |\plmac@defarg|           & |frobozz|                        \\
%     |\plmac@perlcode|         & |join("---", @_)| & (catcode~11) \\
%     \hline
%   \end{tabular}
% \end{table}
%
% \bigskip
%
% \begin{macro}{\perlnewcommand}
% \begin{macro}{\perlrenewcommand}
% \begin{macro}{\plmac@command}
% \begin{macro}{\plmac@next}
% |\perlnewcommand| and |\perlrenewcommand| are the first two commands
% exported to the user by \perlmac.  |\perlnewcommand| is analogous to
% |\newcommand| except that the macro body consists of Perl code instead
% of \LaTeX{} code.  Likewise, |\perlrenewcommand| is analogous to
% |\renewcommand| except that the macro body consists of Perl code
% instead of \LaTeX{} code.  |\perlnewcommand| and |\perlrenewcommand|
% merely define |\plmac@command| and |\plmac@next| and invoke
% |\plmac@newcommand@i|.
%    \begin{macrocode}
\def\perlnewcommand{%
  \let\plmac@command=\newcommand
  \let\plmac@next=\relax
  \@ifnextchar*{\plmac@newcommand@i}{\plmac@newcommand@i!}%
}
%    \end{macrocode}
%    \begin{macrocode}
\def\perlrenewcommand{%
  \let\plmac@next=\relax
  \let\plmac@command=\renewcommand
  \@ifnextchar*{\plmac@newcommand@i}{\plmac@newcommand@i!}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\plmac@newcommand@i}
% \begin{macro}{\plmac@starchar}
% \begin{macro}{\plmac@macname}
% \begin{macro}{\plmac@oldbody}
% \begin{macro}{\plmac@cleaned@macname}
% If\label{page:newcommand-i} the user invoked
% |\perl|[|re|]|newcommand*| then |\plmac@newcommand@i| is passed a
% ``|*|'' and, in turn, defines |\plmac@starchar| as ``|*|''.  If the
% user invoked |\perl|[|re|]|newcommand| (no ``|*|'') then
% |\plmac@newcommand@i| is passed a ``|!|'' and, in turn, defines
% |\plmac@starchar| as \meta{empty}.  In either case,
% |\plmac@newcommand@i| defines |\plmac@macname| as the name of the
% user's macro, |\plmac@cleaned@macname| as a |\write|able
% (i.e.,~category code~11) version of |\plmac@macname|, and
% |\plmac@oldbody| and the previous definition of the user's macro.
% (|\plmac@oldbody| is needed by |\perlrenewcommand|.)  It then invokes
% |\plmac@newcommand@ii|.
%    \begin{macrocode}
\def\plmac@newcommand@i#1#2{%
  \ifx#1*%
    \def\plmac@starchar{*}%
  \else
    \def\plmac@starchar{}%
  \fi
  \def\plmac@macname{#2}%
  \let\plmac@oldbody=#2\relax
  \expandafter\def\expandafter\plmac@cleaned@macname\expandafter{%
    \expandafter\string\plmac@macname}%
  \@ifnextchar[{\plmac@newcommand@ii}{\plmac@newcommand@ii[0]}%]
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\plmac@newcommand@ii}
% \begin{macro}{\plmac@numargs}
% |\plmac@newcommand@i| invokes |\plmac@newcommand@ii| with the number
% of arguments to the user's macro in brackets.  |\plmac@newcommand@ii|
% stores that number in |\plmac@numargs| and invokes
% |\plmac@newcommand@iii@opt| if the first argument is to be optional or
% |\plmac@newcommand@iii@no@opt| if all arguments are to be mandatory.
%    \begin{macrocode}
\def\plmac@newcommand@ii[#1]{%
  \def\plmac@numargs{#1}%
  \@ifnextchar[{\plmac@newcommand@iii@opt}
               {\plmac@newcommand@iii@no@opt}%]
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\plmac@newcommand@iii@opt}
% \begin{macro}{\plmac@newcommand@iii@no@opt}
% \begin{macro}{\plmac@defarg}
% Only one of these two macros is executed per invocation of
% |\perl|[|re|]|newcommand|, depending on whether or not the first
% argument of the user's macro is an optional argument.
% |\plmac@newcommand@iii@opt| is invoked if the argument is optional.
% It defines |\plmac@defarg| to the default value of the optional
% argument.  |\plmac@newcommand@iii@no@opt| is invoked if all arguments
% are mandatory.  It defines |\plmac@defarg| as |\relax|.  Both
% |\plmac@newcommand@iii@opt| and |\plmac@newcommand@iii@no@opt| then
% invoke |\plmac@haveargs|.
%    \begin{macrocode}
\def\plmac@newcommand@iii@opt[#1]{%
  \def\plmac@defarg{#1}%
  \plmac@haveargs
}
%    \end{macrocode}
%    \begin{macrocode}
\def\plmac@newcommand@iii@no@opt{%
  \let\plmac@defarg=\relax
  \plmac@haveargs
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\plmac@perlcode}
% \begin{macro}{\plmac@haveargs}
% Now things start to get tricky.  We have all of the arguments we need
% to define the user's command so all that's left is to grab the macro
% body.  But there's a catch: Valid Perl code is unlikely to be valid
% \LaTeX{} code.  We therefore have to read the macro body in a
% |\verb|-like mode.  Furthermore, we actually need to \emph{store} the
% macro body in a variable, as we don't need it right away.
%
% The approach we take in |\plmac@haveargs| is as follows.  First, we
% give all ``special'' characters category code~12 (``other'').  We then
% indicate that the carriage return character (control-M) marks the end
% of a line and that curly braces retain their normal meaning.  With the
% aforementioned category-code definitions, we now have to store the
% next curly-brace-delimited fragment of text, end the current group to
% reset all category codes to their previous value, and continue
% processing the user's macro definition.  How do we do that?  The
% answer is to assign the upcoming text fragment to a token register
% (|\plmac@perlcode|) while an |\afterassignment| is in effect.  The
% |\afterassignment| causes control to transfer to |\plmac@havecode|
% right after |\plmac@perlcode| receives the macro body with all of the
% ``special'' characters made impotent.
%    \begin{macrocode}
\newtoks\plmac@perlcode
%    \end{macrocode}
%    \begin{macrocode}
\def\plmac@haveargs{%
  \begingroup
    \let\do\@makeother\dospecials
    \catcode`\^^M=\active
    \newlinechar`\^^M
    \endlinechar=`\^^M
    \catcode`\{=1
    \catcode`\}=2
    \afterassignment\plmac@havecode
    \global\plmac@perlcode
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% Control is transfered to |\plmac@havecode| from |\plmac@haveargs|
% right after the user's macro body is assigned to |\plmac@perlcode|.
% We now have everything we need to define the user's macro.  The goal
% is to define it as ``|\plmac@write@perl{|\meta{contents of
% Figure~\ref{fig:tofile-use}}|}|''.  This is easier said than done
% because the number of arguments in the user's macro is not known
% statically, yet we need to iterate over however many arguments there
% are.  Because of this complexity, we will explain |\plmac@perlcode|
% piece-by-piece.
%
% \begin{macro}{\plmac@sep}
% Define a character to separate each of the items presented in
% Figures~\ref{fig:tofile-define} and~\ref{fig:tofile-use}.  Perl will
% need to strip this off each argument.  For convenience in porting to
% languages with less powerful string manipulation than Perl's, we
% define |\plmac@sep| as a carriage-return character of category code~11
% (``letter'').
%    \begin{macrocode}
{\catcode`\^^M=11\gdef\plmac@sep{^^M}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\plmac@argnum}
% Define a loop variable that will iterate from |1| to the number of
% arguments in the user's function, i.e.,~|\plmac@numargs|.
%    \begin{macrocode}
\newcount\plmac@argnum
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\plmac@havecode}
% Now comes the final piece of what started as a call to
% |\perl|[|re|]|newcommand|.  First, to reset all category codes back to
% normal, |\plmac@havecode| ends the group that was begun in
% |\plmac@haveargs|.
% \changes{v1.1}{2004/02/24}{Added a \texttt{\string\string\string\plmac@next}
%   hook to support \PerlTeX's new environment-defining macros}
%    \begin{macrocode}
\def\plmac@havecode{%
  \endgroup
%    \end{macrocode}
%
% \begin{macro}{\plmac@define@sub}
% We invoke |\plmac@write@perl| to define a Perl subroutine named after
% |\plmac@cleaned@macname|.  |\plmac@define@sub| sends Perl the
% information shown in Figure~\vref{fig:tofile-define}.
%    \begin{macrocode}
  \edef\plmac@define@sub{%
    \noexpand\plmac@write@perl{DEF\plmac@sep
      \plmac@tag\plmac@sep
      \plmac@cleaned@macname\plmac@sep
      \plmac@tag\plmac@sep
      \the\plmac@perlcode
    }%
  }%
  \plmac@define@sub
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\plmac@body}
% The rest of |\plmac@havecode| is preparation for defining the user's
% macro.  (\LaTeXe's |\newcommand| or |\renewcommand| will do the actual
% work, though.)  |\plmac@body| will eventually contain the complete
% (\LaTeX) body of the user's macro.  Here, we initialize it to the
% first three items listed in Figure~\vref{fig:tofile-use} (with
% intervening |\plmac@sep|s).
%    \begin{macrocode}
  \edef\plmac@body{%
    USE\plmac@sep
    \plmac@tag\plmac@sep
    \plmac@cleaned@macname
  }%
%    \end{macrocode}
%
% \begin{macro}{\plmac@hash}
% Now, for each argument |#1|, |#2|,~\dots, |#\plmac@numargs| we append
% a |\plmac@tag| plus the argument to |\plmac@body| (as always, with a
% |\plmac@sep| after each item).  This requires more trickery, as \TeX{}
% requires a macro-parameter character (``|#|'') to be followed by a
% literal number, not a variable.  The approach we take, which I first
% discovered in the Texinfo source code (although it's used by \LaTeX{}
% and probably other \TeX-based systems as well), is to |\let|-bind
% |\plmac@hash| to |\relax|.  This makes |\plmac@hash| unexpandable, and
% because it's not a ``|#|'', \TeX{} doesn't complain.  After
% |\plmac@body| has been extended to include |\plmac@hash1|,
% |\plmac@hash2|,~\dots, |\plmac@hash\plmac@numargs|, we then
% |\let|-bind |\plmac@hash| to |##|, which \TeX{} lets us do because
% we're within a macro definition (|\plmac@havecode|).  |\plmac@body|
% will then contain |#1|, |#2|,~\dots, |#\plmac@numargs|, as desired.
%    \begin{macrocode}
  \let\plmac@hash=\relax
  \plmac@argnum=\@ne
  \loop
    \ifnum\plmac@numargs<\plmac@argnum
    \else
      \edef\plmac@body{%
        \plmac@body\plmac@sep\plmac@tag\plmac@sep
        \plmac@hash\plmac@hash\number\plmac@argnum}%
      \advance\plmac@argnum by \@ne
  \repeat
  \let\plmac@hash=##%
%    \end{macrocode}
%
% \begin{macro}{\plmac@define@command}
% We're ready to execute a |\|[|re|]|newcommand|.  Because we need to
% expand many of our variables, we |\edef| |\plmac@define@command| to
% the appropriate |\|[|re|]|newcommand| call, which we will soon
% execute.  The user's macro must first be |\let|-bound to |\relax| to
% prevent it from expanding.  Then, we handle two cases: either all
% arguments are mandatory (and |\plmac@defarg| is |\relax|) or the
% user's macro has an optional argument (with default value
% |\plmac@defarg|).
%    \begin{macrocode}
  \expandafter\let\plmac@macname=\relax
  \ifx\plmac@defarg\relax
    \edef\plmac@define@command{%
      \noexpand\plmac@command\plmac@starchar{\plmac@macname}%
      [\plmac@numargs]{%
        \noexpand\plmac@write@perl{\plmac@body}%
      }%
  }%
  \else
    \edef\plmac@define@command{%
      \noexpand\plmac@command\plmac@starchar{\plmac@macname}%
      [\plmac@numargs][\plmac@defarg]{%
        \noexpand\plmac@write@perl{\plmac@body}%
      }%
  }%
  \fi
%    \end{macrocode}
%
% The final steps are to restore the previous definition of the user's
% macro---we had set it to |\relax| above to make the name
% unexpandable---then redefine it by invoking |\plmac@define@command|.
% Why do we need to restore the previous definition if we're just going
% to redefine it?  Because |\newcommand| needs to produce an error if
% the macro was previously defined and |\renewcommand| needs to produce
% an error if the macro was \emph{not} previously defined.
%
% |\plmac@havecode| concludes by invoking |\plmac@next|, which is a
% no-op for |\perlnewcommand| and |\perlrenewcommand| but processes the
% end-environment code for |\perlnewenvironment| and
% |\perlrenewenvironment|.
%    \begin{macrocode}
  \expandafter\let\plmac@macname=\plmac@oldbody
  \plmac@define@command
  \plmac@next
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \subsubsection{Defining Perl environments}
% \label{sec:perl-env}
%
% \changes{v1.1}{2004/02/24}{Added new
%   \texttt{\string\string\string\perlnewenvironment} and
%   \texttt{\string\string\string\perlrenewenvironment} macros}
%
% Section~\ref{sec:perl-mac} detailed the implementation of
% |\perlnewcommand| and |\perlrenewcommand|.  Section~\ref{sec:perl-env}
% does likewise for |\perlnewenvironment| and |\perlrenewenvironment|,
% which are the Perl-bodied analogues of |\newenvironment| and
% |\renewenvironment|.  This section is significantly shorter than the
% previous because |\perlnewenvironment| and |\perlrenewenvironment| are
% largely built atop the macros already defined in
% Section~\ref{sec:perl-mac}.
%
% \begin{macro}{\perlnewenvironment}
% \begin{macro}{\perlrenewenvironment}
% \begin{macro}{\plmac@command}
% \begin{macro}{\plmac@next}
% |\perlnewenvironment| and |\perlrenewenvironment| are the remaining
% two commands exported to the user by \perlmac.  |\perlnewenvironment|
% is analogous to |\newenvironment| except that the macro body consists
% of Perl code instead of \LaTeX{} code.  Likewise,
% |\perlrenewenvironment| is analogous to |\renewenvironment| except
% that the macro body consists of Perl code instead of \LaTeX{} code.
% |\perlnewenvironment| and |\perlrenewenvironment| merely define
% |\plmac@command| and |\plmac@next| and invoke
% |\plmac@newenvironment@i|.
%
% The significance of |\plmac@next| (which was let-bound to |\relax| for
% |\perl|[|re|]|newcommand| but is let-bound to |\plmac@end@environment|
% here) is that a \LaTeX{} environment definition is really two macro
% definitions: |\|\meta{name} and |\end|\meta{name}.  Because we want to
% reuse as much code as possible the idea is to define the ``begin''
% code as one macro, then inject---by way of |plmac@next|---a call to
% |\plmac@end@environment|, which defines the ``end'' code as a second
% macro.
%    \begin{macrocode}
\def\perlnewenvironment{%
  \let\plmac@command=\newcommand
  \let\plmac@next=\plmac@end@environment
  \@ifnextchar*{\plmac@newenvironment@i}{\plmac@newenvironment@i!}%
}
%    \end{macrocode}
%    \begin{macrocode}
\def\perlrenewenvironment{%
  \let\plmac@command=\renewcommand
  \let\plmac@next=\plmac@end@environment
  \@ifnextchar*{\plmac@newenvironment@i}{\plmac@newenvironment@i!}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\plmac@newenvironment@i}
% \begin{macro}{\plmac@starchar}
% \begin{macro}{\plmac@envname}
% \begin{macro}{\plmac@macname}
% \begin{macro}{\plmac@oldbody}
% \begin{macro}{\plmac@cleaned@macname}
% The |\plmac@newenvironment@i| macro is analogous to
% |\plmac@newcommand@i|; see the description of
% |\plmac@newcommand@i|~\vpageref{page:newcommand-i} to understand the
% basic structure.  The primary difference is that the environment name
% (|#2|) is just text, not a control sequence.  We store this text in
% |\plmac@envname| to facilitate generating the names of the two macros
% that constitute an environment definition.  Note that there is no
% |\plmac@newenvironment@ii|; control passes instead to
% |\plmac@newcommand@ii|.
%    \begin{macrocode}
\def\plmac@newenvironment@i#1#2{%
  \ifx#1*%
    \def\plmac@starchar{*}%
  \else
    \def\plmac@starchar{}%
  \fi
  \def\plmac@envname{#2}%
  \expandafter\def\expandafter\plmac@macname\expandafter{\csname#2\endcsname}%
  \expandafter\let\expandafter\plmac@oldbody\plmac@macname\relax
  \expandafter\def\expandafter\plmac@cleaned@macname\expandafter{%
    \expandafter\string\plmac@macname}%
  \@ifnextchar[{\plmac@newcommand@ii}{\plmac@newcommand@ii[0]}%]
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\plmac@end@environment}
% \begin{macro}{\plmac@next}
% \begin{macro}{\plmac@macname}
% \begin{macro}{\plmac@oldbody}
% \begin{macro}{\plmac@cleaned@macname}
% Recall that an environment definition is a shortcut for two macro
% definitions: |\|\meta{name} and |\end|\meta{name} (where \meta{name}
% was stored in |\plmac@envname| by |\plmac@newenvironment@i|).  After
% defining |\|\meta{name}, |\plmac@havecode| transfers control to
% |\plmac@end@environment| because |\plmac@next| was let-bound to
% |\plmac@end@environment| in |\perl|[|re|]|newenvironment|.
%
% |\plmac@end@environment|'s purpose is to define |\end|\meta{name}.
% This is a little tricky, however, because \LaTeX's
% |\|[|re|]|newcommand| refuses to (re)define a macro whose name begins
% with ``|end|''.  The solution that |\plmac@end@environment| takes is
% first to define a |\plmac@end@macro| macro then (in |plmac@next|)
% let-bind |\end|\meta{name} to it.  Other than that,
% |\plmac@end@environment| is a combined and simplified version of
% |\perlnewenvironment|, |\perlrenewenvironment|, and
% |\plmac@newenvironment@i|.
%    \begin{macrocode}
\def\plmac@end@environment{%
  \expandafter\def\expandafter\plmac@next\expandafter{\expandafter
    \let\csname end\plmac@envname\endcsname=\plmac@end@macro
    \let\plmac@next=\relax
  }%
  \def\plmac@macname{\plmac@end@macro}%
  \expandafter\let\expandafter\plmac@oldbody\csname end\plmac@envname\endcsname
  \expandafter\def\expandafter\plmac@cleaned@macname\expandafter{%
    \expandafter\string\plmac@macname}%
  \@ifnextchar[{\plmac@newcommand@ii}{\plmac@newcommand@ii[0]}%]
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \subsubsection{Executing top-level Perl code}
% \label{sec:perl-run}
%
% The macros defined in Sections~\ref{sec:perl-mac}
% and~\ref{sec:perl-env} enable an author to inject subroutines into the
% Perl sandbox.  The final \PerlTeX\ macro, |\perldo|, instructs the
% Perl sandbox to execute a block of code outside of all subroutines.
% |\perldo|'s implementation is much simpler than that of the other
% author macros because |\perldo| does not have to process subroutine
% arguments.  Figure~\ref{fig:tofile-run} illustrates the data that gets
% written to |plmac@tofile| (indirectly) by |\perldo|.
%
% \begin{figure}[htbp]
%   \centering
%   \DeleteShortVerb\|
%   \begin{tabular}{|l|}
%     \hline
%     \verb|RUN|             \\ \hline
%     \verb|\plmac@tag|      \\ \hline
%     \textit{Ignored}       \\ \hline
%     \verb|\plmac@tag|      \\ \hline
%     \verb|\plmac@perlcode| \\ \hline
%   \end{tabular}
%   \MakeShortVerb\|
%   \caption{Data written to \texttt{\string\plmac@tofile} to execute
%     Perl code}
%   \label{fig:tofile-run}
% \end{figure}
%
%
% \begin{macro}{\perldo}
% Execute a block of Perl code and pass the result to \LaTeX\ for
% further processing.  This code is nearly identical to that of
% Section~\ref{sec:perl-mac}'s |\plmac@haveargs| but ends by invoking
% |\plmac@have@run@code| instead of |\plmac@havecode|.
% \changes{v1.3}{2006/06/23}{Introduced \texttt{\string\string\string\perldo}
%   to support code execution outside of all subroutines.}
%    \begin{macrocode}
\def\perldo{%
  \begingroup
    \let\do\@makeother\dospecials
    \catcode`\^^M=\active
    \newlinechar`\^^M
    \endlinechar=`\^^M
    \catcode`\{=1
    \catcode`\}=2
    \afterassignment\plmac@have@run@code
    \global\plmac@perlcode
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\plmac@have@run@code}
% \begin{macro}{\plmac@run@code}
% Pass a block of code to Perl to execute.  |\plmac@have@run@code| is
% identical to |\plmac@havecode| but specifies the |RUN| tag instead of
% the |DEF| tag.
% \changes{v1.3}{2006/06/23}{Added to assist
%   \texttt{\string\string\string\perldo}}
%    \begin{macrocode}
\def\plmac@have@run@code{%
  \endgroup
  \edef\plmac@run@code{%
    \noexpand\plmac@write@perl{RUN\plmac@sep
      \plmac@tag\plmac@sep
      N/A\plmac@sep
      \plmac@tag\plmac@sep
      \the\plmac@perlcode
    }%
  }%
  \plmac@run@code
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \subsubsection{Communication between \LaTeX{} and Perl}
%
% As shown in the previous section, when a document invokes
% |\perl|[|re|]|newcommand| to define a macro, \perlmac{} defines the
% macro in terms of a call to |\plmac@write@perl|.  In this section, we
% learn how |\plmac@write@perl| operates.
%
% At the highest level, \LaTeX-to-Perl communication is performed via
% the filesystem.  In essence, \LaTeX{} writes a file (|\plmac@tofile|)
% corresponding to the information in either
% Figure~\ref{fig:tofile-define} or Figure~\ref{fig:tofile-use}; Perl
% reads the file, executes the code within it, and writes a |.tex| file
% (|\plmac@fromfile|); and, finally, \LaTeX{} reads and executes the new
% |.tex| file.  However, the actual communication protocol is a bit more
% involved than that.  The problem is that Perl needs to know when
% \LaTeX{} has finished writing Perl code and \LaTeX{} needs to know
% when Perl has finished writing \LaTeX{} code.  The solution involves
% introducing three extra files---|\plmac@toflag|, |\plmac@fromflag|,
% and |\plmac@doneflag|---which are used exclusively for \LaTeX-to-Perl
% synchronization.
%
% There's a catch: Although Perl can create and delete files, \LaTeX{}
% can only create them.  Even worse, \LaTeX{} (more specifically,
% te\TeX, which is the \TeX{} distribution under which I developed
% \PerlTeX) cannot reliably poll for a file's \emph{non}existence; if a
% file is deleted in the middle of an |\immediate\openin|, |latex|
% aborts with an error message.  These restrictions led to the
% regrettably convoluted protocol illustrated in
% Figure~\ref{fig:comm-protocol}.  In the figure, ``Touch'' means
% ``create a zero-length file''; ``Await'' means ``wait until the file
% exists''; and, ``Read'', ``Write'', and ``Delete'' are defined as
% expected.  Assuming the filesystem performs these operations in a
% sequentially consistent order (not necessarily guaranteed on all
% filesystems, unfortunately), \PerlTeX{} should behave as expected.
%
% \begin{figure}[htbp]
%   \centering
%   \makeatletter\setlength{\unitlength}{\baselineskip}\makeatother
%   \DeleteShortVerb\|
%   \def\topl{\texttt{\string\plmac@tofile}}
%   \def\frpl{\texttt{\string\plmac@fromfile}}
%   \def\tfpl{\texttt{\string\plmac@toflag}}
%   \def\ffpl{\texttt{\string\plmac@fromflag}}
%   \def\dfpl{\texttt{\string\plmac@doneflag}}
%   \begin{tabular}{@{}c@{\hspace*{1cm}}|l@{~}l|c|l@{~}l|@{}}
%     \multicolumn{1}{@{}c@{\hspace*{1cm}}}{Time} &
%     \multicolumn{2}{c}{\LaTeX} &
%     \multicolumn{1}{c}{} &
%     \multicolumn{2}{c@{}}{Perl} \\
%
%     \cline{2-3}\cline{5-6}
%     \smash{\vector(0,-1){11}}
%     & Write & \topl &               &        &       \\
%     & Touch & \tfpl & $\rightarrow$ & Await  & \tfpl \\
%     &       &       &               & Read   & \topl \\
%     &       &       &               & Write  & \frpl \\
%     &       &       &               & Delete & \tfpl \\
%     &       &       &               & Delete & \topl \\
%     &       &       &               & Delete & \dfpl \\
%     & Await & \ffpl & $\leftarrow$  & Touch  & \ffpl \\
%     & Read  & \frpl &               &        &       \\
%     & Touch & \topl & $\rightarrow$ & Await  & \topl \\
%     &       &       &               & Delete & \ffpl \\
%     & Await & \dfpl & $\leftarrow$  & Touch  & \dfpl \\
%     \cline{2-3}\cline{5-6}
%   \end{tabular}
%   \MakeShortVerb\|
%   \caption{\LaTeX-to-Perl communication protocol}
%   \label{fig:comm-protocol}
% \end{figure}
%
% \begin{macro}{\plmac@await@existence}
% \begin{macro}{\ifplmac@file@exists}
% \begin{macro}{\plmac@file@existstrue}
% \begin{macro}{\plmac@file@existsfalse}
% The purpose of the |\plmac@await@existence| macro is to repeatedly
% check the existence of a given file until the file actually exists.
% For convenience, we use \LaTeXe's |\IfFileExists| macro to check the
% file and invoke |\plmac@file@existstrue| or |\plmac@file@existsfalse|,
% as appropriate.
%    \begin{macrocode}
\newif\ifplmac@file@exists
%    \end{macrocode}
%    \begin{macrocode}
\newcommand{\plmac@await@existence}[1]{%
  \loop
    \IfFileExists{#1}%
                 {\plmac@file@existstrue}%
                 {\plmac@file@existsfalse}%
    \ifplmac@file@exists
    \else
  \repeat
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\plmac@outfile}
% We define a file handle for |\plmac@write@perl@i| to use to create and
% write |\plmac@tofile| and |\plmac@toflag|.
%    \begin{macrocode}
\newwrite\plmac@outfile
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\plmac@write@perl}
% |\plmac@write@perl| begins the \LaTeX-to-Perl data exchange, following
% the protocol illustrated in Figure~\ref{fig:comm-protocol}.
% |\plmac@write@perl| prepares for the next piece of text in the input
% stream to be read with ``special'' characters marked as category
% code~12 (``other'').  This prevents \LaTeX{} from complaining if the
% Perl code contains invalid \LaTeX{} (which it usually will).
% |\plmac@write@perl| ends by passing control to |\plmac@write@perl@i|,
% which performs the bulk of the work.
%    \begin{macrocode}
\newcommand{\plmac@write@perl}{%
  \begingroup
    \let\do\@makeother\dospecials
    \catcode`\^^M=\active
    \newlinechar`\^^M
    \endlinechar=`\^^M
    \catcode`\{=1
    \catcode`\}=2
    \plmac@write@perl@i
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\plmac@write@perl@i}
% When |\plmac@write@perl@i| begins executing, the category codes are
% set up so that the macro's argument will be evaluated ``verbatim''
% except for the part consisting of the \LaTeX\ code passed in by the
% author, which is partially expanded.  Thus, everything is in place for
% |\plmac@write@perl@i| to send its argument to Perl and read back the
% (\LaTeX) result.
%
% Because\label{page:define-dummies} all of \perlmac's protocol
% processing is encapsulated within |\plmac@write@perl@i|, this is the
% only macro that strictly requires \perltex.  Consequently, we wrap the
% entire macro definition within a check for \perltex.
%    \begin{macrocode}
\ifplmac@have@perltex
  \newcommand{\plmac@write@perl@i}[1]{%
%    \end{macrocode}
% The first step is to write argument |#1| to |\plmac@tofile|:
% \changes{v1.1}{2004/02/19}{Made argument-handling more rational by
%   making \texttt{\string\string\string\protect},
%   \texttt{\string\string\string\begin}, and
%   \texttt{\string\string\string\end} non-expandable}
%    \begin{macrocode}
      \immediate\openout\plmac@outfile=\plmac@tofile\relax
      \let\protect=\noexpand
      \def\begin{\noexpand\begin}%
      \def\end{\noexpand\end}%
      \immediate\write\plmac@outfile{#1}%
      \immediate\closeout\plmac@outfile
%    \end{macrocode}
% \noindent
% (In the future, it might be worth redefining |\def|, |\edef|, |\gdef|,
% |\xdef|, |\let|, and maybe some other control sequences as
% ``|\noexpand|\meta{control sequence}|\noexpand|'' so that |\write|
% doesn't try to expand an undefined control sequence.)
%
% We're now finished using |#1| so we can end the group begun by
% |\plmac@write@perl|, thereby resetting each character's category code
% back to its previous value.
%    \begin{macrocode}
    \endgroup
%    \end{macrocode}
%
% Continuing the protocol illustrated in Figure~\ref{fig:comm-protocol},
% we create a zero-byte |\plmac@toflag| in order to notify \perltex{}
% that it's now safe to read |\plmac@tofile|.
%    \begin{macrocode}
    \immediate\openout\plmac@outfile=\plmac@toflag\relax
    \immediate\closeout\plmac@outfile
%    \end{macrocode}
%
% To avoid reading |\plmac@fromfile| before \perltex{} has finished
% writing it we must wait until \perltex{} creates |\plmac@fromflag|,
% which it does only after it has written |\plmac@fromfile|.
%    \begin{macrocode}
    \plmac@await@existence\plmac@fromflag
%    \end{macrocode}
%
% At this point, |\plmac@fromfile| should contain valid \LaTeX{} code.
% However, we defer inputting it until we the very end.  Doing so
% enables recursive and mutually recursive invocations of \PerlTeX{}
% macros.
% \changes{v1.2}{2004/10/07}{Moved the
%   \texttt{\string\string\string\input} of the generated Perl code to
%   the end of the routine in order to support recursive \PerlTeX{}
%   macro invocations.}
%
% Because \TeX{} can't delete files we require an additional
% \LaTeX-to-Perl synchronization step.  For convenience, we recycle
% |\plmac@tofile| as a synchronization file rather than introduce yet
% another flag file to complement |\plmac@toflag|, |\plmac@fromflag|,
% and |\plmac@doneflag|.
%    \begin{macrocode}
    \immediate\openout\plmac@outfile=\plmac@tofile\relax
    \immediate\closeout\plmac@outfile
    \plmac@await@existence\plmac@doneflag
%    \end{macrocode}
%
% The only thing left to do is to |\input| and evaluate
% |\plmac@fromfile|, which contains the \LaTeX{} output from the Perl
% subroutine.
%    \begin{macrocode}
    \input\plmac@fromfile\relax
  }
%    \end{macrocode}
%
% \changes{v1.1}{2004/02/24}{Added a dummy version of the macro to use
%   if \texttt{latex} was launched directly, without \perltex}
% The foregoing code represents the ``real'' definition of
% |\plmac@write@perl@i|.  For the user's convenience, we define a dummy
% version of |\plmac@write@perl@i| so that a document which utilizes
% \perlmac{} can still compile even if not built using \perltex.  All
% calls to macros defined with |\perl|[|re|]|newcommand| and all
% invocations of environments defined with |\perl|[|re|]|newenvironment|
% are replaced with ``\fbox{Perl\TeX}''.  A minor complication is that
% text can't be inserted before the |\begin{document}|.  Hence, we
% initially define |\plmac@write@perl@i| as a do-nothing macro and
% redefine it as ``|\fbox{Perl\TeX}|'' at the |\begin{document}|.
%    \begin{macrocode}
\else
  \newcommand{\plmac@write@perl@i}[1]{\endgroup}
  \AtBeginDocument{%
    \renewcommand{\plmac@write@perl@i}[1]{%
%    \end{macrocode}
% \begin{macro}{\plmac@show@placeholder}
% There's really no point in outputting a framed ``Perl\TeX'' when a
% macro is defined \emph{and} when it's used.  |\plmac@show@placeholder|
% checks the first character of the protocol header.  If it's
% ``D''~(|DEF|), nothing is output.  Otherwise, it'll be ``U''~(|USE|)
% and ``Perl\TeX'' will be output.
%    \begin{macrocode}
      \def\plmac@show@placeholder##1##2\@empty{%
        \ifx##1D\relax
          \endgroup
        \else
          \endgroup
          \fbox{Perl\TeX}%
        \fi
      }%
      \plmac@show@placeholder#1\@empty
    }%
  }
\fi
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \iffalse
%</package>
% \fi
%
% ^^A  Keep track of the number of lines of code in perltex.
% \makeatletter
% \immediate\write\@auxout{^^A
%   \noexpand\gdef\noexpand\perlmaclines{\the\c@CodelineNo}}
% \makeatother
%
%
% \subsection{\perltex}
% \label{sec:perltex}
%
% \perltex{} is a wrapper script for |latex| (or any other \LaTeX{}
% compiler).  It sets up client-server communication between \LaTeX{}
% and Perl, with \LaTeX{} as the client and Perl as the server.  When a
% \LaTeX{} document sends a piece of Perl code to \perltex{} (with the
% help of \perlmac, as detailed in Section~\ref{sec:perlmacros}),
% \perltex{} executes it within a secure sandbox and transmits the
% resulting \LaTeX{} code back to the document.
%
% \iffalse
%<*perltex>
% \fi
%
% \subsubsection{Header comments}
%
% Because \perltex{} is generated without a \textsf{DocStrip}
% preamble or postamble we have to manually include the desired
% text as Perl comments.
%
%    \begin{macrocode}
#! /usr/bin/env perl

###########################################################
# Prepare a LaTeX run for two-way communication with Perl #
# By Scott Pakin <scott+pt@pakin.org>                     #
###########################################################

#-------------------------------------------------------------------
# This is file `perltex.pl',
# generated with the docstrip utility.
#
# The original source files were:
#
# perltex.dtx  (with options: `perltex')
#
# This is a generated file.
#
# Copyright (C) 2006, Scott Pakin <scott+pt@pakin.org>
#
# This file may be distributed and/or modified under the conditions
# of the LaTeX Project Public License, either version 1.3c of this
# license or (at your option) any later version.  The latest
# version of this license is in:
#
#    http://www.latex-project.org/lppl.txt
#
# and version 1.3c or later is part of all distributions of LaTeX
# version 2006/05/20 or later.
#-------------------------------------------------------------------

%    \end{macrocode}
%
% \subsubsection{Top-level code evaluation}
%
% In previous versions of \perltex, the |--nosafe| option created and
% ran code within a sandbox in which all operations are allowed (via
% |Opcode::full_opset()|).  Unfortunately, certain operations still fail
% to work within such a sandbox.  We therefore define a top-level
% ``non-sandbox'', |top_level_eval()|, in which to execute code.
% |top_level_eval()| merely calls |eval()| on its argument.  However, it
% needs to be declared top-level and before anything else because
% |eval()| runs in the lexical scope of its caller.
% \changes{v1.3}{2006/06/24}{Modified \perltex\ to eschew the sandbox
%   altogether when \texttt{--nosafe} is specified}
%
%    \begin{macrocode}
sub top_level_eval ($)
{
    return eval $_[0];
}
%    \end{macrocode}
%
% \subsubsection{Perl modules and pragmas}
%
% We use |Safe| and |Opcode| to implement the secure sandbox,
% |Getopt::Long| and |Pod::Usage| to parse the command line, and
% various other modules and pragmas for miscellaneous things.
%    \begin{macrocode}
use Safe;
use Opcode;
use Getopt::Long;
use Pod::Usage;
use File::Basename;
use POSIX;
use warnings;
use strict;
%    \end{macrocode}
%
% \subsubsection{Variable declarations}
%
% With |use strict| in effect, we need to declare all of our variables.
% For clarity, we separate our global-variable declarations into
% variables corresponding to command-line options and other global
% variables.
%
% \paragraph{Variables corresponding to command-line arguments}
% \begin{perlmacro}{$latexprog}
% \begin{perlmacro}{$runsafely}
% \begin{perlmacro}{@permittedops}
% |$latexprog| is the name of the \LaTeX{} executable
% (e.g.,~``|latex|'').  If |$runsafely| is~|1| (the default), then the
% user's Perl code runs in a secure sandbox; if it's |0|, then arbitrary
% Perl code is allowed to run.  |@permittedops| is a list of features
% made available to the user's Perl code.  Valid values are described in
% Perl's |Opcode| manual page.  \perltex's default is a list containing
% only |:browse|.
%    \begin{macrocode}
my $latexprog;
my $runsafely = 1;
my @permittedops;
%    \end{macrocode}
% \end{perlmacro}
% \end{perlmacro}
% \end{perlmacro}
%
% \paragraph{Other global variables}
% \begin{perlmacro}{$progname}
% \begin{perlmacro}{$jobname}
% \begin{perlmacro}{@latexcmdline}
% \begin{perlmacro}{$toperl}
% \begin{perlmacro}{$fromperl}
% \begin{perlmacro}{$toflag}
% \begin{perlmacro}{$fromflag}
% \begin{perlmacro}{$doneflag}
% \begin{perlmacro}{$logfile}
% \begin{perlmacro}{$sandbox}
% \begin{perlmacro}{$sandbox_eval}
% \begin{perlmacro}{$latexpid}
% |$progname| is the run-time name of the \perltex{} program.  |$jobname|
% is the base name of the user's |.tex| file, which defaults to the
% \TeX{} default of |texput|.  |@latexcmdline| is the command line to
% pass to the \LaTeX{} executable.  |$toperl| defines the filename used
% for \LaTeX{}$\rightarrow$Perl communication.  |$fromperl| defines the
% filename used for Perl$\rightarrow$\LaTeX{} communication.  |$toflag|
% is the name of a file that will exist only after \LaTeX{} creates
% |$tofile|.  |$fromflag| is the name of a file that will exist only
% after Perl creates |$fromfile|.  |$doneflag| is the name of a file
% that will exist only after Perl deletes |$fromflag|.  |$logfile| is the
% name of a log file to which \perltex{} writes verbose execution
% information.  |$sandbox| is a secure sandbox in which to run code that
% appeared in the \LaTeX{} document.  |$sandbox_eval| is a subroutine
% that evalutes a string within |$sandbox| (normally) or outside of all
% sandboxes (if |--nosafe| is specified).  |$latexpid| is the process~ID
% of the |latex| process.
%    \begin{macrocode}
my $progname = basename $0;
my $jobname = "texput";
my @latexcmdline;
my $toperl;
my $fromperl;
my $toflag;
my $fromflag;
my $doneflag;
my $logfile;
my $sandbox = new Safe;
my $sandbox_eval;
my $latexpid;
%    \end{macrocode}
% \end{perlmacro}
% \end{perlmacro}
% \end{perlmacro}
% \end{perlmacro}
% \end{perlmacro}
% \end{perlmacro}
% \end{perlmacro}
% \end{perlmacro}
% \end{perlmacro}
% \end{perlmacro}
% \end{perlmacro}
% \end{perlmacro}
%
% \subsubsection{Command-line conversion}
%
% In this section, \perltex{} parses its own command line and prepares a
% command line to pass to |latex|.
%
% \paragraph{Parsing \perltex's command line}
% We first set |$latexprog| to be the contents of the environment
% variable |PERLTEX| or the value ``|latex|'' if |PERLTEX| is not
% specified.  We then use |Getopt::Long| to parse the command line,
% leaving any parameters we don't recognize in the argument vector
% (|@ARGV|) because these are presumably |latex| options.
%    \begin{macrocode}
$latexprog = $ENV{"PERLTEX"} || "latex";
Getopt::Long::Configure("require_order", "pass_through");
GetOptions("help"     => sub {pod2usage(-verbose => 1)},
           "latex=s"  => \$latexprog,
           "safe!"    => \$runsafely,
           "permit=s" => \@permittedops) || pod2usage(2);
%    \end{macrocode}
%
% \paragraph{Preparing a \LaTeX{} command line}
% \begin{perlmacro}{$firstcmd}
% \begin{perlmacro}{$option}
% We start by searching |@ARGV| for the first string that does not start
% with ``|-|'' or ``|\|''.  This string, which represents a filename, is
% used to set |$jobname|.
%    \begin{macrocode}
@latexcmdline = @ARGV;
my $firstcmd = 0;
for ($firstcmd=0; $firstcmd<=$#latexcmdline; $firstcmd++) {
    my $option = $latexcmdline[$firstcmd];
    next if substr($option, 0, 1) eq "-";
    if (substr ($option, 0, 1) ne "\\") {
        $jobname = basename $option, ".tex" ;
        $latexcmdline[$firstcmd] = "\\input $option";
    }
    last;
}
push @latexcmdline, "" if $#latexcmdline==-1;
%    \end{macrocode}
% \end{perlmacro}
% \end{perlmacro}
%
% \begin{perlmacro}{$separator}
% To avoid conflicts with the code and parameters passed to Perl from
% \LaTeX{} (see Figure~\vref{fig:tofile-define} and
% Figure~\vref{fig:tofile-use}) we define a separator string,
% |$separator|, containing 20 random uppercase letters.
%    \begin{macrocode}
my $separator = "";
foreach (1 .. 20) {
    $separator .= chr(ord("A") + rand(26));
}
%    \end{macrocode}
% \end{perlmacro}
%
% Now that we have the name of the \LaTeX{} job (|$jobname|) we can
% assign |$toperl|, |$fromperl|, |$toflag|, |$fromflag|, |$doneflag|,
% and |$logfile| in terms of |$jobname| plus a suitable extension.
%    \begin{macrocode}
$toperl = $jobname . ".topl";
$fromperl = $jobname . ".frpl";
$toflag = $jobname . ".tfpl";
$fromflag = $jobname . ".ffpl";
$doneflag = $jobname . ".dfpl";
$logfile = $jobname . ".lgpl";
%    \end{macrocode}
%
% We now replace the filename of the |.tex| file passed to \perltex{}
% with a |\def|inition of the separator character, |\def|initions of the
% various files, and the original file with |\input| prepended if
% necessary.
%    \begin{macrocode}
$latexcmdline[$firstcmd] =
    sprintf '\makeatletter' . '\def%s{%s}' x 6 . '\makeatother%s',
    '\plmac@tag', $separator,
    '\plmac@tofile', $toperl,
    '\plmac@fromfile', $fromperl,
    '\plmac@toflag', $toflag,
    '\plmac@fromflag', $fromflag,
    '\plmac@doneflag', $doneflag,
    $latexcmdline[$firstcmd];
%    \end{macrocode}
%
% \subsubsection{Launching \LaTeX}
%
% We start by deleting the |$toperl|, |$fromperl|, |$toflag|,
% |$fromflag|, and |$doneflag| files, in case any of these were left
% over from a previous (aborted) run.  We also create a log file,
% |$logfile|.  As |@latexcmdline| contains the complete command line to
% pass to |latex| we need only |fork| a new process and have the child
% process overlay itself with |latex|.  \perltex{} continues running as
% the parent.
%
% Note that here and elsewhere in \perltex, |unlink| is called
% repeatedly until the file is actually deleted.  This works around a
% race condition that occurs in some filesystems in which file deletions
% are executed somewhat lazily.
% \changes{v1.0a}{2003/08/21}{Made all \texttt{unlink} calls wait for the
%   file to actually disappear}
%    \begin{macrocode}
foreach my $file ($toperl, $fromperl, $toflag, $fromflag, $doneflag) {
    unlink $file while -e $file;
}
open (LOGFILE, ">$logfile") || die "open(\"$logfile\"): $!\n";
%    \end{macrocode}
%    \begin{macrocode}
defined ($latexpid = fork) || die "fork: $!\n";
unshift @latexcmdline, $latexprog;
if (!$latexpid) {
    exec {$latexcmdline[0]} @latexcmdline;
    die "exec('@latexcmdline'): $!\n";
}
%    \end{macrocode}
%
% \subsubsection{Preparing a sandbox}
%
% \perltex{} uses Perl's |Safe| and |Opcode| modules to declare a secure
% sandbox (|$sandbox|) in which to run Perl code passed to it from
% \LaTeX{}\@.  When the sandbox compiles and executes Perl code, it
% permits only operations that are deemed safe.  For example, the Perl
% code is allowed by default to assign variables, call functions, and
% execute loops.  However, it is not normally allowed to delete files,
% kill processes, or invoke other programs.  If \perltex\ is run with
% the |--nosafe| option we bypass the sandbox entirely and execute Perl
% code using an ordinary |eval()| statement.
%    \begin{macrocode}
if ($runsafely) {
    @permittedops=(":browse") if $#permittedops==-1;
    $sandbox->permit_only (@permittedops);
    $sandbox_eval = sub {$sandbox->reval($_[0])};
}
else {
    $sandbox_eval = \&top_level_eval;
}
%    \end{macrocode}
%
% \subsubsection{Communicating with \LaTeX{}}
%
% The following code constitutes \perltex's main loop.  Until |latex|
% exits, the loop repeatedly reads Perl code from \LaTeX{}, evaluates
% it, and returns the result as per the protocol described in
% Figure~\vref{fig:comm-protocol}.
% \changes{v1.0a}{2003/08/21}{Undefined \texttt{\string\string\string$/}
%   only locally}
%
%    \begin{macrocode}
while (1) {
%    \end{macrocode}
%
% \begin{perlmacro}{$awaitexists}
% We define a local subroutine |$awaitexists| which waits for a given
% file to exist.  If |latex| exits while |$awaitexists| is waiting, then
% \perltex{} cleans up and exits, too.
% \changes{v1.0a}{2003/08/06}{Bug fix: Added ``\texttt{undef
%   \$latexpid}'' to make the \texttt{END} block correctly return a
%   status code of~0 on success}
%    \begin{macrocode}
    my $awaitexists = sub {
      while (!-e $_[0]) {
          sleep 0;
          if (waitpid($latexpid, &WNOHANG)==-1) {
              foreach my $file ($toperl, $fromperl, $toflag,
                                $fromflag, $doneflag) {
                  unlink $file while -e $file;
              }
              undef $latexpid;
              exit 0;
          }
      }
    };
%    \end{macrocode}
% \end{perlmacro}
%
% \begin{perlmacro}{$entirefile}
% Wait for |$toflag| to exist.  When it does, this implies that
% |$toperl| must exist as well.  We read the entire contents of
% |$toperl| into the |$entirefile| variable and process it.
% Figures~\ref{fig:tofile-define} and~\ref{fig:tofile-use} illustrate
% the contents of |$toperl|.
%    \begin{macrocode}
    $awaitexists->($toflag);
    my $entirefile;
    {
        local $/ = undef;
        open (TOPERL, "<$toperl") || die "open($toperl): $!\n";
        $entirefile = <TOPERL>;
        close TOPERL;
    }
%    \end{macrocode}
% \end{perlmacro}
%
% \begin{perlmacro}{$optag}
% \begin{perlmacro}{$macroname}
% \begin{perlmacro}{@otherstuff}
% We split the contents of |$entirefile| into an operation tag (either
% |DEF|, |USE|, or |RUN|), the macro name, and everything else
% (|@otherstuff|).  If |$optag| is |DEF| then |@otherstuff| will contain
% the Perl code to define.  If |$optag| is |USE| then |@otherstuff| will
% be a list of subroutine arguments.  If |$optag| is |RUN| then
% |@otherstuff| will be a block of Perl code to run.
%    \begin{macrocode}
    my ($optag, $macroname, @otherstuff) =
        map {chomp; $_} split "$separator\n", $entirefile;
%    \end{macrocode}
% \end{perlmacro}
% \end{perlmacro}
% \end{perlmacro}
%
% We clean up the macro name by deleting all leading non-letters,
% replacing all subsequent non-alphanumerics with ``|_|'', and
% prepending ``|latex_|'' to the macro name.
%    \begin{macrocode}
    $macroname =~ s/^[^A-Za-z]+//;
    $macroname =~ s/\W/_/g;
    $macroname = "latex_" . $macroname;
%    \end{macrocode}
%
% If we're calling a subroutine, then we make the arguments more
% palatable to Perl by single-quoting them and replacing every
% occurrence of ``|\|'' with ``|\\|'' and every occurrence of ``|'|''
% with ``|\'|''.
%    \begin{macrocode}
    if ($optag eq "USE") {
      foreach (@otherstuff) {
          s/\\/\\\\/g;
          s/\'/\\\'/g;
          $_ = "'$_'";
      }
    }
%    \end{macrocode}
%
% \begin{perlmacro}{$perlcode}
% There are three possible values that can be assigned to |$perlcode|.
% If |$optag| is |DEF|, then |$perlcode| is made to contain a definition
% of the user's subroutine, named |$macroname|.  If |$optag| is |USE|,
% then |$perlcode| becomes an invocation of |$macroname| which gets
% passed all of the macro arguments.  Finally, if |$optag| is |RUN|,
% then |$perlcode| is the unmodified Perl code passed to us from
% \perlmac.  Figure~\ref{fig:latex-perl-define} presents an example
% of how the following code converts a \PerlTeX{} macro definition into
% a Perl subroutine definition and Figure~\ref{fig:latex-perl-use}
% presents an example of how the following code converts a \PerlTeX{}
% macro invocation into a Perl subroutine invocation.
%
% \begin{figure}[tbp]
%   \centering
%   \DeleteShortVerb\|
%   \begin{tabular}{l|l|}
%     \cline{2-2}
%     \LaTeX: & \verb|\perlnewcommand{\mymacro}[2]{%| \\
%             & \verb|  sprintf "Isn't $_[0] %s $_[1]?\n",| \\
%             & \verb|    $_[0]>=$_[1] ? ">=" : "<"| \\
%             & \verb|}| \\
%     \cline{2-2}
%     \multicolumn{2}{c}{} \\
%     \multicolumn{1}{c}{} & \multicolumn{1}{c}{\Huge$\Downarrow$} \\
%     \multicolumn{2}{c}{} \\
%     \cline{2-2}
%     Perl:   & \verb|sub latex_mymacro {| \\
%             & \verb|  sprintf "Isn't $_[0] %s $_[1]?\n",| \\
%             & \verb|    $_[0]>=$_[1] ? ">=" : "<"| \\
%             & \verb|}| \\
%     \cline{2-2}
%   \end{tabular}
%   \MakeShortVerb\|
%   \caption{Conversion from \LaTeX{} to Perl (subroutine definition)}
%   \label{fig:latex-perl-define}
% \end{figure}
%
% \begin{figure}[tbp]
%   \centering
%   \DeleteShortVerb\|
%   \begin{tabular}{l|l|}
%     \cline{2-2}
%     \LaTeX: & \verb|\mymacro{12}{34}| \\
%     \cline{2-2}
%     \multicolumn{2}{c}{} \\
%     \multicolumn{1}{c}{} & \multicolumn{1}{c}{\Huge$\Downarrow$} \\
%     \multicolumn{2}{c}{} \\
%     \cline{2-2}
%     Perl:   & \verb|latex_mymacro ('12', '34');| \\
%     \cline{2-2}
%   \end{tabular}
%   \MakeShortVerb\|
%   \caption{Conversion from \LaTeX{} to Perl (subroutine invocation)}
%   \label{fig:latex-perl-use}
% \end{figure}
%
%    \begin{macrocode}
    my $perlcode;
    if ($optag eq "DEF") {
        $perlcode =
            sprintf "sub %s {%s}\n",
            $macroname, $otherstuff[0];
    }
    elsif ($optag eq "USE") {
        $perlcode = sprintf "%s (%s);\n", $macroname, join(", ", @otherstuff);
    }
    elsif ($optag eq "RUN") {
        $perlcode = $otherstuff[0];
    }
    else {
        die "${progname}: Internal error -- unexpected operation tag \"$optag\"\n";
    }
%    \end{macrocode}
% \end{perlmacro}
%
% Log what we're about to evaluate.
%    \begin{macrocode}
    print LOGFILE "#" x 31, " PERL CODE ", "#" x 32, "\n";
    print LOGFILE $perlcode, "\n";
%    \end{macrocode}
%
% \begin{perlmacro}{$result}
% \begin{perlmacro}{$msg}
% We're now ready to execute the user's code using the |$sandbox_eval|
% function.  If a warning occurs we write it as a Perl comment to the
% log file.  If an error occurs (i.e.,~|$@| is defined) we replace the
% result (|$result|) with a call to \LaTeXe's |\PackageError| macro to
% return a suitable error message.  We produce one error message for
% sandbox policy violations (detected by the error message, |$@|,
% containing the string ``\texttt{trapped by}'') and a different error
% message for all other errors caused by executing the user's code.  For
% clarity of reading both warning and error messages, we elide the
% string ``\texttt{at (eval} \meta{number}\texttt{) line}
% \meta{number}''.
%    \begin{macrocode}
    undef $_;
    my $result;
    {
        my $warningmsg;
        local $SIG{__WARN__} =
            sub {chomp ($warningmsg=$_[0]); return 0};
        $result = $sandbox_eval->($perlcode);
        if (defined $warningmsg) {
            $warningmsg =~ s/at \(eval \d+\) line \d+\W+//;
            print LOGFILE "# ===> $warningmsg\n\n";
        }
    }
    $result="" if !$result || $optag eq "RUN";
    if ($@) {
        my $msg = $@;
        $msg =~ s/at \(eval \d+\) line \d+\W+//;
        $msg =~ s/\s+/ /;
        $result = "\\PackageError{perltex}{$msg}";
        my @helpstring;
        if ($msg =~ /\btrapped by\b/) {
            @helpstring =
                ("The preceding error message comes from Perl.  Apparently,",
                 "the Perl code you tried to execute attempted to perform an",
                 "`unsafe' operation.  If you trust the Perl code (e.g., if",
                 "you wrote it) then you can invoke perltex with the --nosafe",
                 "option to allow arbitrary Perl code to execute.",
                 "Alternatively, you can selectively enable Perl features",
                 "using perltex's --permit option.  Don't do this if you don't",
                 "trust the Perl code, however; malicious Perl code can do a",
                 "world of harm to your computer system.");
        }
        else {
            @helpstring =
              ("The preceding error message comes from Perl.  Apparently,",
               "there's a bug in your Perl code.  You'll need to sort that",
               "out in your document and re-run perltex.");
        }
        my $helpstring = join ("\\MessageBreak\n", @helpstring);
        $helpstring =~ s/\.  /.\\space\\space /g;
        $result .= "{$helpstring}";
    }
%    \end{macrocode}
% \end{perlmacro}
% \end{perlmacro}
%
% Log the resulting \LaTeX{} code.
%    \begin{macrocode}
    print LOGFILE "%" x 30, " LATEX RESULT ", "%" x 30, "\n";
    print LOGFILE $result, "\n\n";
%    \end{macrocode}
%
% We add |\endinput| to the generated \LaTeX{} code to suppress an
% extraneous end-of-line character that \TeX{} would otherwise insert.
%    \begin{macrocode}
    $result .= '\endinput';
%    \end{macrocode}
%
% Continuing the protocol described in Figure~\vref{fig:comm-protocol}
% we now write |$result| (which contains either the result of executing
% the user's or a |\PackageError|) to the |$fromperl| file, delete
% |$toflag|, |$toperl|, and |$doneflag|, and notify \LaTeX{} by touching
% the |$fromflag| file.
%    \begin{macrocode}
    open (FROMPERL, ">$fromperl") || die "open($fromperl): $!\n";
    syswrite FROMPERL, $result;
    close FROMPERL;
%    \end{macrocode}
%    \begin{macrocode}
    unlink $toflag while -e $toflag;
    unlink $toperl while -e $toperl;
    unlink $doneflag while -e $doneflag;
%    \end{macrocode}
%    \begin{macrocode}
    open (FROMFLAG, ">$fromflag") || die "open($fromflag): $!\n";
    close FROMFLAG;
%    \end{macrocode}
%
% We have to perform one final \LaTeX-to-Perl synchronization step.
% Otherwise, a subsequent |\perl|[|re|]|newcommand| would see that
% |$fromflag| already exists and race ahead, finding that |$fromperl|
% does not contain what it's supposed to.
%    \begin{macrocode}
    $awaitexists->($toperl);
    unlink $fromflag while -e $fromflag;
    open (DONEFLAG, ">$doneflag") || die "open($doneflag): $!\n";
    close DONEFLAG;
}
%    \end{macrocode}
%
% \subsubsection{Final cleanup}
%
% If we exit abnormally we should do our best to kill the child |latex|
% process so that it doesn't continue running forever, holding onto
% system resources.
%    \begin{macrocode}
END {
    close LOGFILE;
    if (defined $latexpid) {
        kill (9, $latexpid);
        exit 1;
    }
    exit 0;
}

__END__
%    \end{macrocode}
%
% \subsubsection{\perltex{} POD documentation}
%
% \perltex{} includes documentation in Perl's POD (Plain Old
% Documentation) format.  This is used both to produce manual pages and
% to provide usage information when \perltex{} is invoked with the
% |--help| option.  The POD documentation is not listed here as part of
% the documented \perltex{} source code because it contains essentially
% the same information as that shown in Section~\ref{sec:perltex-man}.
% If you're curious what the POD source looks like then see the
% generated \perltex{} file.
%
% \iffalse
%    \begin{macrocode}

=head1 NAME

perltex - enable LaTeX macros to be defined in terms of Perl code


=head1 SYNOPSIS

perltex
[B<--help>]
[B<--latex>=I<program>]
[B<-->[B<no>]B<safe>]
[B<--permit>=I<feature>]
[I<latex options>]


=head1 DESCRIPTION

LaTeX -- through the underlying TeX typesetting system -- produces
beautifully typeset documents but has a macro language that is
difficult to program.  In particular, support for complex string
manipulation is largely lacking.  Perl is a popular general-purpose
programming language whose forte is string manipulation.  However, it
has no typesetting capabilities whatsoever.

Clearly, Perl's programmability could complement LaTeX's typesetting
strengths.  B<perltex> is the tool that enables a symbiosis between
the two systems.  All a user needs to do is compile a LaTeX document
using B<perltex> instead of B<latex>.  (B<perltex> is actually a
wrapper for B<latex>, so no B<latex> functionality is lost.)  If the
document includes a C<\usepackage{perltex}> in its preamble, then
C<\perlnewcommand> and C<\perlrenewcommand> macros will be made
available.  These behave just like LaTeX's C<\newcommand> and
C<\renewcommand> except that the macro body contains Perl code instead
of LaTeX code.


=head1 OPTIONS

B<perltex> accepts the following command-line options:

=over 4

=item B<--help>

Display basic usage information.

=item B<--latex>=I<program>

Specify a program to use instead of B<latex>.  For example,
C<--latex=pdflatex> would typeset the given document using
B<pdflatex> instead of ordinary B<latex>.

=item B<-->[B<no>]B<safe>

Enable or disable sandboxing.  With the default of C<--safe>,
B<perltex> executes the code from a C<\perlnewcommand> or
C<\perlrenewcommand> macro within a protected environment that
prohibits ``unsafe'' operations such as accessing files or executing
external programs.  Specifying C<--nosafe> gives the LaTeX document
I<carte blanche> to execute any arbitrary Perl code, including that
which can harm the user's files.  See L<Safe> for more information.

=item B<--permit>=I<feature>

Permit particular Perl operations to be performed.  The C<--permit>
option, which can be specified more than once on the command line,
enables finer-grained control over the B<perltex> sandbox.  See
L<Opcode> for more information.

=back

These options are then followed by whatever options are normally
passed to B<latex> (or whatever program was specified with
C<--latex>), including, for instance, the name of the F<.tex> file to
compile.


=head1 EXAMPLES

In its simplest form, B<perltex> is run just like B<latex>:

    perltex myfile.tex

To use B<pdflatex> instead of regular B<latex>, use the C<--latex>
option:

    perltex --latex=pdflatex myfile.tex

If LaTeX gives a ``C<trapped by operation mask>'' error and you trust
the F<.tex> file you're trying to compile not to execute malicious
Perl code (e.g., because you wrote it yourself), you can disable
B<perltex>'s safety mechansisms with C<--nosafe>:

    perltex --nosafe myfile.tex

The following command gives documents only B<perltex>'s default
permissions (C<:browse>) plus the ability to open files and invoke the
C<time> command:

    perltex --permit=:browse --permit=:filesys_open
      --permit=time myfile.tex


=head1 ENVIRONMENT

B<perltex> honors the following environment variables:

=over 4

=item PERLTEX

Specify the filename of the LaTeX compiler.  The LaTeX compiler
defaults to ``C<latex>''.  The C<PERLTEX> environment variable
overrides this default, and the C<--latex> command-line option (see
L</OPTIONS>) overrides that.

=back


=head1 FILES

While compiling F<jobname.tex>, B<perltex> makes use of the following
files:

=over 4

=item F<jobname.lgpl>

log file written by Perl; helpful for debugging Perl macros

=item F<jobname.topl>

information sent from LaTeX to Perl

=item F<jobname.frpl>

information sent from Perl to LaTeX

=item F<jobname.tfpl>

``flag'' file whose existence indicates that F<jobname.topl> contains
valid data

=item F<jobname.ffpl>

``flag'' file whose existence indicates that F<jobname.frpl> contains
valid data

=item F<jobname.dfpl>

``flag'' file whose existence indicates that F<jobname.ffpl> has been
deleted

=back


=head1 NOTES

B<perltex>'s sandbox defaults to what L<Opcode> calls ``C<:browse>''.


=head1 SEE ALSO

latex(1), pdflatex(1), perl(1), Safe(3pm), Opcode(3pm)


=head1 AUTHOR

Scott Pakin, I<scott+pt@pakin.org>
%    \end{macrocode}
% \fi
%
% \iffalse
%</perltex>
% \fi
%
% ^^A  Keep track of the number of lines of code in perltex.
% \makeatletter
% \immediate\write\@auxout{^^A
%   \noexpand\gdef\noexpand\perltexlines{\the\c@CodelineNo}}
% \makeatother
%
%
% \subsection{Porting to other languages}
% \label{sec:porting}
%
% Perl is a natural choice for a \LaTeX{} macro language because of its
% excellent support for text manipulation including extended regular
% expressions, string interpolation, and ``here'' strings, to name a few
% nice features.  However, Perl's syntax is unusual and its semantics
% are rife with annoying special cases.  Some users will therefore long
% for a \meta{some-language-other-than-Perl}\TeX\@.  Fortunately,
% porting \PerlTeX{} to use a different language should be fairly
% straightforward.  \perltex{} will need to be rewritten in the target
% language, of course, but \perlmac{} modifications will likely be
% fairly minimal.  In all probability, only the following changes will
% need to be made:
%
% \begin{itemize}
%   \item Rename \perlmac{} and \perltex{} (and choose a package name
%   other than ``\PerlTeX'') as per the \PerlTeX{} license agreement
%   (Section~\ref{sec:license}).
%
%   \item In your replacement for \perlmac, replace all occurrences of
%   ``|plmac|'' with a different string.
%
%   \item In your replacement for \perltex, choose different file
%   extensions for the various helper files.
% \end{itemize}
%
% The importance of these changes is that they help ensure version
% consistency and that they make it possible to run
% \meta{some-language-other-than-Perl}\TeX{} alongside \PerlTeX,
% enabling multiple programming languages to be utilized in the same
% \LaTeX{} document.
%
% \Finale
\endinput