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
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
|
# B::Deparse.pm
# Copyright (c) 1998-2000, 2002, 2003, 2004, 2005, 2006 Stephen McCamant.
# All rights reserved.
# This module is free software; you can redistribute and/or modify
# it under the same terms as Perl itself.
# This is based on the module of the same name by Malcolm Beattie,
# but essentially none of his code remains.
package B::Deparse;
use Carp;
use B qw(class main_root main_start main_cv svref_2object opnumber perlstring
OPf_WANT OPf_WANT_VOID OPf_WANT_SCALAR OPf_WANT_LIST
OPf_KIDS OPf_REF OPf_STACKED OPf_SPECIAL OPf_MOD OPf_PARENS
OPpLVAL_INTRO OPpOUR_INTRO OPpENTERSUB_AMPER OPpSLICE OPpKVSLICE
OPpCONST_BARE
OPpTRANS_SQUASH OPpTRANS_DELETE OPpTRANS_COMPLEMENT OPpTARGET_MY
OPpEXISTS_SUB OPpSORT_NUMERIC OPpSORT_INTEGER OPpREPEAT_DOLIST
OPpSORT_REVERSE OPpMULTIDEREF_EXISTS OPpMULTIDEREF_DELETE
OPpSPLIT_ASSIGN OPpSPLIT_LEX
OPpPADHV_ISKEYS OPpRV2HV_ISKEYS
OPpCONCAT_NESTED
OPpMULTICONCAT_APPEND OPpMULTICONCAT_STRINGIFY OPpMULTICONCAT_FAKE
OPpTRUEBOOL OPpINDEX_BOOLNEG
SVf_IOK SVf_NOK SVf_ROK SVf_POK SVpad_OUR SVf_FAKE SVs_RMG SVs_SMG
SVs_PADTMP SVpad_TYPED
CVf_METHOD CVf_LVALUE
PMf_KEEP PMf_GLOBAL PMf_CONTINUE PMf_EVAL PMf_ONCE
PMf_MULTILINE PMf_SINGLELINE PMf_FOLD PMf_EXTENDED PMf_EXTENDED_MORE
PADNAMEt_OUTER
MDEREF_reload
MDEREF_AV_pop_rv2av_aelem
MDEREF_AV_gvsv_vivify_rv2av_aelem
MDEREF_AV_padsv_vivify_rv2av_aelem
MDEREF_AV_vivify_rv2av_aelem
MDEREF_AV_padav_aelem
MDEREF_AV_gvav_aelem
MDEREF_HV_pop_rv2hv_helem
MDEREF_HV_gvsv_vivify_rv2hv_helem
MDEREF_HV_padsv_vivify_rv2hv_helem
MDEREF_HV_vivify_rv2hv_helem
MDEREF_HV_padhv_helem
MDEREF_HV_gvhv_helem
MDEREF_ACTION_MASK
MDEREF_INDEX_none
MDEREF_INDEX_const
MDEREF_INDEX_padsv
MDEREF_INDEX_gvsv
MDEREF_INDEX_MASK
MDEREF_FLAG_last
MDEREF_MASK
MDEREF_SHIFT
);
$VERSION = '1.54';
use strict;
our $AUTOLOAD;
use warnings ();
require feature;
use Config;
BEGIN {
# List version-specific constants here.
# Easiest way to keep this code portable between version looks to
# be to fake up a dummy constant that will never actually be true.
foreach (qw(OPpSORT_INPLACE OPpSORT_DESCEND OPpITER_REVERSED OPpCONST_NOVER
OPpPAD_STATE PMf_SKIPWHITE RXf_SKIPWHITE
PMf_CHARSET PMf_KEEPCOPY PMf_NOCAPTURE CVf_ANONCONST
CVf_LOCKED OPpREVERSE_INPLACE OPpSUBSTR_REPL_FIRST
PMf_NONDESTRUCT OPpEVAL_BYTES
OPpLVREF_TYPE OPpLVREF_SV OPpLVREF_AV OPpLVREF_HV
OPpLVREF_CV OPpLVREF_ELEM SVpad_STATE)) {
eval { B->import($_) };
no strict 'refs';
*{$_} = sub () {0} unless *{$_}{CODE};
}
}
# Todo:
# (See also BUGS section at the end of this file)
#
# - finish tr/// changes
# - add option for even more parens (generalize \&foo change)
# - left/right context
# - copy comments (look at real text with $^P?)
# - avoid semis in one-statement blocks
# - associativity of &&=, ||=, ?:
# - ',' => '=>' (auto-unquote?)
# - break long lines ("\r" as discretionary break?)
# - configurable syntax highlighting: ANSI color, HTML, TeX, etc.
# - more style options: brace style, hex vs. octal, quotes, ...
# - print big ints as hex/octal instead of decimal (heuristic?)
# - handle 'my $x if 0'?
# - version using op_next instead of op_first/sibling?
# - avoid string copies (pass arrays, one big join?)
# - here-docs?
# Current test.deparse failures
# comp/hints 6 - location of BEGIN blocks wrt. block openings
# run/switchI 1 - missing -I switches entirely
# perl -Ifoo -e 'print @INC'
# op/caller 2 - warning mask propagates backwards before warnings::register
# 'use warnings; BEGIN {${^WARNING_BITS} eq "U"x12;} use warnings::register'
# op/getpid 2 - can't assign to shared my() declaration (threads only)
# 'my $x : shared = 5'
# op/override 7 - parens on overridden require change v-string interpretation
# 'BEGIN{*CORE::GLOBAL::require=sub {}} require v5.6'
# c.f. 'BEGIN { *f = sub {0} }; f 2'
# op/pat 774 - losing Unicode-ness of Latin1-only strings
# 'use charnames ":short"; $x="\N{latin:a with acute}"'
# op/recurse 12 - missing parens on recursive call makes it look like method
# 'sub f { f($x) }'
# op/subst 90 - inconsistent handling of utf8 under "use utf8"
# op/taint 29 - "use re 'taint'" deparsed in the wrong place wrt. block open
# op/tiehandle compile - "use strict" deparsed in the wrong place
# uni/tr_ several
# ext/B/t/xref 11 - line numbers when we add newlines to one-line subs
# ext/Data/Dumper/t/dumper compile
# ext/DB_file/several
# ext/Encode/several
# ext/Ernno/Errno warnings
# ext/IO/lib/IO/t/io_sel 23
# ext/PerlIO/t/encoding compile
# ext/POSIX/t/posix 6
# ext/Socket/Socket 8
# ext/Storable/t/croak compile
# lib/Attribute/Handlers/t/multi compile
# lib/bignum/ several
# lib/charnames 35
# lib/constant 32
# lib/English 40
# lib/ExtUtils/t/bytes 4
# lib/File/DosGlob compile
# lib/Filter/Simple/t/data 1
# lib/Math/BigInt/t/constant 1
# lib/Net/t/config Deparse-warning
# lib/overload compile
# lib/Switch/ several
# lib/Symbol 4
# lib/Test/Simple several
# lib/Term/Complete
# lib/Tie/File/t/29_downcopy 5
# lib/vars 22
# Object fields:
#
# in_coderef2text:
# True when deparsing via $deparse->coderef2text; false when deparsing the
# main program.
#
# avoid_local:
# (local($a), local($b)) and local($a, $b) have the same internal
# representation but the short form looks better. We notice we can
# use a large-scale local when checking the list, but need to prevent
# individual locals too. This hash holds the addresses of OPs that
# have already had their local-ness accounted for. The same thing
# is done with my().
#
# curcv:
# CV for current sub (or main program) being deparsed
#
# curcvlex:
# Cached hash of lexical variables for curcv: keys are
# names prefixed with "m" or "o" (representing my/our), and
# each value is an array with two elements indicating the cop_seq
# of scopes in which a var of that name is valid and a third ele-
# ment referencing the pad name.
#
# curcop:
# COP for statement being deparsed
#
# curstash:
# name of the current package for deparsed code
#
# subs_todo:
# array of [cop_seq, CV, is_format?, name] for subs and formats we still
# want to deparse. The fourth element is a pad name thingy for lexical
# subs or a string for special blocks. For other subs, it is undef. For
# lexical subs, CV may be undef, indicating a stub declaration.
#
# protos_todo:
# as above, but [name, prototype] for subs that never got a GV
#
# subs_done, forms_done:
# keys are addresses of GVs for subs and formats we've already
# deparsed (or at least put into subs_todo)
#
# subs_declared
# keys are names of subs for which we've printed declarations.
# That means we can omit parentheses from the arguments. It also means we
# need to put CORE:: on core functions of the same name.
#
# in_subst_repl
# True when deparsing the replacement part of a substitution.
#
# in_refgen
# True when deparsing the argument to \.
#
# parens: -p
# linenums: -l
# unquote: -q
# cuddle: ' ' or '\n', depending on -sC
# indent_size: -si
# use_tabs: -sT
# ex_const: -sv
# A little explanation of how precedence contexts and associativity
# work:
#
# deparse() calls each per-op subroutine with an argument $cx (short
# for context, but not the same as the cx* in the perl core), which is
# a number describing the op's parents in terms of precedence, whether
# they're inside an expression or at statement level, etc. (see
# chart below). When ops with children call deparse on them, they pass
# along their precedence. Fractional values are used to implement
# associativity ('($x + $y) + $z' => '$x + $y + $y') and related
# parentheses hacks. The major disadvantage of this scheme is that
# it doesn't know about right sides and left sides, so say if you
# assign a listop to a variable, it can't tell it's allowed to leave
# the parens off the listop.
# Precedences:
# 26 [TODO] inside interpolation context ("")
# 25 left terms and list operators (leftward)
# 24 left ->
# 23 nonassoc ++ --
# 22 right **
# 21 right ! ~ \ and unary + and -
# 20 left =~ !~
# 19 left * / % x
# 18 left + - .
# 17 left << >>
# 16 nonassoc named unary operators
# 15 nonassoc < > <= >= lt gt le ge
# 14 nonassoc == != <=> eq ne cmp
# 13 left &
# 12 left | ^
# 11 left &&
# 10 left ||
# 9 nonassoc .. ...
# 8 right ?:
# 7 right = += -= *= etc.
# 6 left , =>
# 5 nonassoc list operators (rightward)
# 4 right not
# 3 left and
# 2 left or xor
# 1 statement modifiers
# 0.5 statements, but still print scopes as do { ... }
# 0 statement level
# -1 format body
# Nonprinting characters with special meaning:
# \cS - steal parens (see maybe_parens_unop)
# \n - newline and indent
# \t - increase indent
# \b - decrease indent ('outdent')
# \f - flush left (no indent)
# \cK - kill following semicolon, if any
# Semicolon handling:
# - Individual statements are not deparsed with trailing semicolons.
# (If necessary, \cK is tacked on to the end.)
# - Whatever code joins statements together or emits them (lineseq,
# scopeop, deparse_root) is responsible for adding semicolons where
# necessary.
# - use statements are deparsed with trailing semicolons because they are
# immediately concatenated with the following statement.
# - indent() removes semicolons wherever it sees \cK.
BEGIN { for (qw[ const stringify rv2sv list glob pushmark null aelem
kvaslice kvhslice padsv argcheck
nextstate dbstate rv2av rv2hv helem custom ]) {
eval "sub OP_\U$_ () { " . opnumber($_) . "}"
}}
# _pessimise_walk(): recursively walk the optree of a sub,
# possibly undoing optimisations along the way.
sub DEBUG { 0 }
sub _pessimise_walk {
my ($self, $startop) = @_;
return unless $$startop;
my ($op, $prevop);
for ($op = $startop; $$op; $prevop = $op, $op = $op->sibling) {
my $ppname = $op->name;
# pessimisations start here
if ($ppname eq "padrange") {
# remove PADRANGE:
# the original optimisation either (1) changed this:
# pushmark -> (various pad and list and null ops) -> the_rest
# or (2), for the = @_ case, changed this:
# pushmark -> gv[_] -> rv2av -> (pad stuff) -> the_rest
# into this:
# padrange ----------------------------------------> the_rest
# so we just need to convert the padrange back into a
# pushmark, and in case (1), set its op_next to op_sibling,
# which is the head of the original chain of optimised-away
# pad ops, or for (2), set it to sibling->first, which is
# the original gv[_].
$B::overlay->{$$op} = {
type => OP_PUSHMARK,
name => 'pushmark',
private => ($op->private & OPpLVAL_INTRO),
};
}
# pessimisations end here
if (class($op) eq 'PMOP') {
if (ref($op->pmreplroot)
&& ${$op->pmreplroot}
&& $op->pmreplroot->isa( 'B::OP' ))
{
$self-> _pessimise_walk($op->pmreplroot);
}
# pessimise any /(?{...})/ code blocks
my ($re, $cv);
my $code_list = $op->code_list;
if ($$code_list) {
$self->_pessimise_walk($code_list);
}
elsif (${$re = $op->pmregexp} && ${$cv = $re->qr_anoncv}) {
$code_list = $cv->ROOT # leavesub
->first # qr
->code_list; # list
$self->_pessimise_walk($code_list);
}
}
if ($op->flags & OPf_KIDS) {
$self-> _pessimise_walk($op->first);
}
}
}
# _pessimise_walk_exe(): recursively walk the op_next chain of a sub,
# possibly undoing optimisations along the way.
sub _pessimise_walk_exe {
my ($self, $startop, $visited) = @_;
no warnings 'recursion';
return unless $$startop;
return if $visited->{$$startop};
my ($op, $prevop);
for ($op = $startop; $$op; $prevop = $op, $op = $op->next) {
last if $visited->{$$op};
$visited->{$$op} = 1;
my $ppname = $op->name;
if ($ppname =~
/^((and|d?or)(assign)?|(map|grep)while|range|cond_expr|once)$/
# entertry is also a logop, but its op_other invariably points
# into the same chain as the main execution path, so we skip it
) {
$self->_pessimise_walk_exe($op->other, $visited);
}
elsif ($ppname eq "subst") {
$self->_pessimise_walk_exe($op->pmreplstart, $visited);
}
elsif ($ppname =~ /^(enter(loop|iter))$/) {
# redoop and nextop will already be covered by the main block
# of the loop
$self->_pessimise_walk_exe($op->lastop, $visited);
}
# pessimisations start here
}
}
# Go through an optree and "remove" some optimisations by using an
# overlay to selectively modify or un-null some ops. Deparsing in the
# absence of those optimisations is then easier.
#
# Note that older optimisations are not removed, as Deparse was already
# written to recognise them before the pessimise/overlay system was added.
sub pessimise {
my ($self, $root, $start) = @_;
no warnings 'recursion';
# walk tree in root-to-branch order
$self->_pessimise_walk($root);
my %visited;
# walk tree in execution order
$self->_pessimise_walk_exe($start, \%visited);
}
sub null {
my $op = shift;
return class($op) eq "NULL";
}
# Add a CV to the list of subs that still need deparsing.
sub todo {
my $self = shift;
my($cv, $is_form, $name) = @_;
my $cvfile = $cv->FILE//'';
return unless ($cvfile eq $0 || exists $self->{files}{$cvfile});
my $seq;
if ($cv->OUTSIDE_SEQ) {
$seq = $cv->OUTSIDE_SEQ;
} elsif (!null($cv->START) and is_state($cv->START)) {
$seq = $cv->START->cop_seq;
} else {
$seq = 0;
}
my $stash = $cv->STASH;
if (class($stash) eq 'HV') {
$self->{packs}{$stash->NAME}++;
}
push @{$self->{'subs_todo'}}, [$seq, $cv, $is_form, $name];
}
# Pop the next sub from the todo list and deparse it
sub next_todo {
my $self = shift;
my $ent = shift @{$self->{'subs_todo'}};
my ($seq, $cv, $is_form, $name) = @$ent;
# any 'use strict; package foo' that should come before the sub
# declaration to sync with the first COP of the sub
my $pragmata = '';
if ($cv and !null($cv->START) and is_state($cv->START)) {
$pragmata = $self->pragmata($cv->START);
}
if (ref $name) { # lexical sub
# emit the sub.
my @text;
my $flags = $name->FLAGS;
push @text,
!$cv || $seq <= $name->COP_SEQ_RANGE_LOW
? $self->keyword($flags & SVpad_OUR
? "our"
: $flags & SVpad_STATE
? "state"
: "my") . " "
: "";
# XXX We would do $self->keyword("sub"), but ‘my CORE::sub’
# doesn’t work and ‘my sub’ ignores a &sub in scope. I.e.,
# we have a core bug here.
push @text, "sub " . substr $name->PVX, 1;
if ($cv) {
# my sub foo { }
push @text, " " . $self->deparse_sub($cv);
$text[-1] =~ s/ ;$/;/;
}
else {
# my sub foo;
push @text, ";\n";
}
return $pragmata . join "", @text;
}
my $gv = $cv->GV;
$name //= $self->gv_name($gv);
if ($is_form) {
return $pragmata . $self->keyword("format") . " $name =\n"
. $self->deparse_format($cv). "\n";
} else {
my $use_dec;
if ($name eq "BEGIN") {
$use_dec = $self->begin_is_use($cv);
if (defined ($use_dec) and $self->{'expand'} < 5) {
return $pragmata if 0 == length($use_dec);
# XXX bit of a hack: Test::More's use_ok() method
# builds a fake use statement which deparses as, e.g.
# use Net::Ping (@{$args[0];});
# As well as being superfluous (the use_ok() is deparsed
# too) and ugly, it fails under use strict and otherwise
# makes use of a lexical var that's not in scope.
# So strip it out.
return $pragmata
if $use_dec =~
m/
\A
use \s \S+ \s \(\@\{
(
\s*\#line\ \d+\ \".*"\s*
)?
\$args\[0\];\}\);
\n
\Z
/x;
$use_dec =~ s/^(use|no)\b/$self->keyword($1)/e;
}
}
my $l = '';
if ($self->{'linenums'}) {
my $line = $gv->LINE;
my $file = $gv->FILE;
$l = "\n\f#line $line \"$file\"\n";
}
my $p = '';
my $stash;
if (class($cv->STASH) ne "SPECIAL") {
$stash = $cv->STASH->NAME;
if ($stash ne $self->{'curstash'}) {
$p = $self->keyword("package") . " $stash;\n";
$name = "$self->{'curstash'}::$name" unless $name =~ /::/;
$self->{'curstash'} = $stash;
}
}
if ($use_dec) {
return "$pragmata$p$l$use_dec";
}
if ( $name !~ /::/ and $self->lex_in_scope("&$name")
|| $self->lex_in_scope("&$name", 1) )
{
$name = "$self->{'curstash'}::$name";
} elsif (defined $stash) {
$name =~ s/^\Q$stash\E::(?!\z|.*::)//;
}
my $ret = "$pragmata${p}${l}" . $self->keyword("sub") . " $name "
. $self->deparse_sub($cv);
$self->{'subs_declared'}{$name} = 1;
return $ret;
}
}
# Return a "use" declaration for this BEGIN block, if appropriate
sub begin_is_use {
my ($self, $cv) = @_;
my $root = $cv->ROOT;
local @$self{qw'curcv curcvlex'} = ($cv);
local $B::overlay = {};
$self->pessimise($root, $cv->START);
#require B::Debug;
#B::walkoptree($cv->ROOT, "debug");
my $lineseq = $root->first;
return if $lineseq->name ne "lineseq";
my $req_op = $lineseq->first->sibling;
return if $req_op->name ne "require";
# maybe it's C<require expr> rather than C<require 'foo'>
return if ($req_op->first->name ne 'const');
my $module;
if ($req_op->first->private & OPpCONST_BARE) {
# Actually it should always be a bareword
$module = $self->const_sv($req_op->first)->PV;
$module =~ s[/][::]g;
$module =~ s/.pm$//;
}
else {
$module = $self->const($self->const_sv($req_op->first), 6);
}
my $version;
my $version_op = $req_op->sibling;
return if class($version_op) eq "NULL";
if ($version_op->name eq "lineseq") {
# We have a version parameter; skip nextstate & pushmark
my $constop = $version_op->first->next->next;
return unless $self->const_sv($constop)->PV eq $module;
$constop = $constop->sibling;
$version = $self->const_sv($constop);
if (class($version) eq "IV") {
$version = $version->int_value;
} elsif (class($version) eq "NV") {
$version = $version->NV;
} elsif (class($version) ne "PVMG") {
# Includes PVIV and PVNV
$version = $version->PV;
} else {
# version specified as a v-string
$version = 'v'.join '.', map ord, split //, $version->PV;
}
$constop = $constop->sibling;
return if $constop->name ne "method_named";
return if $self->meth_sv($constop)->PV ne "VERSION";
}
$lineseq = $version_op->sibling;
return if $lineseq->name ne "lineseq";
my $entersub = $lineseq->first->sibling;
if ($entersub->name eq "stub") {
return "use $module $version ();\n" if defined $version;
return "use $module ();\n";
}
return if $entersub->name ne "entersub";
# See if there are import arguments
my $args = '';
my $svop = $entersub->first->sibling; # Skip over pushmark
return unless $self->const_sv($svop)->PV eq $module;
# Pull out the arguments
for ($svop=$svop->sibling; index($svop->name, "method_") != 0;
$svop = $svop->sibling) {
$args .= ", " if length($args);
$args .= $self->deparse($svop, 6);
}
my $use = 'use';
my $method_named = $svop;
return if $method_named->name ne "method_named";
my $method_name = $self->meth_sv($method_named)->PV;
if ($method_name eq "unimport") {
$use = 'no';
}
# Certain pragmas are dealt with using hint bits,
# so we ignore them here
if ($module eq 'strict' || $module eq 'integer'
|| $module eq 'bytes' || $module eq 'warnings'
|| $module eq 'feature') {
return "";
}
if (defined $version && length $args) {
return "$use $module $version ($args);\n";
} elsif (defined $version) {
return "$use $module $version;\n";
} elsif (length $args) {
return "$use $module ($args);\n";
} else {
return "$use $module;\n";
}
}
sub stash_subs {
my ($self, $pack, $seen) = @_;
my (@ret, $stash);
if (!defined $pack) {
$pack = '';
$stash = \%::;
}
else {
$pack =~ s/(::)?$/::/;
no strict 'refs';
$stash = \%{"main::$pack"};
}
return
if ($seen ||= {})->{
$INC{"overload.pm"} ? overload::StrVal($stash) : $stash
}++;
my $stashobj = svref_2object($stash);
my %stash = $stashobj->ARRAY;
while (my ($key, $val) = each %stash) {
my $flags = $val->FLAGS;
if ($flags & SVf_ROK) {
# A reference. Dump this if it is a reference to a CV. If it
# is a constant acting as a proxy for a full subroutine, then
# we may or may not have to dump it. If some form of perl-
# space visible code must have created it, be it a use
# statement, or some direct symbol-table manipulation code that
# we will deparse, then we don’t want to dump it. If it is the
# result of a declaration like sub f () { 42 } then we *do*
# want to dump it. The only way to distinguish these seems
# to be the SVs_PADTMP flag on the constant, which is admit-
# tedly a hack.
my $class = class(my $referent = $val->RV);
if ($class eq "CV") {
$self->todo($referent, 0);
} elsif (
$class !~ /^(AV|HV|CV|FM|IO|SPECIAL)\z/
# A more robust way to write that would be this, but B does
# not provide the SVt_ constants:
# ($referent->FLAGS & B::SVTYPEMASK) < B::SVt_PVAV
and $referent->FLAGS & SVs_PADTMP
) {
push @{$self->{'protos_todo'}}, [$pack . $key, $val];
}
} elsif ($flags & (SVf_POK|SVf_IOK)) {
# Just a prototype. As an ugly but fairly effective way
# to find out if it belongs here is to see if the AUTOLOAD
# (if any) for the stash was defined in one of our files.
my $A = $stash{"AUTOLOAD"};
if (defined ($A) && class($A) eq "GV" && defined($A->CV)
&& class($A->CV) eq "CV") {
my $AF = $A->FILE;
next unless $AF eq $0 || exists $self->{'files'}{$AF};
}
push @{$self->{'protos_todo'}},
[$pack . $key, $flags & SVf_POK ? $val->PV: undef];
} elsif (class($val) eq "GV") {
if (class(my $cv = $val->CV) ne "SPECIAL") {
next if $self->{'subs_done'}{$$val}++;
# Ignore imposters (aliases etc)
my $name = $cv->NAME_HEK;
if(defined $name) {
# avoid using $cv->GV here because if the $val GV is
# an alias, CvGV() could upgrade the real stash entry
# from an RV to a GV
next unless $name eq $key;
next unless $$stashobj == ${$cv->STASH};
}
else {
next if $$val != ${$cv->GV};
}
$self->todo($cv, 0);
}
if (class(my $cv = $val->FORM) ne "SPECIAL") {
next if $self->{'forms_done'}{$$val}++;
next if $$val != ${$cv->GV}; # Ignore imposters
$self->todo($cv, 1);
}
if (class($val->HV) ne "SPECIAL" && $key =~ /::$/) {
$self->stash_subs($pack . $key, $seen);
}
}
}
}
sub print_protos {
my $self = shift;
my $ar;
my @ret;
foreach $ar (@{$self->{'protos_todo'}}) {
if (ref $ar->[1]) {
# Only print a constant if it occurs in the same package as a
# dumped sub. This is not perfect, but a heuristic that will
# hopefully work most of the time. Ideally we would use
# CvFILE, but a constant stub has no CvFILE.
my $pack = ($ar->[0] =~ /(.*)::/)[0];
next if $pack and !$self->{packs}{$pack}
}
my $body = defined $ar->[1]
? ref $ar->[1]
? " () {\n " . $self->const($ar->[1]->RV,0) . ";\n}"
: " (". $ar->[1] . ");"
: ";";
push @ret, "sub " . $ar->[0] . "$body\n";
}
delete $self->{'protos_todo'};
return @ret;
}
sub style_opts {
my $self = shift;
my $opts = shift;
my $opt;
while (length($opt = substr($opts, 0, 1))) {
if ($opt eq "C") {
$self->{'cuddle'} = " ";
$opts = substr($opts, 1);
} elsif ($opt eq "i") {
$opts =~ s/^i(\d+)//;
$self->{'indent_size'} = $1;
} elsif ($opt eq "T") {
$self->{'use_tabs'} = 1;
$opts = substr($opts, 1);
} elsif ($opt eq "v") {
$opts =~ s/^v([^.]*)(.|$)//;
$self->{'ex_const'} = $1;
}
}
}
sub new {
my $class = shift;
my $self = bless {}, $class;
$self->{'cuddle'} = "\n";
$self->{'curcop'} = undef;
$self->{'curstash'} = "main";
$self->{'ex_const'} = "'???'";
$self->{'expand'} = 0;
$self->{'files'} = {};
$self->{'packs'} = {};
$self->{'indent_size'} = 4;
$self->{'linenums'} = 0;
$self->{'parens'} = 0;
$self->{'subs_todo'} = [];
$self->{'unquote'} = 0;
$self->{'use_dumper'} = 0;
$self->{'use_tabs'} = 0;
$self->{'ambient_warnings'} = undef; # Assume no lexical warnings
$self->{'ambient_hints'} = 0;
$self->{'ambient_hinthash'} = undef;
$self->init();
while (my $arg = shift @_) {
if ($arg eq "-d") {
$self->{'use_dumper'} = 1;
require Data::Dumper;
} elsif ($arg =~ /^-f(.*)/) {
$self->{'files'}{$1} = 1;
} elsif ($arg eq "-l") {
$self->{'linenums'} = 1;
} elsif ($arg eq "-p") {
$self->{'parens'} = 1;
} elsif ($arg eq "-P") {
$self->{'noproto'} = 1;
} elsif ($arg eq "-q") {
$self->{'unquote'} = 1;
} elsif (substr($arg, 0, 2) eq "-s") {
$self->style_opts(substr $arg, 2);
} elsif ($arg =~ /^-x(\d)$/) {
$self->{'expand'} = $1;
}
}
return $self;
}
{
# Mask out the bits that L<warnings::register> uses
my $WARN_MASK;
BEGIN {
$WARN_MASK = $warnings::Bits{all} | $warnings::DeadBits{all};
}
sub WARN_MASK () {
return $WARN_MASK;
}
}
# Initialise the contextual information, either from
# defaults provided with the ambient_pragmas method,
# or from perl's own defaults otherwise.
sub init {
my $self = shift;
$self->{'warnings'} = defined ($self->{'ambient_warnings'})
? $self->{'ambient_warnings'} & WARN_MASK
: undef;
$self->{'hints'} = $self->{'ambient_hints'};
$self->{'hinthash'} = $self->{'ambient_hinthash'};
# also a convenient place to clear out subs_declared
delete $self->{'subs_declared'};
}
sub compile {
my(@args) = @_;
return sub {
my $self = B::Deparse->new(@args);
# First deparse command-line args
if (defined $^I) { # deparse -i
print q(BEGIN { $^I = ).perlstring($^I).qq(; }\n);
}
if ($^W) { # deparse -w
print qq(BEGIN { \$^W = $^W; }\n);
}
if ($/ ne "\n" or defined $O::savebackslash) { # deparse -l and -0
my $fs = perlstring($/) || 'undef';
my $bs = perlstring($O::savebackslash) || 'undef';
print qq(BEGIN { \$/ = $fs; \$\\ = $bs; }\n);
}
my @BEGINs = B::begin_av->isa("B::AV") ? B::begin_av->ARRAY : ();
my @UNITCHECKs = B::unitcheck_av->isa("B::AV")
? B::unitcheck_av->ARRAY
: ();
my @CHECKs = B::check_av->isa("B::AV") ? B::check_av->ARRAY : ();
my @INITs = B::init_av->isa("B::AV") ? B::init_av->ARRAY : ();
my @ENDs = B::end_av->isa("B::AV") ? B::end_av->ARRAY : ();
my @names = qw(BEGIN UNITCHECK CHECK INIT END);
my @blocks = \(@BEGINs, @UNITCHECKs, @CHECKs, @INITs, @ENDs);
while (@names) {
my ($name, $blocks) = (shift @names, shift @blocks);
for my $block (@$blocks) {
$self->todo($block, 0, $name);
}
}
$self->stash_subs();
local($SIG{"__DIE__"}) =
sub {
if ($self->{'curcop'}) {
my $cop = $self->{'curcop'};
my($line, $file) = ($cop->line, $cop->file);
print STDERR "While deparsing $file near line $line,\n";
}
};
$self->{'curcv'} = main_cv;
$self->{'curcvlex'} = undef;
print $self->print_protos;
@{$self->{'subs_todo'}} =
sort {$a->[0] <=> $b->[0]} @{$self->{'subs_todo'}};
my $root = main_root;
local $B::overlay = {};
unless (null $root) {
$self->pad_subs($self->{'curcv'});
# Check for a stub-followed-by-ex-cop, resulting from a program
# consisting solely of sub declarations. For backward-compati-
# bility (and sane output) we don’t want to emit the stub.
# leave
# enter
# stub
# ex-nextstate (or ex-dbstate)
my $kid;
if ( $root->name eq 'leave'
and ($kid = $root->first)->name eq 'enter'
and !null($kid = $kid->sibling) and $kid->name eq 'stub'
and !null($kid = $kid->sibling) and $kid->name eq 'null'
and class($kid) eq 'COP' and null $kid->sibling )
{
# ignore
} else {
$self->pessimise($root, main_start);
print $self->indent($self->deparse_root($root)), "\n";
}
}
my @text;
while (scalar(@{$self->{'subs_todo'}})) {
push @text, $self->next_todo;
}
print $self->indent(join("", @text)), "\n" if @text;
# Print __DATA__ section, if necessary
no strict 'refs';
my $laststash = defined $self->{'curcop'}
? $self->{'curcop'}->stash->NAME : $self->{'curstash'};
if (defined *{$laststash."::DATA"}{IO}) {
print $self->keyword("package") . " $laststash;\n"
unless $laststash eq $self->{'curstash'};
print $self->keyword("__DATA__") . "\n";
print readline(*{$laststash."::DATA"});
}
}
}
sub coderef2text {
my $self = shift;
my $sub = shift;
croak "Usage: ->coderef2text(CODEREF)" unless UNIVERSAL::isa($sub, "CODE");
$self->init();
local $self->{in_coderef2text} = 1;
return $self->indent($self->deparse_sub(svref_2object($sub)));
}
my %strict_bits = do {
local $^H;
map +($_ => strict::bits($_)), qw/refs subs vars/
};
sub ambient_pragmas {
my $self = shift;
my ($hint_bits, $warning_bits, $hinthash) = (0);
while (@_ > 1) {
my $name = shift();
my $val = shift();
if ($name eq 'strict') {
require strict;
if ($val eq 'none') {
$hint_bits &= $strict_bits{$_} for qw/refs subs vars/;
next();
}
my @names;
if ($val eq "all") {
@names = qw/refs subs vars/;
}
elsif (ref $val) {
@names = @$val;
}
else {
@names = split' ', $val;
}
$hint_bits |= $strict_bits{$_} for @names;
}
elsif ($name eq 'integer'
|| $name eq 'bytes'
|| $name eq 'utf8') {
require "$name.pm";
if ($val) {
$hint_bits |= ${$::{"${name}::"}{"hint_bits"}};
}
else {
$hint_bits &= ~${$::{"${name}::"}{"hint_bits"}};
}
}
elsif ($name eq 're') {
require re;
if ($val eq 'none') {
$hint_bits &= ~re::bits(qw/taint eval/);
next();
}
my @names;
if ($val eq 'all') {
@names = qw/taint eval/;
}
elsif (ref $val) {
@names = @$val;
}
else {
@names = split' ',$val;
}
$hint_bits |= re::bits(@names);
}
elsif ($name eq 'warnings') {
if ($val eq 'none') {
$warning_bits = $warnings::NONE;
next();
}
my @names;
if (ref $val) {
@names = @$val;
}
else {
@names = split/\s+/, $val;
}
$warning_bits = $warnings::NONE if !defined ($warning_bits);
$warning_bits |= warnings::bits(@names);
}
elsif ($name eq 'warning_bits') {
$warning_bits = $val;
}
elsif ($name eq 'hint_bits') {
$hint_bits = $val;
}
elsif ($name eq '%^H') {
$hinthash = $val;
}
else {
croak "Unknown pragma type: $name";
}
}
if (@_) {
croak "The ambient_pragmas method expects an even number of args";
}
$self->{'ambient_warnings'} = $warning_bits;
$self->{'ambient_hints'} = $hint_bits;
$self->{'ambient_hinthash'} = $hinthash;
}
# This method is the inner loop, so try to keep it simple
sub deparse {
my $self = shift;
my($op, $cx) = @_;
Carp::confess("Null op in deparse") if !defined($op)
|| class($op) eq "NULL";
my $meth = "pp_" . $op->name;
return $self->$meth($op, $cx);
}
sub indent {
my $self = shift;
my $txt = shift;
# \cK also swallows a preceding line break when followed by a
# semicolon.
$txt =~ s/\n\cK;//g;
my @lines = split(/\n/, $txt);
my $leader = "";
my $level = 0;
my $line;
for $line (@lines) {
my $cmd = substr($line, 0, 1);
if ($cmd eq "\t" or $cmd eq "\b") {
$level += ($cmd eq "\t" ? 1 : -1) * $self->{'indent_size'};
if ($self->{'use_tabs'}) {
$leader = "\t" x ($level / 8) . " " x ($level % 8);
} else {
$leader = " " x $level;
}
$line = substr($line, 1);
}
if (index($line, "\f") > 0) {
$line =~ s/\f/\n/;
}
if (substr($line, 0, 1) eq "\f") {
$line = substr($line, 1); # no indent
} else {
$line = $leader . $line;
}
$line =~ s/\cK;?//g;
}
return join("\n", @lines);
}
sub pad_subs {
my ($self, $cv) = @_;
my $padlist = $cv->PADLIST;
my @names = $padlist->ARRAYelt(0)->ARRAY;
my @values = $padlist->ARRAYelt(1)->ARRAY;
my @todo;
PADENTRY:
for my $ix (0.. $#names) { for $_ ($names[$ix]) {
next if class($_) eq "SPECIAL";
my $name = $_->PVX;
if (defined $name && $name =~ /^&./) {
my $low = $_->COP_SEQ_RANGE_LOW;
my $flags = $_->FLAGS;
my $outer = $flags & PADNAMEt_OUTER;
if ($flags & SVpad_OUR) {
push @todo, [$low, undef, 0, $_]
# [seq, no cv, not format, padname]
unless $outer;
next;
}
my $protocv = $flags & SVpad_STATE
? $values[$ix]
: $_->PROTOCV;
if (class ($protocv) ne 'CV') {
my $flags = $flags;
my $cv = $cv;
my $name = $_;
while ($flags & PADNAMEt_OUTER && class ($protocv) ne 'CV')
{
$cv = $cv->OUTSIDE;
next PADENTRY if class($cv) eq 'SPECIAL'; # XXX freed?
my $padlist = $cv->PADLIST;
my $ix = $name->PARENT_PAD_INDEX;
$name = $padlist->NAMES->ARRAYelt($ix);
$flags = $name->FLAGS;
$protocv = $flags & SVpad_STATE
? $padlist->ARRAYelt(1)->ARRAYelt($ix)
: $name->PROTOCV;
}
}
my $defined_in_this_sub = ${$protocv->OUTSIDE} == $$cv || do {
my $other = $protocv->PADLIST;
$$other && $other->outid == $padlist->id;
};
if ($flags & PADNAMEt_OUTER) {
next unless $defined_in_this_sub;
push @todo, [$protocv->OUTSIDE_SEQ, $protocv, 0, $_];
next;
}
my $outseq = $protocv->OUTSIDE_SEQ;
if ($outseq <= $low) {
# defined before its name is visible, so it’s gotta be
# declared and defined at once: my sub foo { ... }
push @todo, [$low, $protocv, 0, $_];
}
else {
# declared and defined separately: my sub f; sub f { ... }
push @todo, [$low, undef, 0, $_];
push @todo, [$outseq, $protocv, 0, $_]
if $defined_in_this_sub;
}
}
}}
@{$self->{'subs_todo'}} =
sort {$a->[0] <=> $b->[0]} @{$self->{'subs_todo'}}, @todo
}
# deparse_argops(): deparse, if possible, a sequence of argcheck + argelem
# ops into a subroutine signature. If successful, return the first op
# following the signature ops plus the signature string; else return the
# empty list.
#
# Normally a bunch of argelem ops will have been generated by the
# signature parsing, but it's possible that ops have been added manually
# or altered. In this case we return "()" and fall back to general
# deparsing of the individual sigelems as 'my $x = $_[N]' etc.
#
# We're only called if the top is an ex-argcheck, which is a placeholder
# indicating a signature subtree.
#
# Return a signature string, or an empty list if no deparseable as a
# signature
sub deparse_argops {
my ($self, $topop, $cv) = @_;
my @sig;
$topop = $topop->first;
return unless $$topop and $topop->name eq 'lineseq';
# last op should be nextstate
my $last = $topop->last;
return unless $$last
and ( _op_is_or_was($last, OP_NEXTSTATE)
or _op_is_or_was($last, OP_DBSTATE));
# first OP_NEXTSTATE
my $o = $topop->first;
return unless $$o;
return if $o->label;
# OP_ARGCHECK
$o = $o->sibling;
return unless $$o and $o->name eq 'argcheck';
my ($params, $opt_params, $slurpy) = $o->aux_list($cv);
my $mandatory = $params - $opt_params;
my $seen_slurpy = 0;
my $last_ix = -1;
# keep looking for valid nextstate + argelem pairs, terminated
# by a final nextstate
while (1) {
$o = $o->sibling;
return unless $$o;
# skip trailing nextstate
last if $$o == $$last;
# OP_NEXTSTATE
return unless $o->name =~ /^(next|db)state$/;
return if $o->label;
# OP_ARGELEM
$o = $o->sibling;
last unless $$o;
if ($o->name eq 'argelem') {
my $ix = $o->string($cv);
while (++$last_ix < $ix) {
push @sig, $last_ix < $mandatory ? '$' : '$=';
}
my $var = $self->padname($o->targ);
if ($var =~ /^[@%]/) {
return if $seen_slurpy;
$seen_slurpy = 1;
return if $ix != $params or !$slurpy
or substr($var,0,1) ne $slurpy;
}
else {
return if $ix >= $params;
}
if ($o->flags & OPf_KIDS) {
my $kid = $o->first;
return unless $$kid and $kid->name eq 'argdefelem';
my $def = $self->deparse($kid->first, 7);
$def = "($def)" if $kid->first->flags & OPf_PARENS;
$var .= " = $def";
}
push @sig, $var;
}
elsif ($o->name eq 'null'
and ($o->flags & OPf_KIDS)
and $o->first->name eq 'argdefelem')
{
# special case - a void context default expression: $ = expr
my $defop = $o->first;
my $ix = $defop->targ;
while (++$last_ix < $ix) {
push @sig, $last_ix < $mandatory ? '$' : '$=';
}
return if $last_ix >= $params
or $last_ix < $mandatory;
my $def = $self->deparse($defop->first, 7);
$def = "($def)" if $defop->first->flags & OPf_PARENS;
push @sig, '$ = ' . $def;
}
else {
return;
}
}
while (++$last_ix < $params) {
push @sig, $last_ix < $mandatory ? '$' : '$=';
}
push @sig, $slurpy if $slurpy and !$seen_slurpy;
return (join(', ', @sig));
}
# Deparse a sub. Returns everything except the 'sub foo',
# e.g. ($$) : method { ...; }
# or : prototype($$) lvalue ($a, $b) { ...; };
sub deparse_sub {
my $self = shift;
my $cv = shift;
my @attrs;
my $proto;
my $sig;
Carp::confess("NULL in deparse_sub") if !defined($cv) || $cv->isa("B::NULL");
Carp::confess("SPECIAL in deparse_sub") if $cv->isa("B::SPECIAL");
local $self->{'curcop'} = $self->{'curcop'};
my $has_sig = $self->{hinthash}{feature_signatures};
if ($cv->FLAGS & SVf_POK) {
my $myproto = $cv->PV;
if ($has_sig) {
push @attrs, "prototype($myproto)";
}
else {
$proto = $myproto;
}
}
if ($cv->CvFLAGS & (CVf_METHOD|CVf_LOCKED|CVf_LVALUE|CVf_ANONCONST)) {
push @attrs, "lvalue" if $cv->CvFLAGS & CVf_LVALUE;
push @attrs, "method" if $cv->CvFLAGS & CVf_METHOD;
push @attrs, "const" if $cv->CvFLAGS & CVf_ANONCONST;
}
local($self->{'curcv'}) = $cv;
local($self->{'curcvlex'});
local(@$self{qw'curstash warnings hints hinthash'})
= @$self{qw'curstash warnings hints hinthash'};
my $body;
my $root = $cv->ROOT;
local $B::overlay = {};
if (not null $root) {
$self->pad_subs($cv);
$self->pessimise($root, $cv->START);
my $lineseq = $root->first;
# stub sub may have single op rather than list of ops
my $is_list = ($lineseq->name eq "lineseq");
my $firstop = $is_list ? $lineseq->first : $lineseq;
# Try to deparse first subtree as a signature if possible.
# Top of signature subtree has an ex-argcheck as a placeholder
if ( $has_sig
and $$firstop
and $firstop->name eq 'null'
and $firstop->targ == OP_ARGCHECK
) {
my ($mysig) = $self->deparse_argops($firstop, $cv);
if (defined $mysig) {
$sig = $mysig;
$firstop = $is_list ? $firstop->sibling : undef;
}
}
if ($is_list && $firstop) {
my @ops;
for (my $o = $firstop; $$o; $o=$o->sibling) {
push @ops, $o;
}
$body = $self->lineseq(undef, 0, @ops).";";
if (!$has_sig and $ops[-1]->name =~ /^(next|db)state$/) {
# this handles void context in
# use feature signatures; sub ($=1) {}
$body .= "\n()";
}
my $scope_en = $self->find_scope_en($lineseq);
if (defined $scope_en) {
my $subs = join"", $self->seq_subs($scope_en);
$body .= ";\n$subs" if length($subs);
}
}
elsif ($firstop) {
$body = $self->deparse($root->first, 0);
}
else {
$body = ';'; # stub sub
}
my $l = '';
if ($self->{'linenums'}) {
# a glob's gp_line is set from the line containing a
# sub's closing '}' if the CV is the first use of the GV.
# So make sure the linenum is set correctly for '}'
my $gv = $cv->GV;
my $line = $gv->LINE;
my $file = $gv->FILE;
$l = "\f#line $line \"$file\"\n";
}
$body = "{\n\t$body\n$l\b}";
}
else {
my $sv = $cv->const_sv;
if ($$sv) {
# uh-oh. inlinable sub... format it differently
$body = "{ " . $self->const($sv, 0) . " }\n";
} else { # XSUB? (or just a declaration)
$body = ';'
}
}
$proto = defined $proto ? "($proto) " : "";
$sig = defined $sig ? "($sig) " : "";
my $attrs = '';
$attrs = ': ' . join('', map "$_ ", @attrs) if @attrs;
return "$proto$attrs$sig$body\n";
}
sub deparse_format {
my $self = shift;
my $form = shift;
my @text;
local($self->{'curcv'}) = $form;
local($self->{'curcvlex'});
local($self->{'in_format'}) = 1;
local(@$self{qw'curstash warnings hints hinthash'})
= @$self{qw'curstash warnings hints hinthash'};
my $op = $form->ROOT;
local $B::overlay = {};
$self->pessimise($op, $form->START);
my $kid;
return "\f." if $op->first->name eq 'stub'
|| $op->first->name eq 'nextstate';
$op = $op->first->first; # skip leavewrite, lineseq
while (not null $op) {
$op = $op->sibling; # skip nextstate
my @exprs;
$kid = $op->first->sibling; # skip pushmark
push @text, "\f".$self->const_sv($kid)->PV;
$kid = $kid->sibling;
for (; not null $kid; $kid = $kid->sibling) {
push @exprs, $self->deparse($kid, -1);
$exprs[-1] =~ s/;\z//;
}
push @text, "\f".join(", ", @exprs)."\n" if @exprs;
$op = $op->sibling;
}
return join("", @text) . "\f.";
}
sub is_scope {
my $op = shift;
return $op->name eq "leave" || $op->name eq "scope"
|| $op->name eq "lineseq"
|| ($op->name eq "null" && class($op) eq "UNOP"
&& (is_scope($op->first) || $op->first->name eq "enter"));
}
sub is_state {
my $name = $_[0]->name;
return $name eq "nextstate" || $name eq "dbstate" || $name eq "setstate";
}
sub is_miniwhile { # check for one-line loop ('foo() while $y--')
my $op = shift;
return (!null($op) and null($op->sibling)
and $op->name eq "null" and class($op) eq "UNOP"
and (($op->first->name =~ /^(and|or)$/
and $op->first->first->sibling->name eq "lineseq")
or ($op->first->name eq "lineseq"
and not null $op->first->first->sibling
and $op->first->first->sibling->name eq "unstack")
));
}
# Check if the op and its sibling are the initialization and the rest of a
# for (..;..;..) { ... } loop
sub is_for_loop {
my $op = shift;
# This OP might be almost anything, though it won't be a
# nextstate. (It's the initialization, so in the canonical case it
# will be an sassign.) The sibling is (old style) a lineseq whose
# first child is a nextstate and whose second is a leaveloop, or
# (new style) an unstack whose sibling is a leaveloop.
my $lseq = $op->sibling;
return 0 unless !is_state($op) and !null($lseq);
if ($lseq->name eq "lineseq") {
if ($lseq->first && !null($lseq->first) && is_state($lseq->first)
&& (my $sib = $lseq->first->sibling)) {
return (!null($sib) && $sib->name eq "leaveloop");
}
} elsif ($lseq->name eq "unstack" && ($lseq->flags & OPf_SPECIAL)) {
my $sib = $lseq->sibling;
return $sib && !null($sib) && $sib->name eq "leaveloop";
}
return 0;
}
sub is_scalar {
my $op = shift;
return ($op->name eq "rv2sv" or
$op->name eq "padsv" or
$op->name eq "gv" or # only in array/hash constructs
$op->flags & OPf_KIDS && !null($op->first)
&& $op->first->name eq "gvsv");
}
sub maybe_parens {
my $self = shift;
my($text, $cx, $prec) = @_;
if ($prec < $cx # unary ops nest just fine
or $prec == $cx and $cx != 4 and $cx != 16 and $cx != 21
or $self->{'parens'})
{
$text = "($text)";
# In a unop, let parent reuse our parens; see maybe_parens_unop
$text = "\cS" . $text if $cx == 16;
return $text;
} else {
return $text;
}
}
# same as above, but get around the 'if it looks like a function' rule
sub maybe_parens_unop {
my $self = shift;
my($name, $kid, $cx) = @_;
if ($cx > 16 or $self->{'parens'}) {
$kid = $self->deparse($kid, 1);
if ($name eq "umask" && $kid =~ /^\d+$/) {
$kid = sprintf("%#o", $kid);
}
return $self->keyword($name) . "($kid)";
} else {
$kid = $self->deparse($kid, 16);
if ($name eq "umask" && $kid =~ /^\d+$/) {
$kid = sprintf("%#o", $kid);
}
$name = $self->keyword($name);
if (substr($kid, 0, 1) eq "\cS") {
# use kid's parens
return $name . substr($kid, 1);
} elsif (substr($kid, 0, 1) eq "(") {
# avoid looks-like-a-function trap with extra parens
# ('+' can lead to ambiguities)
return "$name(" . $kid . ")";
} else {
return "$name $kid";
}
}
}
sub maybe_parens_func {
my $self = shift;
my($func, $text, $cx, $prec) = @_;
if ($prec <= $cx or substr($text, 0, 1) eq "(" or $self->{'parens'}) {
return "$func($text)";
} else {
return "$func $text";
}
}
sub find_our_type {
my ($self, $name) = @_;
$self->populate_curcvlex() if !defined $self->{'curcvlex'};
my $seq = $self->{'curcop'} ? $self->{'curcop'}->cop_seq : 0;
for my $a (@{$self->{'curcvlex'}{"o$name"}}) {
my ($st, undef, $padname) = @$a;
if ($st >= $seq && $padname->FLAGS & SVpad_TYPED) {
return $padname->SvSTASH->NAME;
}
}
return '';
}
sub maybe_local {
my $self = shift;
my($op, $cx, $text) = @_;
my $name = $op->name;
my $our_intro = ($name =~ /^(?:(?:gv|rv2)[ash]v|split|refassign
|lv(?:av)?ref)$/x)
? OPpOUR_INTRO
: 0;
my $lval_intro = $name eq 'split' ? 0 : OPpLVAL_INTRO;
# The @a in \(@a) isn't in ref context, but only when the
# parens are there.
my $need_parens = $self->{'in_refgen'} && $name =~ /[ah]v\z/
&& ($op->flags & (OPf_PARENS|OPf_REF)) == OPf_PARENS;
if ((my $priv = $op->private) & ($lval_intro|$our_intro)) {
my @our_local;
push @our_local, "local" if $priv & $lval_intro;
push @our_local, "our" if $priv & $our_intro;
my $our_local = join " ", map $self->keyword($_), @our_local;
if( $our_local[-1] eq 'our' ) {
if ( $text !~ /^\W(\w+::)*\w+\z/
and !utf8::decode($text) || $text !~ /^\W(\w+::)*\w+\z/
) {
die "Unexpected our($text)\n";
}
$text =~ s/(\w+::)+//;
if (my $type = $self->find_our_type($text)) {
$our_local .= ' ' . $type;
}
}
return $need_parens ? "($text)" : $text
if $self->{'avoid_local'}{$$op};
if ($need_parens) {
return "$our_local($text)";
} elsif (want_scalar($op) || $our_local eq 'our') {
return "$our_local $text";
} else {
return $self->maybe_parens_func("$our_local", $text, $cx, 16);
}
} else {
return $need_parens ? "($text)" : $text;
}
}
sub maybe_targmy {
my $self = shift;
my($op, $cx, $func, @args) = @_;
if ($op->private & OPpTARGET_MY) {
my $var = $self->padname($op->targ);
my $val = $func->($self, $op, 7, @args);
return $self->maybe_parens("$var = $val", $cx, 7);
} else {
return $func->($self, $op, $cx, @args);
}
}
sub padname_sv {
my $self = shift;
my $targ = shift;
return $self->{'curcv'}->PADLIST->ARRAYelt(0)->ARRAYelt($targ);
}
sub maybe_my {
my $self = shift;
my($op, $cx, $text, $padname, $forbid_parens) = @_;
# The @a in \(@a) isn't in ref context, but only when the
# parens are there.
my $need_parens = !$forbid_parens && $self->{'in_refgen'}
&& $op->name =~ /[ah]v\z/
&& ($op->flags & (OPf_PARENS|OPf_REF)) == OPf_PARENS;
# The @a in \my @a must not have parens.
if (!$need_parens && $self->{'in_refgen'}) {
$forbid_parens = 1;
}
if ($op->private & OPpLVAL_INTRO and not $self->{'avoid_local'}{$$op}) {
# Check $padname->FLAGS for statehood, rather than $op->private,
# because enteriter ops do not carry the flag.
my $my =
$self->keyword($padname->FLAGS & SVpad_STATE ? "state" : "my");
if ($padname->FLAGS & SVpad_TYPED) {
$my .= ' ' . $padname->SvSTASH->NAME;
}
if ($need_parens) {
return "$my($text)";
} elsif ($forbid_parens || want_scalar($op)) {
return "$my $text";
} else {
return $self->maybe_parens_func($my, $text, $cx, 16);
}
} else {
return $need_parens ? "($text)" : $text;
}
}
# The following OPs don't have functions:
# pp_padany -- does not exist after parsing
sub AUTOLOAD {
if ($AUTOLOAD =~ s/^.*::pp_//) {
warn "unexpected OP_".
($_[1]->type == OP_CUSTOM ? "CUSTOM ($AUTOLOAD)" : uc $AUTOLOAD);
return "XXX";
} else {
die "Undefined subroutine $AUTOLOAD called";
}
}
sub DESTROY {} # Do not AUTOLOAD
# $root should be the op which represents the root of whatever
# we're sequencing here. If it's undefined, then we don't append
# any subroutine declarations to the deparsed ops, otherwise we
# append appropriate declarations.
sub lineseq {
my($self, $root, $cx, @ops) = @_;
my($expr, @exprs);
my $out_cop = $self->{'curcop'};
my $out_seq = defined($out_cop) ? $out_cop->cop_seq : undef;
my $limit_seq;
if (defined $root) {
$limit_seq = $out_seq;
my $nseq;
$nseq = $self->find_scope_st($root->sibling) if ${$root->sibling};
$limit_seq = $nseq if !defined($limit_seq)
or defined($nseq) && $nseq < $limit_seq;
}
$limit_seq = $self->{'limit_seq'}
if defined($self->{'limit_seq'})
&& (!defined($limit_seq) || $self->{'limit_seq'} < $limit_seq);
local $self->{'limit_seq'} = $limit_seq;
$self->walk_lineseq($root, \@ops,
sub { push @exprs, $_[0]} );
my $sep = $cx ? '; ' : ";\n";
my $body = join($sep, grep {length} @exprs);
my $subs = "";
if (defined $root && defined $limit_seq && !$self->{'in_format'}) {
$subs = join "\n", $self->seq_subs($limit_seq);
}
return join($sep, grep {length} $body, $subs);
}
sub scopeop {
my($real_block, $self, $op, $cx) = @_;
my $kid;
my @kids;
local(@$self{qw'curstash warnings hints hinthash'})
= @$self{qw'curstash warnings hints hinthash'} if $real_block;
if ($real_block) {
$kid = $op->first->sibling; # skip enter
if (is_miniwhile($kid)) {
my $top = $kid->first;
my $name = $top->name;
if ($name eq "and") {
$name = $self->keyword("while");
} elsif ($name eq "or") {
$name = $self->keyword("until");
} else { # no conditional -> while 1 or until 0
return $self->deparse($top->first, 1) . " "
. $self->keyword("while") . " 1";
}
my $cond = $top->first;
my $body = $cond->sibling->first; # skip lineseq
$cond = $self->deparse($cond, 1);
$body = $self->deparse($body, 1);
return "$body $name $cond";
}
} else {
$kid = $op->first;
}
for (; !null($kid); $kid = $kid->sibling) {
push @kids, $kid;
}
if ($cx > 0) { # inside an expression, (a do {} while for lineseq)
my $body = $self->lineseq($op, 0, @kids);
return is_lexical_subs(@kids)
? $body
: ($self->lex_in_scope("&do") ? "CORE::do" : "do")
. " {\n\t$body\n\b}";
} else {
my $lineseq = $self->lineseq($op, $cx, @kids);
return (length ($lineseq) ? "$lineseq;" : "");
}
}
sub pp_scope { scopeop(0, @_); }
sub pp_lineseq { scopeop(0, @_); }
sub pp_leave { scopeop(1, @_); }
# This is a special case of scopeop and lineseq, for the case of the
# main_root. The difference is that we print the output statements as
# soon as we get them, for the sake of impatient users.
sub deparse_root {
my $self = shift;
my($op) = @_;
local(@$self{qw'curstash warnings hints hinthash'})
= @$self{qw'curstash warnings hints hinthash'};
my @kids;
return if null $op->first; # Can happen, e.g., for Bytecode without -k
for (my $kid = $op->first->sibling; !null($kid); $kid = $kid->sibling) {
push @kids, $kid;
}
$self->walk_lineseq($op, \@kids,
sub { return unless length $_[0];
print $self->indent($_[0].';');
print "\n"
unless $_[1] == $#kids;
});
}
sub walk_lineseq {
my ($self, $op, $kids, $callback) = @_;
my @kids = @$kids;
for (my $i = 0; $i < @kids; $i++) {
my $expr = "";
if (is_state $kids[$i]) {
$expr = $self->deparse($kids[$i++], 0);
if ($i > $#kids) {
$callback->($expr, $i);
last;
}
}
if (is_for_loop($kids[$i])) {
$callback->($expr . $self->for_loop($kids[$i], 0),
$i += $kids[$i]->sibling->name eq "unstack" ? 2 : 1);
next;
}
my $expr2 = $self->deparse($kids[$i], (@kids != 1)/2);
$expr2 =~ s/^sub :(?!:)/+sub :/; # statement label otherwise
$expr .= $expr2;
$callback->($expr, $i);
}
}
# The BEGIN {} is used here because otherwise this code isn't executed
# when you run B::Deparse on itself.
my %globalnames;
BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC",
"ENV", "ARGV", "ARGVOUT", "_"); }
sub gv_name {
my $self = shift;
my $gv = shift;
my $raw = shift;
#Carp::confess() unless ref($gv) eq "B::GV";
my $cv = $gv->FLAGS & SVf_ROK ? $gv->RV : 0;
my $stash = ($cv || $gv)->STASH->NAME;
my $name = $raw
? $cv ? $cv->NAME_HEK || $cv->GV->NAME : $gv->NAME
: $cv
? B::safename($cv->NAME_HEK || $cv->GV->NAME)
: $gv->SAFENAME;
if ($stash eq 'main' && $name =~ /^::/) {
$stash = '::';
}
elsif (($stash eq 'main'
&& ($globalnames{$name} || $name =~ /^[^A-Za-z_:]/))
or ($stash eq $self->{'curstash'} && !$globalnames{$name}
&& ($stash eq 'main' || $name !~ /::/))
)
{
$stash = "";
} else {
$stash = $stash . "::";
}
if (!$raw and $name =~ /^(\^..|{)/) {
$name = "{$name}"; # ${^WARNING_BITS}, etc and ${
}
return $stash . $name;
}
# Return the name to use for a stash variable.
# If a lexical with the same name is in scope, or
# if strictures are enabled, it may need to be
# fully-qualified.
sub stash_variable {
my ($self, $prefix, $name, $cx) = @_;
return $prefix.$self->maybe_qualify($prefix, $name) if $name =~ /::/;
unless ($prefix eq '$' || $prefix eq '@' || $prefix eq '&' || #'
$prefix eq '%' || $prefix eq '$#') {
return "$prefix$name";
}
if ($name =~ /^[^[:alpha:]_+-]$/) {
if (defined $cx && $cx == 26) {
if ($prefix eq '@') {
return "$prefix\{$name}";
}
elsif ($name eq '#') { return '${#}' } # "${#}a" vs "$#a"
}
if ($prefix eq '$#') {
return "\$#{$name}";
}
}
return $prefix . $self->maybe_qualify($prefix, $name);
}
my %unctrl = # portable to EBCDIC
(
"\c@" => '@', # unused
"\cA" => 'A',
"\cB" => 'B',
"\cC" => 'C',
"\cD" => 'D',
"\cE" => 'E',
"\cF" => 'F',
"\cG" => 'G',
"\cH" => 'H',
"\cI" => 'I',
"\cJ" => 'J',
"\cK" => 'K',
"\cL" => 'L',
"\cM" => 'M',
"\cN" => 'N',
"\cO" => 'O',
"\cP" => 'P',
"\cQ" => 'Q',
"\cR" => 'R',
"\cS" => 'S',
"\cT" => 'T',
"\cU" => 'U',
"\cV" => 'V',
"\cW" => 'W',
"\cX" => 'X',
"\cY" => 'Y',
"\cZ" => 'Z',
"\c[" => '[', # unused
"\c\\" => '\\', # unused
"\c]" => ']', # unused
"\c_" => '_', # unused
);
# Return just the name, without the prefix. It may be returned as a quoted
# string. The second return value is a boolean indicating that.
sub stash_variable_name {
my($self, $prefix, $gv) = @_;
my $name = $self->gv_name($gv, 1);
$name = $self->maybe_qualify($prefix,$name);
if ($name =~ /^(?:\S|(?!\d)[\ca-\cz]?(?:\w|::)*|\d+)\z/) {
$name =~ s/^([\ca-\cz])/'^' . $unctrl{$1}/e;
$name =~ /^(\^..|{)/ and $name = "{$name}";
return $name, 0; # not quoted
}
else {
single_delim("q", "'", $name, $self), 1;
}
}
sub maybe_qualify {
my ($self,$prefix,$name) = @_;
my $v = ($prefix eq '$#' ? '@' : $prefix) . $name;
if ($prefix eq "") {
$name .= "::" if $name =~ /(?:\ACORE::[^:]*|::)\z/;
return $name;
}
return $name if $name =~ /::/;
return $self->{'curstash'}.'::'. $name
if
$name =~ /^(?!\d)\w/ # alphabetic
&& $v !~ /^\$[ab]\z/ # not $a or $b
&& $v =~ /\A[\$\@\%\&]/ # scalar, array, hash, or sub
&& !$globalnames{$name} # not a global name
&& $self->{hints} & $strict_bits{vars} # strict vars
&& !$self->lex_in_scope($v,1) # no "our"
or $self->lex_in_scope($v); # conflicts with "my" variable
return $name;
}
sub lex_in_scope {
my ($self, $name, $our) = @_;
substr $name, 0, 0, = $our ? 'o' : 'm'; # our/my
$self->populate_curcvlex() if !defined $self->{'curcvlex'};
return 0 if !defined($self->{'curcop'});
my $seq = $self->{'curcop'}->cop_seq;
return 0 if !exists $self->{'curcvlex'}{$name};
for my $a (@{$self->{'curcvlex'}{$name}}) {
my ($st, $en) = @$a;
return 1 if $seq > $st && $seq <= $en;
}
return 0;
}
sub populate_curcvlex {
my $self = shift;
for (my $cv = $self->{'curcv'}; class($cv) eq "CV"; $cv = $cv->OUTSIDE) {
my $padlist = $cv->PADLIST;
# an undef CV still in lexical chain
next if class($padlist) eq "SPECIAL";
my @padlist = $padlist->ARRAY;
my @ns = $padlist[0]->ARRAY;
for (my $i=0; $i<@ns; ++$i) {
next if class($ns[$i]) eq "SPECIAL";
if (class($ns[$i]) eq "PV") {
# Probably that pesky lexical @_
next;
}
my $name = $ns[$i]->PVX;
next unless defined $name;
my ($seq_st, $seq_en) =
($ns[$i]->FLAGS & SVf_FAKE)
? (0, 999999)
: ($ns[$i]->COP_SEQ_RANGE_LOW, $ns[$i]->COP_SEQ_RANGE_HIGH);
push @{$self->{'curcvlex'}{
($ns[$i]->FLAGS & SVpad_OUR ? 'o' : 'm') . $name
}}, [$seq_st, $seq_en, $ns[$i]];
}
}
}
sub find_scope_st { ((find_scope(@_))[0]); }
sub find_scope_en { ((find_scope(@_))[1]); }
# Recurses down the tree, looking for pad variable introductions and COPs
sub find_scope {
my ($self, $op, $scope_st, $scope_en) = @_;
carp("Undefined op in find_scope") if !defined $op;
return ($scope_st, $scope_en) unless $op->flags & OPf_KIDS;
my @queue = ($op);
while(my $op = shift @queue ) {
for (my $o=$op->first; $$o; $o=$o->sibling) {
if ($o->name =~ /^pad.v$/ && $o->private & OPpLVAL_INTRO) {
my $s = int($self->padname_sv($o->targ)->COP_SEQ_RANGE_LOW);
my $e = $self->padname_sv($o->targ)->COP_SEQ_RANGE_HIGH;
$scope_st = $s if !defined($scope_st) || $s < $scope_st;
$scope_en = $e if !defined($scope_en) || $e > $scope_en;
return ($scope_st, $scope_en);
}
elsif (is_state($o)) {
my $c = $o->cop_seq;
$scope_st = $c if !defined($scope_st) || $c < $scope_st;
$scope_en = $c if !defined($scope_en) || $c > $scope_en;
return ($scope_st, $scope_en);
}
elsif ($o->flags & OPf_KIDS) {
unshift (@queue, $o);
}
}
}
return ($scope_st, $scope_en);
}
# Returns a list of subs which should be inserted before the COP
sub cop_subs {
my ($self, $op, $out_seq) = @_;
my $seq = $op->cop_seq;
$seq = $out_seq if defined($out_seq) && $out_seq < $seq;
return $self->seq_subs($seq);
}
sub seq_subs {
my ($self, $seq) = @_;
my @text;
#push @text, "# ($seq)\n";
return "" if !defined $seq;
my @pending;
while (scalar(@{$self->{'subs_todo'}})
and $seq > $self->{'subs_todo'}[0][0]) {
my $cv = $self->{'subs_todo'}[0][1];
# Skip the OUTSIDE check for lexical subs. We may be deparsing a
# cloned anon sub with lexical subs declared in it, in which case
# the OUTSIDE pointer points to the anon protosub.
my $lexical = ref $self->{'subs_todo'}[0][3];
my $outside = !$lexical && $cv && $cv->OUTSIDE;
if (!$lexical and $cv
and ${$cv->OUTSIDE || \0} != ${$self->{'curcv'}})
{
push @pending, shift @{$self->{'subs_todo'}};
next;
}
push @text, $self->next_todo;
}
unshift @{$self->{'subs_todo'}}, @pending;
return @text;
}
sub _features_from_bundle {
my ($hints, $hh) = @_;
foreach (@{$feature::feature_bundle{@feature::hint_bundles[$hints >> $feature::hint_shift]}}) {
$hh->{$feature::feature{$_}} = 1;
}
return $hh;
}
# generate any pragmas, 'package foo' etc needed to synchronise
# with the given cop
sub pragmata {
my $self = shift;
my($op) = @_;
my @text;
my $stash = $op->stashpv;
if ($stash ne $self->{'curstash'}) {
push @text, $self->keyword("package") . " $stash;\n";
$self->{'curstash'} = $stash;
}
my $warnings = $op->warnings;
my $warning_bits;
if ($warnings->isa("B::SPECIAL") && $$warnings == 4) {
$warning_bits = $warnings::Bits{"all"} & WARN_MASK;
}
elsif ($warnings->isa("B::SPECIAL") && $$warnings == 5) {
$warning_bits = $warnings::NONE;
}
elsif ($warnings->isa("B::SPECIAL")) {
$warning_bits = undef;
}
else {
$warning_bits = $warnings->PV & WARN_MASK;
}
if (defined ($warning_bits) and
!defined($self->{warnings}) || $self->{'warnings'} ne $warning_bits) {
push @text,
$self->declare_warnings($self->{'warnings'}, $warning_bits);
$self->{'warnings'} = $warning_bits;
}
my $hints = $op->hints;
my $old_hints = $self->{'hints'};
if ($self->{'hints'} != $hints) {
push @text, $self->declare_hints($self->{'hints'}, $hints);
$self->{'hints'} = $hints;
}
my $newhh;
$newhh = $op->hints_hash->HASH;
{
# feature bundle hints
my $from = $old_hints & $feature::hint_mask;
my $to = $ hints & $feature::hint_mask;
if ($from != $to) {
if ($to == $feature::hint_mask) {
if ($self->{'hinthash'}) {
delete $self->{'hinthash'}{$_}
for grep /^feature_/, keys %{$self->{'hinthash'}};
}
else { $self->{'hinthash'} = {} }
$self->{'hinthash'}
= _features_from_bundle($from, $self->{'hinthash'});
}
else {
my $bundle =
$feature::hint_bundles[$to >> $feature::hint_shift];
$bundle =~ s/(\d[13579])\z/$1+1/e; # 5.11 => 5.12
push @text,
$self->keyword("no") . " feature ':all';\n",
$self->keyword("use") . " feature ':$bundle';\n";
}
}
}
{
push @text, $self->declare_hinthash(
$self->{'hinthash'}, $newhh,
$self->{indent_size}, $self->{hints},
);
$self->{'hinthash'} = $newhh;
}
return join("", @text);
}
# Notice how subs and formats are inserted between statements here;
# also $[ assignments and pragmas.
sub pp_nextstate {
my $self = shift;
my($op, $cx) = @_;
$self->{'curcop'} = $op;
my @text;
my @subs = $self->cop_subs($op);
if (@subs) {
# Special marker to swallow up the semicolon
push @subs, "\cK";
}
push @text, @subs;
push @text, $self->pragmata($op);
# This should go after of any branches that add statements, to
# increase the chances that it refers to the same line it did in
# the original program.
if ($self->{'linenums'} && $cx != .5) { # $cx == .5 means in a format
push @text, "\f#line " . $op->line .
' "' . $op->file, qq'"\n';
}
push @text, $op->label . ": " if $op->label;
return join("", @text);
}
sub declare_warnings {
my ($self, $from, $to) = @_;
$from //= '';
my $all = (warnings::bits("all") & WARN_MASK);
unless ((($from & WARN_MASK) & ~$all) =~ /[^\0]/) {
# no FATAL bits need turning off
if ( ($to & WARN_MASK) eq $all) {
return $self->keyword("use") . " warnings;\n";
}
elsif (($to & WARN_MASK) eq ("\0"x length($to) & WARN_MASK)) {
return $self->keyword("no") . " warnings;\n";
}
}
return "BEGIN {\${^WARNING_BITS} = \""
. join("", map { sprintf("\\x%02x", ord $_) } split "", $to)
. "\"}\n\cK";
}
sub declare_hints {
my ($self, $from, $to) = @_;
my $use = $to & ~$from;
my $no = $from & ~$to;
my $decls = "";
for my $pragma (hint_pragmas($use)) {
$decls .= $self->keyword("use") . " $pragma;\n";
}
for my $pragma (hint_pragmas($no)) {
$decls .= $self->keyword("no") . " $pragma;\n";
}
return $decls;
}
# Internal implementation hints that the core sets automatically, so don't need
# (or want) to be passed back to the user
my %ignored_hints = (
'open<' => 1,
'open>' => 1,
':' => 1,
'strict/refs' => 1,
'strict/subs' => 1,
'strict/vars' => 1,
'feature/bits' => 1,
);
my %rev_feature;
sub declare_hinthash {
my ($self, $from, $to, $indent, $hints) = @_;
my $doing_features =
($hints & $feature::hint_mask) == $feature::hint_mask;
my @decls;
my @features;
my @unfeatures; # bugs?
for my $key (sort keys %$to) {
next if $ignored_hints{$key};
my $is_feature = $key =~ /^feature_/;
next if $is_feature and not $doing_features;
if (!exists $from->{$key} or $from->{$key} ne $to->{$key}) {
push(@features, $key), next if $is_feature;
push @decls,
qq(\$^H{) . single_delim("q", "'", $key, $self) . qq(} = )
. (
defined $to->{$key}
? single_delim("q", "'", $to->{$key}, $self)
: 'undef'
)
. qq(;);
}
}
for my $key (sort keys %$from) {
next if $ignored_hints{$key};
my $is_feature = $key =~ /^feature_/;
next if $is_feature and not $doing_features;
if (!exists $to->{$key}) {
push(@unfeatures, $key), next if $is_feature;
push @decls, qq(delete \$^H{'$key'};);
}
}
my @ret;
if (@features || @unfeatures) {
if (!%rev_feature) { %rev_feature = reverse %feature::feature }
}
if (@features) {
push @ret, $self->keyword("use") . " feature "
. join(", ", map "'$rev_feature{$_}'", @features) . ";\n";
}
if (@unfeatures) {
push @ret, $self->keyword("no") . " feature "
. join(", ", map "'$rev_feature{$_}'", @unfeatures)
. ";\n";
}
@decls and
push @ret,
join("\n" . (" " x $indent), "BEGIN {", @decls) . "\n}\n\cK";
return @ret;
}
sub hint_pragmas {
my ($bits) = @_;
my (@pragmas, @strict);
push @pragmas, "integer" if $bits & 0x1;
for (sort keys %strict_bits) {
push @strict, "'$_'" if $bits & $strict_bits{$_};
}
if (@strict == keys %strict_bits) {
push @pragmas, "strict";
}
elsif (@strict) {
push @pragmas, "strict " . join ', ', @strict;
}
push @pragmas, "bytes" if $bits & 0x8;
return @pragmas;
}
sub pp_dbstate { pp_nextstate(@_) }
sub pp_setstate { pp_nextstate(@_) }
sub pp_unstack { return "" } # see also leaveloop
my %feature_keywords = (
# keyword => 'feature',
state => 'state',
say => 'say',
given => 'switch',
when => 'switch',
default => 'switch',
break => 'switch',
evalbytes=>'evalbytes',
__SUB__ => '__SUB__',
fc => 'fc',
);
# keywords that are strong and also have a prototype
#
my %strong_proto_keywords = map { $_ => 1 } qw(
pos
prototype
scalar
study
undef
);
sub feature_enabled {
my($self,$name) = @_;
my $hh;
my $hints = $self->{hints} & $feature::hint_mask;
if ($hints && $hints != $feature::hint_mask) {
$hh = _features_from_bundle($hints);
}
elsif ($hints) { $hh = $self->{'hinthash'} }
return $hh && $hh->{"feature_$feature_keywords{$name}"}
}
sub keyword {
my $self = shift;
my $name = shift;
return $name if $name =~ /^CORE::/; # just in case
if (exists $feature_keywords{$name}) {
return "CORE::$name" if not $self->feature_enabled($name);
}
# This sub may be called for a program that has no nextstate ops. In
# that case we may have a lexical sub named no/use/sub in scope but
# $self->lex_in_scope will return false because it depends on the
# current nextstate op. So we need this alternate method if there is
# no current cop.
if (!$self->{'curcop'}) {
$self->populate_curcvlex() if !defined $self->{'curcvlex'};
return "CORE::$name" if exists $self->{'curcvlex'}{"m&$name"}
|| exists $self->{'curcvlex'}{"o&$name"};
} elsif ($self->lex_in_scope("&$name")
|| $self->lex_in_scope("&$name", 1)) {
return "CORE::$name";
}
if ($strong_proto_keywords{$name}
|| ($name !~ /^(?:chom?p|do|exec|glob|s(?:elect|ystem))\z/
&& !defined eval{prototype "CORE::$name"})
) { return $name }
if (
exists $self->{subs_declared}{$name}
or
exists &{"$self->{curstash}::$name"}
) {
return "CORE::$name"
}
return $name;
}
sub baseop {
my $self = shift;
my($op, $cx, $name) = @_;
return $self->keyword($name);
}
sub pp_stub { "()" }
sub pp_wantarray { baseop(@_, "wantarray") }
sub pp_fork { baseop(@_, "fork") }
sub pp_wait { maybe_targmy(@_, \&baseop, "wait") }
sub pp_getppid { maybe_targmy(@_, \&baseop, "getppid") }
sub pp_time { maybe_targmy(@_, \&baseop, "time") }
sub pp_tms { baseop(@_, "times") }
sub pp_ghostent { baseop(@_, "gethostent") }
sub pp_gnetent { baseop(@_, "getnetent") }
sub pp_gprotoent { baseop(@_, "getprotoent") }
sub pp_gservent { baseop(@_, "getservent") }
sub pp_ehostent { baseop(@_, "endhostent") }
sub pp_enetent { baseop(@_, "endnetent") }
sub pp_eprotoent { baseop(@_, "endprotoent") }
sub pp_eservent { baseop(@_, "endservent") }
sub pp_gpwent { baseop(@_, "getpwent") }
sub pp_spwent { baseop(@_, "setpwent") }
sub pp_epwent { baseop(@_, "endpwent") }
sub pp_ggrent { baseop(@_, "getgrent") }
sub pp_sgrent { baseop(@_, "setgrent") }
sub pp_egrent { baseop(@_, "endgrent") }
sub pp_getlogin { baseop(@_, "getlogin") }
sub POSTFIX () { 1 }
# I couldn't think of a good short name, but this is the category of
# symbolic unary operators with interesting precedence
sub pfixop {
my $self = shift;
my($op, $cx, $name, $prec, $flags) = (@_, 0);
my $kid = $op->first;
$kid = $self->deparse($kid, $prec);
return $self->maybe_parens(($flags & POSTFIX)
? "$kid$name"
# avoid confusion with filetests
: $name eq '-'
&& $kid =~ /^[a-zA-Z](?!\w)/
? "$name($kid)"
: "$name$kid",
$cx, $prec);
}
sub pp_preinc { pfixop(@_, "++", 23) }
sub pp_predec { pfixop(@_, "--", 23) }
sub pp_postinc { maybe_targmy(@_, \&pfixop, "++", 23, POSTFIX) }
sub pp_postdec { maybe_targmy(@_, \&pfixop, "--", 23, POSTFIX) }
sub pp_i_preinc { pfixop(@_, "++", 23) }
sub pp_i_predec { pfixop(@_, "--", 23) }
sub pp_i_postinc { maybe_targmy(@_, \&pfixop, "++", 23, POSTFIX) }
sub pp_i_postdec { maybe_targmy(@_, \&pfixop, "--", 23, POSTFIX) }
sub pp_complement { maybe_targmy(@_, \&pfixop, "~", 21) }
*pp_ncomplement = *pp_complement;
sub pp_scomplement { maybe_targmy(@_, \&pfixop, "~.", 21) }
sub pp_negate { maybe_targmy(@_, \&real_negate) }
sub real_negate {
my $self = shift;
my($op, $cx) = @_;
if ($op->first->name =~ /^(i_)?negate$/) {
# avoid --$x
$self->pfixop($op, $cx, "-", 21.5);
} else {
$self->pfixop($op, $cx, "-", 21);
}
}
sub pp_i_negate { pp_negate(@_) }
sub pp_not {
my $self = shift;
my($op, $cx) = @_;
if ($cx <= 4) {
$self->listop($op, $cx, "not", $op->first);
} else {
$self->pfixop($op, $cx, "!", 21);
}
}
sub unop {
my $self = shift;
my($op, $cx, $name, $nollafr) = @_;
my $kid;
if ($op->flags & OPf_KIDS) {
$kid = $op->first;
if (not $name) {
# this deals with 'boolkeys' right now
return $self->deparse($kid,$cx);
}
my $builtinname = $name;
$builtinname =~ /^CORE::/ or $builtinname = "CORE::$name";
if (defined prototype($builtinname)
&& $builtinname ne 'CORE::readline'
&& prototype($builtinname) =~ /^;?\*/
&& $kid->name eq "rv2gv") {
$kid = $kid->first;
}
if ($nollafr) {
if (($kid = $self->deparse($kid, 16)) !~ s/^\cS//) {
# require foo() is a syntax error.
$kid =~ /^(?!\d)\w/ and $kid = "($kid)";
}
return $self->maybe_parens(
$self->keyword($name) . " $kid", $cx, 16
);
}
return $self->maybe_parens_unop($name, $kid, $cx);
} else {
return $self->maybe_parens(
$self->keyword($name) . ($op->flags & OPf_SPECIAL ? "()" : ""),
$cx, 16,
);
}
}
sub pp_chop { maybe_targmy(@_, \&unop, "chop") }
sub pp_chomp { maybe_targmy(@_, \&unop, "chomp") }
sub pp_schop { maybe_targmy(@_, \&unop, "chop") }
sub pp_schomp { maybe_targmy(@_, \&unop, "chomp") }
sub pp_defined { unop(@_, "defined") }
sub pp_undef { unop(@_, "undef") }
sub pp_study { unop(@_, "study") }
sub pp_ref { unop(@_, "ref") }
sub pp_pos { maybe_local(@_, unop(@_, "pos")) }
sub pp_sin { maybe_targmy(@_, \&unop, "sin") }
sub pp_cos { maybe_targmy(@_, \&unop, "cos") }
sub pp_rand { maybe_targmy(@_, \&unop, "rand") }
sub pp_srand { unop(@_, "srand") }
sub pp_exp { maybe_targmy(@_, \&unop, "exp") }
sub pp_log { maybe_targmy(@_, \&unop, "log") }
sub pp_sqrt { maybe_targmy(@_, \&unop, "sqrt") }
sub pp_int { maybe_targmy(@_, \&unop, "int") }
sub pp_hex { maybe_targmy(@_, \&unop, "hex") }
sub pp_oct { maybe_targmy(@_, \&unop, "oct") }
sub pp_abs { maybe_targmy(@_, \&unop, "abs") }
sub pp_length { maybe_targmy(@_, \&unop, "length") }
sub pp_ord { maybe_targmy(@_, \&unop, "ord") }
sub pp_chr { maybe_targmy(@_, \&unop, "chr") }
sub pp_each { unop(@_, "each") }
sub pp_values { unop(@_, "values") }
sub pp_keys { unop(@_, "keys") }
{ no strict 'refs'; *{"pp_r$_"} = *{"pp_$_"} for qw< keys each values >; }
sub pp_boolkeys {
# no name because its an optimisation op that has no keyword
unop(@_,"");
}
sub pp_aeach { unop(@_, "each") }
sub pp_avalues { unop(@_, "values") }
sub pp_akeys { unop(@_, "keys") }
sub pp_pop { unop(@_, "pop") }
sub pp_shift { unop(@_, "shift") }
sub pp_caller { unop(@_, "caller") }
sub pp_reset { unop(@_, "reset") }
sub pp_exit { unop(@_, "exit") }
sub pp_prototype { unop(@_, "prototype") }
sub pp_close { unop(@_, "close") }
sub pp_fileno { unop(@_, "fileno") }
sub pp_umask { unop(@_, "umask") }
sub pp_untie { unop(@_, "untie") }
sub pp_tied { unop(@_, "tied") }
sub pp_dbmclose { unop(@_, "dbmclose") }
sub pp_getc { unop(@_, "getc") }
sub pp_eof { unop(@_, "eof") }
sub pp_tell { unop(@_, "tell") }
sub pp_getsockname { unop(@_, "getsockname") }
sub pp_getpeername { unop(@_, "getpeername") }
sub pp_chdir {
my ($self, $op, $cx) = @_;
if (($op->flags & (OPf_SPECIAL|OPf_KIDS)) == (OPf_SPECIAL|OPf_KIDS)) {
my $kw = $self->keyword("chdir");
my $kid = $self->const_sv($op->first)->PV;
my $code = $kw
. ($cx >= 16 || $self->{'parens'} ? "($kid)" : " $kid");
maybe_targmy(@_, sub { $_[3] }, $code);
} else {
maybe_targmy(@_, \&unop, "chdir")
}
}
sub pp_chroot { maybe_targmy(@_, \&unop, "chroot") }
sub pp_readlink { unop(@_, "readlink") }
sub pp_rmdir { maybe_targmy(@_, \&unop, "rmdir") }
sub pp_readdir { unop(@_, "readdir") }
sub pp_telldir { unop(@_, "telldir") }
sub pp_rewinddir { unop(@_, "rewinddir") }
sub pp_closedir { unop(@_, "closedir") }
sub pp_getpgrp { maybe_targmy(@_, \&unop, "getpgrp") }
sub pp_localtime { unop(@_, "localtime") }
sub pp_gmtime { unop(@_, "gmtime") }
sub pp_alarm { unop(@_, "alarm") }
sub pp_sleep { maybe_targmy(@_, \&unop, "sleep") }
sub pp_dofile {
my $code = unop(@_, "do", 1); # llafr does not apply
if ($code =~ s/^((?:CORE::)?do) \{/$1({/) { $code .= ')' }
$code;
}
sub pp_entereval {
unop(
@_,
$_[1]->private & OPpEVAL_BYTES ? 'evalbytes' : "eval"
)
}
sub pp_ghbyname { unop(@_, "gethostbyname") }
sub pp_gnbyname { unop(@_, "getnetbyname") }
sub pp_gpbyname { unop(@_, "getprotobyname") }
sub pp_shostent { unop(@_, "sethostent") }
sub pp_snetent { unop(@_, "setnetent") }
sub pp_sprotoent { unop(@_, "setprotoent") }
sub pp_sservent { unop(@_, "setservent") }
sub pp_gpwnam { unop(@_, "getpwnam") }
sub pp_gpwuid { unop(@_, "getpwuid") }
sub pp_ggrnam { unop(@_, "getgrnam") }
sub pp_ggrgid { unop(@_, "getgrgid") }
sub pp_lock { unop(@_, "lock") }
sub pp_continue { unop(@_, "continue"); }
sub pp_break { unop(@_, "break"); }
sub givwhen {
my $self = shift;
my($op, $cx, $givwhen) = @_;
my $enterop = $op->first;
my ($head, $block);
if ($enterop->flags & OPf_SPECIAL) {
$head = $self->keyword("default");
$block = $self->deparse($enterop->first, 0);
}
else {
my $cond = $enterop->first;
my $cond_str = $self->deparse($cond, 1);
$head = "$givwhen ($cond_str)";
$block = $self->deparse($cond->sibling, 0);
}
return "$head {\n".
"\t$block\n".
"\b}\cK";
}
sub pp_leavegiven { givwhen(@_, $_[0]->keyword("given")); }
sub pp_leavewhen { givwhen(@_, $_[0]->keyword("when")); }
sub pp_exists {
my $self = shift;
my($op, $cx) = @_;
my $arg;
my $name = $self->keyword("exists");
if ($op->private & OPpEXISTS_SUB) {
# Checking for the existence of a subroutine
return $self->maybe_parens_func($name,
$self->pp_rv2cv($op->first, 16), $cx, 16);
}
if ($op->flags & OPf_SPECIAL) {
# Array element, not hash element
return $self->maybe_parens_func($name,
$self->pp_aelem($op->first, 16), $cx, 16);
}
return $self->maybe_parens_func($name, $self->pp_helem($op->first, 16),
$cx, 16);
}
sub pp_delete {
my $self = shift;
my($op, $cx) = @_;
my $arg;
my $name = $self->keyword("delete");
if ($op->private & (OPpSLICE|OPpKVSLICE)) {
if ($op->flags & OPf_SPECIAL) {
# Deleting from an array, not a hash
return $self->maybe_parens_func($name,
$self->pp_aslice($op->first, 16),
$cx, 16);
}
return $self->maybe_parens_func($name,
$self->pp_hslice($op->first, 16),
$cx, 16);
} else {
if ($op->flags & OPf_SPECIAL) {
# Deleting from an array, not a hash
return $self->maybe_parens_func($name,
$self->pp_aelem($op->first, 16),
$cx, 16);
}
return $self->maybe_parens_func($name,
$self->pp_helem($op->first, 16),
$cx, 16);
}
}
sub pp_require {
my $self = shift;
my($op, $cx) = @_;
my $opname = $op->flags & OPf_SPECIAL ? 'CORE::require' : 'require';
my $kid = $op->first;
if ($kid->name eq 'const') {
my $priv = $kid->private;
my $sv = $self->const_sv($kid);
my $arg;
if ($priv & OPpCONST_BARE) {
$arg = $sv->PV;
$arg =~ s[/][::]g;
$arg =~ s/\.pm//g;
} elsif ($priv & OPpCONST_NOVER) {
$opname = $self->keyword('no');
$arg = $self->const($sv, 16);
} elsif ((my $tmp = $self->const($sv, 16)) =~ /^v/) {
$arg = $tmp;
}
if ($arg) {
return $self->maybe_parens("$opname $arg", $cx, 16);
}
}
$self->unop(
$op, $cx,
$opname,
1, # llafr does not apply
);
}
sub pp_scalar {
my $self = shift;
my($op, $cx) = @_;
my $kid = $op->first;
if (not null $kid->sibling) {
# XXX Was a here-doc
return $self->dquote($op);
}
$self->unop(@_, "scalar");
}
sub padval {
my $self = shift;
my $targ = shift;
return $self->{'curcv'}->PADLIST->ARRAYelt(1)->ARRAYelt($targ);
}
sub anon_hash_or_list {
my $self = shift;
my($op, $cx) = @_;
my($pre, $post) = @{{"anonlist" => ["[","]"],
"anonhash" => ["{","}"]}->{$op->name}};
my($expr, @exprs);
$op = $op->first->sibling; # skip pushmark
for (; !null($op); $op = $op->sibling) {
$expr = $self->deparse($op, 6);
push @exprs, $expr;
}
if ($pre eq "{" and $cx < 1) {
# Disambiguate that it's not a block
$pre = "+{";
}
return $pre . join(", ", @exprs) . $post;
}
sub pp_anonlist {
my $self = shift;
my ($op, $cx) = @_;
if ($op->flags & OPf_SPECIAL) {
return $self->anon_hash_or_list($op, $cx);
}
warn "Unexpected op pp_" . $op->name() . " without OPf_SPECIAL";
return 'XXX';
}
*pp_anonhash = \&pp_anonlist;
sub pp_refgen {
my $self = shift;
my($op, $cx) = @_;
my $kid = $op->first;
if ($kid->name eq "null") {
my $anoncode = $kid = $kid->first;
if ($anoncode->name eq "anonconst") {
$anoncode = $anoncode->first->first->sibling;
}
if ($anoncode->name eq "anoncode"
or !null($anoncode = $kid->sibling) and
$anoncode->name eq "anoncode") {
return $self->e_anoncode({ code => $self->padval($anoncode->targ) });
} elsif ($kid->name eq "pushmark") {
my $sib_name = $kid->sibling->name;
if ($sib_name eq 'entersub') {
my $text = $self->deparse($kid->sibling, 1);
# Always show parens for \(&func()), but only with -p otherwise
$text = "($text)" if $self->{'parens'}
or $kid->sibling->private & OPpENTERSUB_AMPER;
return "\\$text";
}
}
}
local $self->{'in_refgen'} = 1;
$self->pfixop($op, $cx, "\\", 20);
}
sub e_anoncode {
my ($self, $info) = @_;
my $text = $self->deparse_sub($info->{code});
return $self->keyword("sub") . " $text";
}
sub pp_srefgen { pp_refgen(@_) }
sub pp_readline {
my $self = shift;
my($op, $cx) = @_;
my $kid = $op->first;
if (is_scalar($kid)
and $op->flags & OPf_SPECIAL
and $self->deparse($kid, 1) eq 'ARGV')
{
return '<<>>';
}
return $self->unop($op, $cx, "readline");
}
sub pp_rcatline {
my $self = shift;
my($op) = @_;
return "<" . $self->gv_name($self->gv_or_padgv($op)) . ">";
}
# Unary operators that can occur as pseudo-listops inside double quotes
sub dq_unop {
my $self = shift;
my($op, $cx, $name, $prec, $flags) = (@_, 0, 0);
my $kid;
if ($op->flags & OPf_KIDS) {
$kid = $op->first;
# If there's more than one kid, the first is an ex-pushmark.
$kid = $kid->sibling if not null $kid->sibling;
return $self->maybe_parens_unop($name, $kid, $cx);
} else {
return $name . ($op->flags & OPf_SPECIAL ? "()" : "");
}
}
sub pp_ucfirst { dq_unop(@_, "ucfirst") }
sub pp_lcfirst { dq_unop(@_, "lcfirst") }
sub pp_uc { dq_unop(@_, "uc") }
sub pp_lc { dq_unop(@_, "lc") }
sub pp_quotemeta { maybe_targmy(@_, \&dq_unop, "quotemeta") }
sub pp_fc { dq_unop(@_, "fc") }
sub loopex {
my $self = shift;
my ($op, $cx, $name) = @_;
if (class($op) eq "PVOP") {
$name .= " " . $op->pv;
} elsif (class($op) eq "OP") {
# no-op
} elsif (class($op) eq "UNOP") {
(my $kid = $self->deparse($op->first, 7)) =~ s/^\cS//;
# last foo() is a syntax error.
$kid =~ /^(?!\d)\w/ and $kid = "($kid)";
$name .= " $kid";
}
return $self->maybe_parens($name, $cx, 7);
}
sub pp_last { loopex(@_, "last") }
sub pp_next { loopex(@_, "next") }
sub pp_redo { loopex(@_, "redo") }
sub pp_goto { loopex(@_, "goto") }
sub pp_dump { loopex(@_, "CORE::dump") }
sub ftst {
my $self = shift;
my($op, $cx, $name) = @_;
if (class($op) eq "UNOP") {
# Genuine '-X' filetests are exempt from the LLAFR, but not
# l?stat()
if ($name =~ /^-/) {
(my $kid = $self->deparse($op->first, 16)) =~ s/^\cS//;
return $self->maybe_parens("$name $kid", $cx, 16);
}
return $self->maybe_parens_unop($name, $op->first, $cx);
} elsif (class($op) =~ /^(SV|PAD)OP$/) {
return $self->maybe_parens_func($name, $self->pp_gv($op, 1), $cx, 16);
} else { # I don't think baseop filetests ever survive ck_ftst, but...
return $name;
}
}
sub pp_lstat { ftst(@_, "lstat") }
sub pp_stat { ftst(@_, "stat") }
sub pp_ftrread { ftst(@_, "-R") }
sub pp_ftrwrite { ftst(@_, "-W") }
sub pp_ftrexec { ftst(@_, "-X") }
sub pp_fteread { ftst(@_, "-r") }
sub pp_ftewrite { ftst(@_, "-w") }
sub pp_fteexec { ftst(@_, "-x") }
sub pp_ftis { ftst(@_, "-e") }
sub pp_fteowned { ftst(@_, "-O") }
sub pp_ftrowned { ftst(@_, "-o") }
sub pp_ftzero { ftst(@_, "-z") }
sub pp_ftsize { ftst(@_, "-s") }
sub pp_ftmtime { ftst(@_, "-M") }
sub pp_ftatime { ftst(@_, "-A") }
sub pp_ftctime { ftst(@_, "-C") }
sub pp_ftsock { ftst(@_, "-S") }
sub pp_ftchr { ftst(@_, "-c") }
sub pp_ftblk { ftst(@_, "-b") }
sub pp_ftfile { ftst(@_, "-f") }
sub pp_ftdir { ftst(@_, "-d") }
sub pp_ftpipe { ftst(@_, "-p") }
sub pp_ftlink { ftst(@_, "-l") }
sub pp_ftsuid { ftst(@_, "-u") }
sub pp_ftsgid { ftst(@_, "-g") }
sub pp_ftsvtx { ftst(@_, "-k") }
sub pp_fttty { ftst(@_, "-t") }
sub pp_fttext { ftst(@_, "-T") }
sub pp_ftbinary { ftst(@_, "-B") }
sub SWAP_CHILDREN () { 1 }
sub ASSIGN () { 2 } # has OP= variant
sub LIST_CONTEXT () { 4 } # Assignment is in list context
my(%left, %right);
sub assoc_class {
my $op = shift;
my $name = $op->name;
if ($name eq "concat" and $op->first->name eq "concat") {
# avoid spurious '=' -- see comment in pp_concat
return "concat";
}
if ($name eq "null" and class($op) eq "UNOP"
and $op->first->name =~ /^(and|x?or)$/
and null $op->first->sibling)
{
# Like all conditional constructs, OP_ANDs and OP_ORs are topped
# with a null that's used as the common end point of the two
# flows of control. For precedence purposes, ignore it.
# (COND_EXPRs have these too, but we don't bother with
# their associativity).
return assoc_class($op->first);
}
return $name . ($op->flags & OPf_STACKED ? "=" : "");
}
# Left associative operators, like '+', for which
# $a + $b + $c is equivalent to ($a + $b) + $c
BEGIN {
%left = ('multiply' => 19, 'i_multiply' => 19,
'divide' => 19, 'i_divide' => 19,
'modulo' => 19, 'i_modulo' => 19,
'repeat' => 19,
'add' => 18, 'i_add' => 18,
'subtract' => 18, 'i_subtract' => 18,
'concat' => 18,
'left_shift' => 17, 'right_shift' => 17,
'bit_and' => 13, 'nbit_and' => 13, 'sbit_and' => 13,
'bit_or' => 12, 'bit_xor' => 12,
'sbit_or' => 12, 'sbit_xor' => 12,
'nbit_or' => 12, 'nbit_xor' => 12,
'and' => 3,
'or' => 2, 'xor' => 2,
);
}
sub deparse_binop_left {
my $self = shift;
my($op, $left, $prec) = @_;
if ($left{assoc_class($op)} && $left{assoc_class($left)}
and $left{assoc_class($op)} == $left{assoc_class($left)})
{
return $self->deparse($left, $prec - .00001);
} else {
return $self->deparse($left, $prec);
}
}
# Right associative operators, like '=', for which
# $a = $b = $c is equivalent to $a = ($b = $c)
BEGIN {
%right = ('pow' => 22,
'sassign=' => 7, 'aassign=' => 7,
'multiply=' => 7, 'i_multiply=' => 7,
'divide=' => 7, 'i_divide=' => 7,
'modulo=' => 7, 'i_modulo=' => 7,
'repeat=' => 7, 'refassign' => 7, 'refassign=' => 7,
'add=' => 7, 'i_add=' => 7,
'subtract=' => 7, 'i_subtract=' => 7,
'concat=' => 7,
'left_shift=' => 7, 'right_shift=' => 7,
'bit_and=' => 7, 'sbit_and=' => 7, 'nbit_and=' => 7,
'nbit_or=' => 7, 'nbit_xor=' => 7,
'sbit_or=' => 7, 'sbit_xor=' => 7,
'andassign' => 7,
'orassign' => 7,
);
}
sub deparse_binop_right {
my $self = shift;
my($op, $right, $prec) = @_;
if ($right{assoc_class($op)} && $right{assoc_class($right)}
and $right{assoc_class($op)} == $right{assoc_class($right)})
{
return $self->deparse($right, $prec - .00001);
} else {
return $self->deparse($right, $prec);
}
}
sub binop {
my $self = shift;
my ($op, $cx, $opname, $prec, $flags) = (@_, 0);
my $left = $op->first;
my $right = $op->last;
my $eq = "";
if ($op->flags & OPf_STACKED && $flags & ASSIGN) {
$eq = "=";
$prec = 7;
}
if ($flags & SWAP_CHILDREN) {
($left, $right) = ($right, $left);
}
my $leftop = $left;
$left = $self->deparse_binop_left($op, $left, $prec);
$left = "($left)" if $flags & LIST_CONTEXT
and $left !~ /^(my|our|local|state|)\s*[\@%\(]/
|| do {
# Parenthesize if the left argument is a
# lone repeat op.
my $left = $leftop->first->sibling;
$left->name eq 'repeat'
&& null($left->sibling);
};
$right = $self->deparse_binop_right($op, $right, $prec);
return $self->maybe_parens("$left $opname$eq $right", $cx, $prec);
}
sub pp_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) }
sub pp_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) }
sub pp_subtract { maybe_targmy(@_, \&binop, "-",18, ASSIGN) }
sub pp_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) }
sub pp_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) }
sub pp_i_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) }
sub pp_i_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) }
sub pp_i_subtract { maybe_targmy(@_, \&binop, "-", 18, ASSIGN) }
sub pp_i_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) }
sub pp_i_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) }
sub pp_pow { maybe_targmy(@_, \&binop, "**", 22, ASSIGN) }
sub pp_left_shift { maybe_targmy(@_, \&binop, "<<", 17, ASSIGN) }
sub pp_right_shift { maybe_targmy(@_, \&binop, ">>", 17, ASSIGN) }
sub pp_bit_and { maybe_targmy(@_, \&binop, "&", 13, ASSIGN) }
sub pp_bit_or { maybe_targmy(@_, \&binop, "|", 12, ASSIGN) }
sub pp_bit_xor { maybe_targmy(@_, \&binop, "^", 12, ASSIGN) }
*pp_nbit_and = *pp_bit_and;
*pp_nbit_or = *pp_bit_or;
*pp_nbit_xor = *pp_bit_xor;
sub pp_sbit_and { maybe_targmy(@_, \&binop, "&.", 13, ASSIGN) }
sub pp_sbit_or { maybe_targmy(@_, \&binop, "|.", 12, ASSIGN) }
sub pp_sbit_xor { maybe_targmy(@_, \&binop, "^.", 12, ASSIGN) }
sub pp_eq { binop(@_, "==", 14) }
sub pp_ne { binop(@_, "!=", 14) }
sub pp_lt { binop(@_, "<", 15) }
sub pp_gt { binop(@_, ">", 15) }
sub pp_ge { binop(@_, ">=", 15) }
sub pp_le { binop(@_, "<=", 15) }
sub pp_ncmp { binop(@_, "<=>", 14) }
sub pp_i_eq { binop(@_, "==", 14) }
sub pp_i_ne { binop(@_, "!=", 14) }
sub pp_i_lt { binop(@_, "<", 15) }
sub pp_i_gt { binop(@_, ">", 15) }
sub pp_i_ge { binop(@_, ">=", 15) }
sub pp_i_le { binop(@_, "<=", 15) }
sub pp_i_ncmp { maybe_targmy(@_, \&binop, "<=>", 14) }
sub pp_seq { binop(@_, "eq", 14) }
sub pp_sne { binop(@_, "ne", 14) }
sub pp_slt { binop(@_, "lt", 15) }
sub pp_sgt { binop(@_, "gt", 15) }
sub pp_sge { binop(@_, "ge", 15) }
sub pp_sle { binop(@_, "le", 15) }
sub pp_scmp { maybe_targmy(@_, \&binop, "cmp", 14) }
sub pp_isa { binop(@_, "isa", 15) }
sub pp_sassign { binop(@_, "=", 7, SWAP_CHILDREN) }
sub pp_aassign { binop(@_, "=", 7, SWAP_CHILDREN | LIST_CONTEXT) }
sub pp_smartmatch {
my ($self, $op, $cx) = @_;
if (($op->flags & OPf_SPECIAL) && $self->{expand} < 2) {
return $self->deparse($op->last, $cx);
}
else {
binop(@_, "~~", 14);
}
}
# '.' is special because concats-of-concats are optimized to save copying
# by making all but the first concat stacked. The effect is as if the
# programmer had written '($a . $b) .= $c', except legal.
sub pp_concat { maybe_targmy(@_, \&real_concat) }
sub real_concat {
my $self = shift;
my($op, $cx) = @_;
my $left = $op->first;
my $right = $op->last;
my $eq = "";
my $prec = 18;
if (($op->flags & OPf_STACKED) and !($op->private & OPpCONCAT_NESTED)) {
# '.=' rather than optimised '.'
$eq = "=";
$prec = 7;
}
$left = $self->deparse_binop_left($op, $left, $prec);
$right = $self->deparse_binop_right($op, $right, $prec);
return $self->maybe_parens("$left .$eq $right", $cx, $prec);
}
sub pp_repeat { maybe_targmy(@_, \&repeat) }
# 'x' is weird when the left arg is a list
sub repeat {
my $self = shift;
my($op, $cx) = @_;
my $left = $op->first;
my $right = $op->last;
my $eq = "";
my $prec = 19;
if ($op->flags & OPf_STACKED) {
$eq = "=";
$prec = 7;
}
if (null($right)) { # list repeat; count is inside left-side ex-list
# in 5.21.5 and earlier
my $kid = $left->first->sibling; # skip pushmark
my @exprs;
for (; !null($kid->sibling); $kid = $kid->sibling) {
push @exprs, $self->deparse($kid, 6);
}
$right = $kid;
$left = "(" . join(", ", @exprs). ")";
} else {
my $dolist = $op->private & OPpREPEAT_DOLIST;
$left = $self->deparse_binop_left($op, $left, $dolist ? 1 : $prec);
if ($dolist) {
$left = "($left)";
}
}
$right = $self->deparse_binop_right($op, $right, $prec);
return $self->maybe_parens("$left x$eq $right", $cx, $prec);
}
sub range {
my $self = shift;
my ($op, $cx, $type) = @_;
my $left = $op->first;
my $right = $left->sibling;
$left = $self->deparse($left, 9);
$right = $self->deparse($right, 9);
return $self->maybe_parens("$left $type $right", $cx, 9);
}
sub pp_flop {
my $self = shift;
my($op, $cx) = @_;
my $flip = $op->first;
my $type = ($flip->flags & OPf_SPECIAL) ? "..." : "..";
return $self->range($flip->first, $cx, $type);
}
# one-line while/until is handled in pp_leave
sub logop {
my $self = shift;
my ($op, $cx, $lowop, $lowprec, $highop, $highprec, $blockname) = @_;
my $left = $op->first;
my $right = $op->first->sibling;
$blockname &&= $self->keyword($blockname);
if ($cx < 1 and is_scope($right) and $blockname
and $self->{'expand'} < 7)
{ # if ($a) {$b}
$left = $self->deparse($left, 1);
$right = $self->deparse($right, 0);
return "$blockname ($left) {\n\t$right\n\b}\cK";
} elsif ($cx < 1 and $blockname and not $self->{'parens'}
and $self->{'expand'} < 7) { # $b if $a
$right = $self->deparse($right, 1);
$left = $self->deparse($left, 1);
return "$right $blockname $left";
} elsif ($cx > $lowprec and $highop) { # $a && $b
$left = $self->deparse_binop_left($op, $left, $highprec);
$right = $self->deparse_binop_right($op, $right, $highprec);
return $self->maybe_parens("$left $highop $right", $cx, $highprec);
} else { # $a and $b
$left = $self->deparse_binop_left($op, $left, $lowprec);
$right = $self->deparse_binop_right($op, $right, $lowprec);
return $self->maybe_parens("$left $lowop $right", $cx, $lowprec);
}
}
sub pp_and { logop(@_, "and", 3, "&&", 11, "if") }
sub pp_or { logop(@_, "or", 2, "||", 10, "unless") }
sub pp_dor { logop(@_, "//", 10) }
# xor is syntactically a logop, but it's really a binop (contrary to
# old versions of opcode.pl). Syntax is what matters here.
sub pp_xor { logop(@_, "xor", 2, "", 0, "") }
sub logassignop {
my $self = shift;
my ($op, $cx, $opname) = @_;
my $left = $op->first;
my $right = $op->first->sibling->first; # skip sassign
$left = $self->deparse($left, 7);
$right = $self->deparse($right, 7);
return $self->maybe_parens("$left $opname $right", $cx, 7);
}
sub pp_andassign { logassignop(@_, "&&=") }
sub pp_orassign { logassignop(@_, "||=") }
sub pp_dorassign { logassignop(@_, "//=") }
my %cmpchain_cmpops = (
eq => ["==", 14],
i_eq => ["==", 14],
ne => ["!=", 14],
i_ne => ["!=", 14],
seq => ["eq", 14],
sne => ["ne", 14],
lt => ["<", 15],
i_lt => ["<", 15],
gt => [">", 15],
i_gt => [">", 15],
le => ["<=", 15],
i_le => ["<=", 15],
ge => [">=", 15],
i_ge => [">=", 15],
slt => ["lt", 15],
sgt => ["gt", 15],
sle => ["le", 15],
sge => ["ge", 15],
);
sub pp_cmpchain_and {
my($self, $op, $cx) = @_;
my($prec, $dep);
while(1) {
my($thiscmp, $rightcond);
if($op->name eq "cmpchain_and") {
$thiscmp = $op->first;
$rightcond = $thiscmp->sibling;
} else {
$thiscmp = $op;
}
my $thiscmptype = $cmpchain_cmpops{$thiscmp->name} // (return "XXX");
if(defined $prec) {
$thiscmptype->[1] == $prec or return "XXX";
$thiscmp->first->name eq "null" &&
!($thiscmp->first->flags & OPf_KIDS)
or return "XXX";
} else {
$prec = $thiscmptype->[1];
$dep = $self->deparse($thiscmp->first, $prec);
}
$dep .= " ".$thiscmptype->[0]." ";
my $operand = $thiscmp->last;
if(defined $rightcond) {
$operand->name eq "cmpchain_dup" or return "XXX";
$operand = $operand->first;
}
$dep .= $self->deparse($operand, $prec);
last unless defined $rightcond;
if($rightcond->name eq "null" && ($rightcond->flags & OPf_KIDS) &&
$rightcond->first->name eq "cmpchain_and") {
$rightcond = $rightcond->first;
}
$op = $rightcond;
}
return $self->maybe_parens($dep, $cx, $prec);
}
sub rv2gv_or_string {
my($self,$op) = @_;
if ($op->name eq "gv") { # could be open("open") or open("###")
my($name,$quoted) =
$self->stash_variable_name("", $self->gv_or_padgv($op));
$quoted ? $name : "*$name";
}
else {
$self->deparse($op, 6);
}
}
sub listop {
my $self = shift;
my($op, $cx, $name, $kid, $nollafr) = @_;
my(@exprs);
my $parens = ($cx >= 5) || $self->{'parens'};
$kid ||= $op->first->sibling;
# If there are no arguments, add final parentheses (or parenthesize the
# whole thing if the llafr does not apply) to account for cases like
# (return)+1 or setpgrp()+1. When the llafr does not apply, we use a
# precedence of 6 (< comma), as "return, 1" does not need parentheses.
if (null $kid) {
return $nollafr
? $self->maybe_parens($self->keyword($name), $cx, 7)
: $self->keyword($name) . '()' x (7 < $cx);
}
my $first;
my $fullname = $self->keyword($name);
my $proto = prototype("CORE::$name");
if (
( (defined $proto && $proto =~ /^;?\*/)
|| $name eq 'select' # select(F) doesn't have a proto
)
&& $kid->name eq "rv2gv"
&& !($kid->private & OPpLVAL_INTRO)
) {
$first = $self->rv2gv_or_string($kid->first);
}
else {
$first = $self->deparse($kid, 6);
}
if ($name eq "chmod" && $first =~ /^\d+$/) {
$first = sprintf("%#o", $first);
}
$first = "+$first"
if not $parens and not $nollafr and substr($first, 0, 1) eq "(";
push @exprs, $first;
$kid = $kid->sibling;
if (defined $proto && $proto =~ /^\*\*/ && $kid->name eq "rv2gv"
&& !($kid->private & OPpLVAL_INTRO)) {
push @exprs, $first = $self->rv2gv_or_string($kid->first);
$kid = $kid->sibling;
}
for (; !null($kid); $kid = $kid->sibling) {
push @exprs, $self->deparse($kid, 6);
}
if ($name eq "reverse" && ($op->private & OPpREVERSE_INPLACE)) {
return "$exprs[0] = $fullname"
. ($parens ? "($exprs[0])" : " $exprs[0]");
}
if ($parens && $nollafr) {
return "($fullname " . join(", ", @exprs) . ")";
} elsif ($parens) {
return "$fullname(" . join(", ", @exprs) . ")";
} else {
return "$fullname " . join(", ", @exprs);
}
}
sub pp_bless { listop(@_, "bless") }
sub pp_atan2 { maybe_targmy(@_, \&listop, "atan2") }
sub pp_substr {
my ($self,$op,$cx) = @_;
if ($op->private & OPpSUBSTR_REPL_FIRST) {
return
listop($self, $op, 7, "substr", $op->first->sibling->sibling)
. " = "
. $self->deparse($op->first->sibling, 7);
}
maybe_local(@_, listop(@_, "substr"))
}
sub pp_index {
# Also handles pp_rindex.
#
# The body of this function includes an unrolled maybe_targmy(),
# since the two parts of that sub's actions need to have have the
# '== -1' bit in between
my($self, $op, $cx) = @_;
my $lex = ($op->private & OPpTARGET_MY);
my $bool = ($op->private & OPpTRUEBOOL);
my $val = $self->listop($op, ($bool ? 14 : $lex ? 7 : $cx), $op->name);
# (index() == -1) has op_eq and op_const optimised away
if ($bool) {
$val .= ($op->private & OPpINDEX_BOOLNEG) ? " == -1" : " != -1";
$val = "($val)" if ($op->flags & OPf_PARENS);
}
if ($lex) {
my $var = $self->padname($op->targ);
$val = $self->maybe_parens("$var = $val", $cx, 7);
}
$val;
}
sub pp_rindex { pp_index(@_); }
sub pp_vec { maybe_targmy(@_, \&maybe_local, listop(@_, "vec")) }
sub pp_sprintf { maybe_targmy(@_, \&listop, "sprintf") }
sub pp_formline { listop(@_, "formline") } # see also deparse_format
sub pp_crypt { maybe_targmy(@_, \&listop, "crypt") }
sub pp_unpack { listop(@_, "unpack") }
sub pp_pack { listop(@_, "pack") }
sub pp_join { maybe_targmy(@_, \&listop, "join") }
sub pp_splice { listop(@_, "splice") }
sub pp_push { maybe_targmy(@_, \&listop, "push") }
sub pp_unshift { maybe_targmy(@_, \&listop, "unshift") }
sub pp_reverse { listop(@_, "reverse") }
sub pp_warn { listop(@_, "warn") }
sub pp_die { listop(@_, "die") }
sub pp_return { listop(@_, "return", undef, 1) } # llafr does not apply
sub pp_open { listop(@_, "open") }
sub pp_pipe_op { listop(@_, "pipe") }
sub pp_tie { listop(@_, "tie") }
sub pp_binmode { listop(@_, "binmode") }
sub pp_dbmopen { listop(@_, "dbmopen") }
sub pp_sselect { listop(@_, "select") }
sub pp_select { listop(@_, "select") }
sub pp_read { listop(@_, "read") }
sub pp_sysopen { listop(@_, "sysopen") }
sub pp_sysseek { listop(@_, "sysseek") }
sub pp_sysread { listop(@_, "sysread") }
sub pp_syswrite { listop(@_, "syswrite") }
sub pp_send { listop(@_, "send") }
sub pp_recv { listop(@_, "recv") }
sub pp_seek { listop(@_, "seek") }
sub pp_fcntl { listop(@_, "fcntl") }
sub pp_ioctl { listop(@_, "ioctl") }
sub pp_flock { maybe_targmy(@_, \&listop, "flock") }
sub pp_socket { listop(@_, "socket") }
sub pp_sockpair { listop(@_, "socketpair") }
sub pp_bind { listop(@_, "bind") }
sub pp_connect { listop(@_, "connect") }
sub pp_listen { listop(@_, "listen") }
sub pp_accept { listop(@_, "accept") }
sub pp_shutdown { listop(@_, "shutdown") }
sub pp_gsockopt { listop(@_, "getsockopt") }
sub pp_ssockopt { listop(@_, "setsockopt") }
sub pp_chown { maybe_targmy(@_, \&listop, "chown") }
sub pp_unlink { maybe_targmy(@_, \&listop, "unlink") }
sub pp_chmod { maybe_targmy(@_, \&listop, "chmod") }
sub pp_utime { maybe_targmy(@_, \&listop, "utime") }
sub pp_rename { maybe_targmy(@_, \&listop, "rename") }
sub pp_link { maybe_targmy(@_, \&listop, "link") }
sub pp_symlink { maybe_targmy(@_, \&listop, "symlink") }
sub pp_mkdir { maybe_targmy(@_, \&listop, "mkdir") }
sub pp_open_dir { listop(@_, "opendir") }
sub pp_seekdir { listop(@_, "seekdir") }
sub pp_waitpid { maybe_targmy(@_, \&listop, "waitpid") }
sub pp_system { maybe_targmy(@_, \&indirop, "system") }
sub pp_exec { maybe_targmy(@_, \&indirop, "exec") }
sub pp_kill { maybe_targmy(@_, \&listop, "kill") }
sub pp_setpgrp { maybe_targmy(@_, \&listop, "setpgrp") }
sub pp_getpriority { maybe_targmy(@_, \&listop, "getpriority") }
sub pp_setpriority { maybe_targmy(@_, \&listop, "setpriority") }
sub pp_shmget { listop(@_, "shmget") }
sub pp_shmctl { listop(@_, "shmctl") }
sub pp_shmread { listop(@_, "shmread") }
sub pp_shmwrite { listop(@_, "shmwrite") }
sub pp_msgget { listop(@_, "msgget") }
sub pp_msgctl { listop(@_, "msgctl") }
sub pp_msgsnd { listop(@_, "msgsnd") }
sub pp_msgrcv { listop(@_, "msgrcv") }
sub pp_semget { listop(@_, "semget") }
sub pp_semctl { listop(@_, "semctl") }
sub pp_semop { listop(@_, "semop") }
sub pp_ghbyaddr { listop(@_, "gethostbyaddr") }
sub pp_gnbyaddr { listop(@_, "getnetbyaddr") }
sub pp_gpbynumber { listop(@_, "getprotobynumber") }
sub pp_gsbyname { listop(@_, "getservbyname") }
sub pp_gsbyport { listop(@_, "getservbyport") }
sub pp_syscall { listop(@_, "syscall") }
sub pp_glob {
my $self = shift;
my($op, $cx) = @_;
my $kid = $op->first->sibling; # skip pushmark
my $keyword =
$op->flags & OPf_SPECIAL ? 'glob' : $self->keyword('glob');
my $text = $self->deparse($kid, $cx);
return $cx >= 5 || $self->{'parens'}
? "$keyword($text)"
: "$keyword $text";
}
# Truncate is special because OPf_SPECIAL makes a bareword first arg
# be a filehandle. This could probably be better fixed in the core
# by moving the GV lookup into ck_truc.
sub pp_truncate {
my $self = shift;
my($op, $cx) = @_;
my(@exprs);
my $parens = ($cx >= 5) || $self->{'parens'};
my $kid = $op->first->sibling;
my $fh;
if ($op->flags & OPf_SPECIAL) {
# $kid is an OP_CONST
$fh = $self->const_sv($kid)->PV;
} else {
$fh = $self->deparse($kid, 6);
$fh = "+$fh" if not $parens and substr($fh, 0, 1) eq "(";
}
my $len = $self->deparse($kid->sibling, 6);
my $name = $self->keyword('truncate');
if ($parens) {
return "$name($fh, $len)";
} else {
return "$name $fh, $len";
}
}
sub indirop {
my $self = shift;
my($op, $cx, $name) = @_;
my($expr, @exprs);
my $firstkid = my $kid = $op->first->sibling;
my $indir = "";
if ($op->flags & OPf_STACKED) {
$indir = $kid;
$indir = $indir->first; # skip rv2gv
if (is_scope($indir)) {
$indir = "{" . $self->deparse($indir, 0) . "}";
$indir = "{;}" if $indir eq "{}";
} elsif ($indir->name eq "const" && $indir->private & OPpCONST_BARE) {
$indir = $self->const_sv($indir)->PV;
} else {
$indir = $self->deparse($indir, 24);
}
$indir = $indir . " ";
$kid = $kid->sibling;
}
if ($name eq "sort" && $op->private & (OPpSORT_NUMERIC | OPpSORT_INTEGER)) {
$indir = ($op->private & OPpSORT_DESCEND) ? '{$b <=> $a} '
: '{$a <=> $b} ';
}
elsif ($name eq "sort" && $op->private & OPpSORT_DESCEND) {
$indir = '{$b cmp $a} ';
}
for (; !null($kid); $kid = $kid->sibling) {
$expr = $self->deparse($kid, !$indir && $kid == $firstkid && $name eq "sort" && $firstkid->name eq "entersub" ? 16 : 6);
push @exprs, $expr;
}
my $name2;
if ($name eq "sort" && $op->private & OPpSORT_REVERSE) {
$name2 = $self->keyword('reverse') . ' ' . $self->keyword('sort');
}
else { $name2 = $self->keyword($name) }
if ($name eq "sort" && ($op->private & OPpSORT_INPLACE)) {
return "$exprs[0] = $name2 $indir $exprs[0]";
}
my $args = $indir . join(", ", @exprs);
if ($indir ne "" && $name eq "sort") {
# We don't want to say "sort(f 1, 2, 3)", since perl -w will
# give bareword warnings in that case. Therefore if context
# requires, we'll put parens around the outside "(sort f 1, 2,
# 3)". Unfortunately, we'll currently think the parens are
# necessary more often that they really are, because we don't
# distinguish which side of an assignment we're on.
if ($cx >= 5) {
return "($name2 $args)";
} else {
return "$name2 $args";
}
} elsif (
!$indir && $name eq "sort"
&& !null($op->first->sibling)
&& $op->first->sibling->name eq 'entersub'
) {
# We cannot say sort foo(bar), as foo will be interpreted as a
# comparison routine. We have to say sort(...) in that case.
return "$name2($args)";
} else {
return length $args
? $self->maybe_parens_func($name2, $args, $cx, 5)
: $name2 . '()' x (7 < $cx);
}
}
sub pp_prtf { indirop(@_, "printf") }
sub pp_print { indirop(@_, "print") }
sub pp_say { indirop(@_, "say") }
sub pp_sort { indirop(@_, "sort") }
sub mapop {
my $self = shift;
my($op, $cx, $name) = @_;
my($expr, @exprs);
my $kid = $op->first; # this is the (map|grep)start
$kid = $kid->first->sibling; # skip a pushmark
my $code = $kid->first; # skip a null
if (is_scope $code) {
$code = "{" . $self->deparse($code, 0) . "} ";
} else {
$code = $self->deparse($code, 24);
$code .= ", " if !null($kid->sibling);
}
$kid = $kid->sibling;
for (; !null($kid); $kid = $kid->sibling) {
$expr = $self->deparse($kid, 6);
push @exprs, $expr if defined $expr;
}
return $self->maybe_parens_func($self->keyword($name),
$code . join(", ", @exprs), $cx, 5);
}
sub pp_mapwhile { mapop(@_, "map") }
sub pp_grepwhile { mapop(@_, "grep") }
sub pp_mapstart { baseop(@_, "map") }
sub pp_grepstart { baseop(@_, "grep") }
my %uses_intro;
BEGIN {
@uses_intro{
eval { require B::Op_private }
? @{$B::Op_private::ops_using{OPpLVAL_INTRO}}
: qw(gvsv rv2sv rv2hv rv2gv rv2av aelem helem aslice
hslice delete padsv padav padhv enteriter entersub padrange
pushmark cond_expr refassign list)
} = ();
delete @uses_intro{qw( lvref lvrefslice lvavref entersub )};
}
# Look for a my/state attribute declaration in a list or ex-list.
# Returns undef if not found, 'my($x, @a) :Foo(bar)' etc otherwise.
#
# There are three basic tree structs that are expected:
#
# my $x :foo;
# <1> ex-list vK/LVINTRO ->c
# <0> ex-pushmark v ->3
# <1> entersub[t2] vKRS*/TARG ->b
# ....
# <0> padsv[$x:64,65] vM/LVINTRO ->c
#
# my @a :foo;
# my %h :foo;
#
# <1> ex-list vK ->c
# <0> ex-pushmark v ->3
# <0> padav[@a:64,65] vM/LVINTRO ->4
# <1> entersub[t2] vKRS*/TARG ->c
# ....
#
# my ($x,@a,%h) :foo;
#
# <;> nextstate(main 64 -e:1) v:{ ->3
# <@> list vKP ->w
# <0> pushmark vM/LVINTRO ->4
# <0> padsv[$x:64,65] vM/LVINTRO ->5
# <0> padav[@a:64,65] vM/LVINTRO ->6
# <0> padhv[%h:64,65] vM/LVINTRO ->7
# <1> entersub[t4] vKRS*/TARG ->f
# ....
# <1> entersub[t5] vKRS*/TARG ->n
# ....
# <1> entersub[t6] vKRS*/TARG ->v
# ....
# where the entersub in all cases looks like
# <1> entersub[t2] vKRS*/TARG ->c
# <0> pushmark s ->5
# <$> const[PV "attributes"] sM ->6
# <$> const[PV "main"] sM ->7
# <1> srefgen sKM/1 ->9
# <1> ex-list lKRM ->8
# <0> padsv[@a:64,65] sRM ->8
# <$> const[PV "foo"] sM ->a
# <.> method_named[PV "import"] ->b
sub maybe_var_attr {
my ($self, $op, $cx) = @_;
my $kid = $op->first->sibling; # skip pushmark
return if class($kid) eq 'NULL';
my $lop;
my $type;
# Extract out all the pad ops and entersub ops into
# @padops and @entersubops. Return if anything else seen.
# Also determine what class (if any) all the pad vars belong to
my $class;
my $decl; # 'my' or 'state'
my (@padops, @entersubops);
for ($lop = $kid; !null($lop); $lop = $lop->sibling) {
my $lopname = $lop->name;
my $loppriv = $lop->private;
if ($lopname =~ /^pad[sah]v$/) {
return unless $loppriv & OPpLVAL_INTRO;
my $padname = $self->padname_sv($lop->targ);
my $thisclass = ($padname->FLAGS & SVpad_TYPED)
? $padname->SvSTASH->NAME : 'main';
# all pad vars must be in the same class
$class //= $thisclass;
return unless $thisclass eq $class;
# all pad vars must be the same sort of declaration
# (all my, all state, etc)
my $this = ($loppriv & OPpPAD_STATE) ? 'state' : 'my';
if (defined $decl) {
return unless $this eq $decl;
}
$decl = $this;
push @padops, $lop;
}
elsif ($lopname eq 'entersub') {
push @entersubops, $lop;
}
else {
return;
}
}
return unless @padops && @padops == @entersubops;
# there should be a balance: each padop has a corresponding
# 'attributes'->import() method call, in the same order.
my @varnames;
my $attr_text;
for my $i (0..$#padops) {
my $padop = $padops[$i];
my $esop = $entersubops[$i];
push @varnames, $self->padname($padop->targ);
return unless ($esop->flags & OPf_KIDS);
my $kid = $esop->first;
return unless $kid->type == OP_PUSHMARK;
$kid = $kid->sibling;
return unless $$kid && $kid->type == OP_CONST;
return unless $self->const_sv($kid)->PV eq 'attributes';
$kid = $kid->sibling;
return unless $$kid && $kid->type == OP_CONST; # __PACKAGE__
$kid = $kid->sibling;
return unless $$kid
&& $kid->name eq "srefgen"
&& ($kid->flags & OPf_KIDS)
&& ($kid->first->flags & OPf_KIDS)
&& $kid->first->first->name =~ /^pad[sah]v$/
&& $kid->first->first->targ == $padop->targ;
$kid = $kid->sibling;
my @attr;
while ($$kid) {
last if ($kid->type != OP_CONST);
push @attr, $self->const_sv($kid)->PV;
$kid = $kid->sibling;
}
return unless @attr;
my $thisattr = ":" . join(' ', @attr);
$attr_text //= $thisattr;
# all import calls must have the same list of attributes
return unless $attr_text eq $thisattr;
return unless $kid->name eq 'method_named';
return unless $self->meth_sv($kid)->PV eq 'import';
$kid = $kid->sibling;
return if $$kid;
}
my $res = $decl;
$res .= " $class " if $class ne 'main';
$res .=
(@varnames > 1)
? "(" . join(', ', @varnames) . ')'
: " $varnames[0]";
return "$res $attr_text";
}
sub pp_list {
my $self = shift;
my($op, $cx) = @_;
{
# might be my ($s,@a,%h) :Foo(bar);
my $my_attr = maybe_var_attr($self, $op, $cx);
return $my_attr if defined $my_attr;
}
my($expr, @exprs);
my $kid = $op->first->sibling; # skip pushmark
return '' if class($kid) eq 'NULL';
my $lop;
my $local = "either"; # could be local(...), my(...), state(...) or our(...)
my $type;
for ($lop = $kid; !null($lop); $lop = $lop->sibling) {
my $lopname = $lop->name;
my $loppriv = $lop->private;
my $newtype;
if ($lopname =~ /^pad[ash]v$/ && $loppriv & OPpLVAL_INTRO) {
if ($loppriv & OPpPAD_STATE) { # state()
($local = "", last) if $local !~ /^(?:either|state)$/;
$local = "state";
} else { # my()
($local = "", last) if $local !~ /^(?:either|my)$/;
$local = "my";
}
my $padname = $self->padname_sv($lop->targ);
if ($padname->FLAGS & SVpad_TYPED) {
$newtype = $padname->SvSTASH->NAME;
}
} elsif ($lopname =~ /^(?:gv|rv2)([ash])v$/
&& $loppriv & OPpOUR_INTRO
or $lopname eq "null" && class($lop) eq 'UNOP'
&& $lop->first->name eq "gvsv"
&& $lop->first->private & OPpOUR_INTRO) { # our()
my $newlocal = "local " x !!($loppriv & OPpLVAL_INTRO) . "our";
($local = "", last)
if $local ne 'either' && $local ne $newlocal;
$local = $newlocal;
my $funny = !$1 || $1 eq 's' ? '$' : $1 eq 'a' ? '@' : '%';
if (my $t = $self->find_our_type(
$funny . $self->gv_or_padgv($lop->first)->NAME
)) {
$newtype = $t;
}
} elsif ($lopname ne 'undef'
and !($loppriv & OPpLVAL_INTRO)
|| !exists $uses_intro{$lopname eq 'null'
? substr B::ppname($lop->targ), 3
: $lopname})
{
$local = ""; # or not
last;
} elsif ($lopname ne "undef")
{
# local()
($local = "", last) if $local !~ /^(?:either|local)$/;
$local = "local";
}
if (defined $type && defined $newtype && $newtype ne $type) {
$local = '';
last;
}
$type = $newtype;
}
$local = "" if $local eq "either"; # no point if it's all undefs
$local &&= join ' ', map $self->keyword($_), split / /, $local;
$local .= " $type " if $local && length $type;
return $self->deparse($kid, $cx) if null $kid->sibling and not $local;
for (; !null($kid); $kid = $kid->sibling) {
if ($local) {
if (class($kid) eq "UNOP" and $kid->first->name eq "gvsv") {
$lop = $kid->first;
} else {
$lop = $kid;
}
$self->{'avoid_local'}{$$lop}++;
$expr = $self->deparse($kid, 6);
delete $self->{'avoid_local'}{$$lop};
} else {
$expr = $self->deparse($kid, 6);
}
push @exprs, $expr;
}
if ($local) {
if (@exprs == 1 && ($local eq 'state' || $local eq 'CORE::state')) {
# 'state @a = ...' is legal, while 'state(@a) = ...' currently isn't
return "$local $exprs[0]";
}
return "$local(" . join(", ", @exprs) . ")";
} else {
return $self->maybe_parens( join(", ", @exprs), $cx, 6);
}
}
sub is_ifelse_cont {
my $op = shift;
return ($op->name eq "null" and class($op) eq "UNOP"
and $op->first->name =~ /^(and|cond_expr)$/
and is_scope($op->first->first->sibling));
}
sub pp_cond_expr {
my $self = shift;
my($op, $cx) = @_;
my $cond = $op->first;
my $true = $cond->sibling;
my $false = $true->sibling;
my $cuddle = $self->{'cuddle'};
unless ($cx < 1 and (is_scope($true) and $true->name ne "null") and
(is_scope($false) || is_ifelse_cont($false))
and $self->{'expand'} < 7) {
$cond = $self->deparse($cond, 8);
$true = $self->deparse($true, 6);
$false = $self->deparse($false, 8);
return $self->maybe_parens("$cond ? $true : $false", $cx, 8);
}
$cond = $self->deparse($cond, 1);
$true = $self->deparse($true, 0);
my $head = $self->keyword("if") . " ($cond) {\n\t$true\n\b}";
my @elsifs;
my $elsif;
while (!null($false) and is_ifelse_cont($false)) {
my $newop = $false->first;
my $newcond = $newop->first;
my $newtrue = $newcond->sibling;
$false = $newtrue->sibling; # last in chain is OP_AND => no else
if ($newcond->name eq "lineseq")
{
# lineseq to ensure correct line numbers in elsif()
# Bug #37302 fixed by change #33710.
$newcond = $newcond->first->sibling;
}
$newcond = $self->deparse($newcond, 1);
$newtrue = $self->deparse($newtrue, 0);
$elsif ||= $self->keyword("elsif");
push @elsifs, "$elsif ($newcond) {\n\t$newtrue\n\b}";
}
if (!null($false)) {
$false = $cuddle . $self->keyword("else") . " {\n\t" .
$self->deparse($false, 0) . "\n\b}\cK";
} else {
$false = "\cK";
}
return $head . join($cuddle, "", @elsifs) . $false;
}
sub pp_once {
my ($self, $op, $cx) = @_;
my $cond = $op->first;
my $true = $cond->sibling;
my $ret = $self->deparse($true, $cx);
$ret =~ s/^(\(?)\$/$1 . $self->keyword("state") . ' $'/e;
$ret;
}
sub loop_common {
my $self = shift;
my($op, $cx, $init) = @_;
my $enter = $op->first;
my $kid = $enter->sibling;
local(@$self{qw'curstash warnings hints hinthash'})
= @$self{qw'curstash warnings hints hinthash'};
my $head = "";
my $bare = 0;
my $body;
my $cond = undef;
my $name;
if ($kid->name eq "lineseq") { # bare or infinite loop
if ($kid->last->name eq "unstack") { # infinite
$head = "while (1) "; # Can't use for(;;) if there's a continue
$cond = "";
} else {
$bare = 1;
}
$body = $kid;
} elsif ($enter->name eq "enteriter") { # foreach
my $ary = $enter->first->sibling; # first was pushmark
my $var = $ary->sibling;
if ($ary->name eq 'null' and $enter->private & OPpITER_REVERSED) {
# "reverse" was optimised away
$ary = listop($self, $ary->first->sibling, 1, 'reverse');
} elsif ($enter->flags & OPf_STACKED
and not null $ary->first->sibling->sibling)
{
$ary = $self->deparse($ary->first->sibling, 9) . " .. " .
$self->deparse($ary->first->sibling->sibling, 9);
} else {
$ary = $self->deparse($ary, 1);
}
if (null $var) {
$var = $self->pp_padsv($enter, 1, 1);
} elsif ($var->name eq "rv2gv") {
$var = $self->pp_rv2sv($var, 1);
if ($enter->private & OPpOUR_INTRO) {
# our declarations don't have package names
$var =~ s/^(.).*::/$1/;
$var = "our $var";
}
} elsif ($var->name eq "gv") {
$var = "\$" . $self->deparse($var, 1);
} else {
$var = $self->deparse($var, 1);
}
$body = $kid->first->first->sibling; # skip OP_AND and OP_ITER
if (!is_state $body->first and $body->first->name !~ /^(?:stub|leave|scope)$/) {
confess unless $var eq '$_';
$body = $body->first;
return $self->deparse($body, 2) . " "
. $self->keyword("foreach") . " ($ary)";
}
$head = "foreach $var ($ary) ";
} elsif ($kid->name eq "null") { # while/until
$kid = $kid->first;
$name = {"and" => "while", "or" => "until"}->{$kid->name};
$cond = $kid->first;
$body = $kid->first->sibling;
} elsif ($kid->name eq "stub") { # bare and empty
return "{;}"; # {} could be a hashref
}
# If there isn't a continue block, then the next pointer for the loop
# will point to the unstack, which is kid's last child, except
# in a bare loop, when it will point to the leaveloop. When neither of
# these conditions hold, then the second-to-last child is the continue
# block (or the last in a bare loop).
my $cont_start = $enter->nextop;
my $cont;
my $precond;
my $postcond;
if ($$cont_start != $$op && ${$cont_start} != ${$body->last}) {
if ($bare) {
$cont = $body->last;
} else {
$cont = $body->first;
while (!null($cont->sibling->sibling)) {
$cont = $cont->sibling;
}
}
my $state = $body->first;
my $cuddle = $self->{'cuddle'};
my @states;
for (; $$state != $$cont; $state = $state->sibling) {
push @states, $state;
}
$body = $self->lineseq(undef, 0, @states);
if (defined $cond and not is_scope $cont and $self->{'expand'} < 3) {
$precond = "for ($init; ";
$postcond = "; " . $self->deparse($cont, 1) .") ";
$cont = "\cK";
} else {
$cont = $cuddle . "continue {\n\t" .
$self->deparse($cont, 0) . "\n\b}\cK";
}
} else {
return "" if !defined $body;
if (length $init) {
$precond = "for ($init; ";
$postcond = ";) ";
}
$cont = "\cK";
$body = $self->deparse($body, 0);
}
if ($precond) { # for(;;)
$cond &&= $name eq 'until'
? listop($self, undef, 1, "not", $cond->first)
: $self->deparse($cond, 1);
$head = "$precond$cond$postcond";
}
if ($name && !$head) {
ref $cond and $cond = $self->deparse($cond, 1);
$head = "$name ($cond) ";
}
$head =~ s/^(for(?:each)?|while|until)/$self->keyword($1)/e;
$body =~ s/;?$/;\n/;
return $head . "{\n\t" . $body . "\b}" . $cont;
}
sub pp_leaveloop { shift->loop_common(@_, "") }
sub for_loop {
my $self = shift;
my($op, $cx) = @_;
my $init = $self->deparse($op, 1);
my $s = $op->sibling;
my $ll = $s->name eq "unstack" ? $s->sibling : $s->first->sibling;
return $self->loop_common($ll, $cx, $init);
}
sub pp_leavetry {
my $self = shift;
return "eval {\n\t" . $self->pp_leave(@_) . "\n\b}";
}
sub _op_is_or_was {
my ($op, $expect_type) = @_;
my $type = $op->type;
return($type == $expect_type
|| ($type == OP_NULL && $op->targ == $expect_type));
}
sub pp_null {
my($self, $op, $cx) = @_;
# might be 'my $s :Foo(bar);'
if ($op->targ == OP_LIST) {
my $my_attr = maybe_var_attr($self, $op, $cx);
return $my_attr if defined $my_attr;
}
if (class($op) eq "OP") {
# old value is lost
return $self->{'ex_const'} if $op->targ == OP_CONST;
} elsif (class ($op) eq "COP") {
return &pp_nextstate;
} elsif ($op->first->name eq 'pushmark'
or $op->first->name eq 'null'
&& $op->first->targ == OP_PUSHMARK
&& _op_is_or_was($op, OP_LIST)) {
return $self->pp_list($op, $cx);
} elsif ($op->first->name eq "enter") {
return $self->pp_leave($op, $cx);
} elsif ($op->first->name eq "leave") {
return $self->pp_leave($op->first, $cx);
} elsif ($op->first->name eq "scope") {
return $self->pp_scope($op->first, $cx);
} elsif ($op->targ == OP_STRINGIFY) {
return $self->dquote($op, $cx);
} elsif ($op->targ == OP_GLOB) {
return $self->pp_glob(
$op->first # entersub
->first # ex-list
->first # pushmark
->sibling, # glob
$cx
);
} elsif (!null($op->first->sibling) and
$op->first->sibling->name eq "readline" and
$op->first->sibling->flags & OPf_STACKED) {
return $self->maybe_parens($self->deparse($op->first, 7) . " = "
. $self->deparse($op->first->sibling, 7),
$cx, 7);
} elsif (!null($op->first->sibling) and
$op->first->sibling->name =~ /^transr?\z/ and
$op->first->sibling->flags & OPf_STACKED) {
return $self->maybe_parens($self->deparse($op->first, 20) . " =~ "
. $self->deparse($op->first->sibling, 20),
$cx, 20);
} elsif ($op->flags & OPf_SPECIAL && $cx < 1 && !$op->targ) {
return ($self->lex_in_scope("&do") ? "CORE::do" : "do")
. " {\n\t". $self->deparse($op->first, $cx) ."\n\b};";
} elsif (!null($op->first->sibling) and
$op->first->sibling->name eq "null" and
class($op->first->sibling) eq "UNOP" and
$op->first->sibling->first->flags & OPf_STACKED and
$op->first->sibling->first->name eq "rcatline") {
return $self->maybe_parens($self->deparse($op->first, 18) . " .= "
. $self->deparse($op->first->sibling, 18),
$cx, 18);
} else {
return $self->deparse($op->first, $cx);
}
}
sub padname {
my $self = shift;
my $targ = shift;
return $self->padname_sv($targ)->PVX;
}
sub padany {
my $self = shift;
my $op = shift;
return substr($self->padname($op->targ), 1); # skip $/@/%
}
sub pp_padsv {
my $self = shift;
my($op, $cx, $forbid_parens) = @_;
my $targ = $op->targ;
return $self->maybe_my($op, $cx, $self->padname($targ),
$self->padname_sv($targ),
$forbid_parens);
}
sub pp_padav { pp_padsv(@_) }
# prepend 'keys' where its been optimised away, with suitable handling
# of CORE:: and parens
sub add_keys_keyword {
my ($self, $str, $cx) = @_;
$str = $self->maybe_parens($str, $cx, 16);
# 'keys %h' versus 'keys(%h)'
$str = " $str" unless $str =~ /^\(/;
return $self->keyword("keys") . $str;
}
sub pp_padhv {
my ($self, $op, $cx) = @_;
my $str = pp_padsv(@_);
# with OPpPADHV_ISKEYS the keys op is optimised away, except
# in scalar context the old op is kept (but not executed) so its targ
# can be used.
if ( ($op->private & OPpPADHV_ISKEYS)
&& !(($op->flags & OPf_WANT) == OPf_WANT_SCALAR))
{
$str = $self->add_keys_keyword($str, $cx);
}
$str;
}
sub gv_or_padgv {
my $self = shift;
my $op = shift;
if (class($op) eq "PADOP") {
return $self->padval($op->padix);
} else { # class($op) eq "SVOP"
return $op->gv;
}
}
sub pp_gvsv {
my $self = shift;
my($op, $cx) = @_;
my $gv = $self->gv_or_padgv($op);
return $self->maybe_local($op, $cx, $self->stash_variable("\$",
$self->gv_name($gv), $cx));
}
sub pp_gv {
my $self = shift;
my($op, $cx) = @_;
my $gv = $self->gv_or_padgv($op);
return $self->maybe_qualify("", $self->gv_name($gv));
}
sub pp_aelemfast_lex {
my $self = shift;
my($op, $cx) = @_;
my $name = $self->padname($op->targ);
$name =~ s/^@/\$/;
my $i = $op->private;
$i -= 256 if $i > 127;
return $name . "[$i]";
}
sub pp_aelemfast {
my $self = shift;
my($op, $cx) = @_;
# optimised PADAV, pre 5.15
return $self->pp_aelemfast_lex(@_) if ($op->flags & OPf_SPECIAL);
my $gv = $self->gv_or_padgv($op);
my($name,$quoted) = $self->stash_variable_name('@',$gv);
$name = $quoted ? "$name->" : '$' . $name;
my $i = $op->private;
$i -= 256 if $i > 127;
return $name . "[$i]";
}
sub rv2x {
my $self = shift;
my($op, $cx, $type) = @_;
if (class($op) eq 'NULL' || !$op->can("first")) {
carp("Unexpected op in pp_rv2x");
return 'XXX';
}
my $kid = $op->first;
if ($kid->name eq "gv") {
return $self->stash_variable($type,
$self->gv_name($self->gv_or_padgv($kid)), $cx);
} elsif (is_scalar $kid) {
my $str = $self->deparse($kid, 0);
if ($str =~ /^\$([^\w\d])\z/) {
# "$$+" isn't a legal way to write the scalar dereference
# of $+, since the lexer can't tell you aren't trying to
# do something like "$$ + 1" to get one more than your
# PID. Either "${$+}" or "$${+}" are workable
# disambiguations, but if the programmer did the former,
# they'd be in the "else" clause below rather than here.
# It's not clear if this should somehow be unified with
# the code in dq and re_dq that also adds lexer
# disambiguation braces.
$str = '$' . "{$1}"; #'
}
return $type . $str;
} else {
return $type . "{" . $self->deparse($kid, 0) . "}";
}
}
sub pp_rv2sv { maybe_local(@_, rv2x(@_, "\$")) }
sub pp_rv2gv { maybe_local(@_, rv2x(@_, "*")) }
sub pp_rv2hv {
my ($self, $op, $cx) = @_;
my $str = rv2x(@_, "%");
if ($op->private & OPpRV2HV_ISKEYS) {
$str = $self->add_keys_keyword($str, $cx);
}
return maybe_local(@_, $str);
}
# skip rv2av
sub pp_av2arylen {
my $self = shift;
my($op, $cx) = @_;
my $kid = $op->first;
if ($kid->name eq "padav") {
return $self->maybe_local($op, $cx, '$#' . $self->padany($kid));
} else {
my $kkid;
if ( $kid->name eq "rv2av"
&& ($kkid = $kid->first)
&& $kkid->name !~ /^(scope|leave|gv)$/)
{
# handle (expr)->$#* postfix form
my $expr;
$expr = $self->deparse($kkid, 24); # 24 is '->'
$expr = "$expr->\$#*";
# XXX maybe_local is probably wrong here: local($#-expression)
# doesn't "do" local (the is no INTRO flag set)
return $self->maybe_local($op, $cx, $expr);
}
else {
# handle $#{expr} form
# XXX see maybe_local comment above
return $self->maybe_local($op, $cx, $self->rv2x($kid, $cx, '$#'));
}
}
}
# skip down to the old, ex-rv2cv
sub pp_rv2cv {
my ($self, $op, $cx) = @_;
if (!null($op->first) && $op->first->name eq 'null' &&
$op->first->targ == OP_LIST)
{
return $self->rv2x($op->first->first->sibling, $cx, "&")
}
else {
return $self->rv2x($op, $cx, "")
}
}
sub list_const {
my $self = shift;
my($cx, @list) = @_;
my @a = map $self->const($_, 6), @list;
if (@a == 0) {
return "()";
} elsif (@a == 1) {
return $a[0];
} elsif ( @a > 2 and !grep(!/^-?\d+$/, @a)) {
# collapse (-1,0,1,2) into (-1..2)
my ($s, $e) = @a[0,-1];
my $i = $s;
return $self->maybe_parens("$s..$e", $cx, 9)
unless grep $i++ != $_, @a;
}
return $self->maybe_parens(join(", ", @a), $cx, 6);
}
sub pp_rv2av {
my $self = shift;
my($op, $cx) = @_;
my $kid = $op->first;
if ($kid->name eq "const") { # constant list
my $av = $self->const_sv($kid);
return $self->list_const($cx, $av->ARRAY);
} else {
return $self->maybe_local($op, $cx, $self->rv2x($op, $cx, "\@"));
}
}
sub is_subscriptable {
my $op = shift;
if ($op->name =~ /^([ahg]elem|multideref$)/) {
return 1;
} elsif ($op->name eq "entersub") {
my $kid = $op->first;
return 0 unless null $kid->sibling;
$kid = $kid->first;
$kid = $kid->sibling until null $kid->sibling;
return 0 if is_scope($kid);
$kid = $kid->first;
return 0 if $kid->name eq "gv" || $kid->name eq "padcv";
return 0 if is_scalar($kid);
return is_subscriptable($kid);
} else {
return 0;
}
}
sub elem_or_slice_array_name
{
my $self = shift;
my ($array, $left, $padname, $allow_arrow) = @_;
if ($array->name eq $padname) {
return $self->padany($array);
} elsif (is_scope($array)) { # ${expr}[0]
return "{" . $self->deparse($array, 0) . "}";
} elsif ($array->name eq "gv") {
($array, my $quoted) =
$self->stash_variable_name(
$left eq '[' ? '@' : '%', $self->gv_or_padgv($array)
);
if (!$allow_arrow && $quoted) {
# This cannot happen.
die "Invalid variable name $array for slice";
}
return $quoted ? "$array->" : $array;
} elsif (!$allow_arrow || is_scalar $array) { # $x[0], $$x[0], ...
return $self->deparse($array, 24);
} else {
return undef;
}
}
sub elem_or_slice_single_index
{
my $self = shift;
my ($idx) = @_;
$idx = $self->deparse($idx, 1);
# Outer parens in an array index will confuse perl
# if we're interpolating in a regular expression, i.e.
# /$x$foo[(-1)]/ is *not* the same as /$x$foo[-1]/
#
# If $self->{parens}, then an initial '(' will
# definitely be paired with a final ')'. If
# !$self->{parens}, the misleading parens won't
# have been added in the first place.
#
# [You might think that we could get "(...)...(...)"
# where the initial and final parens do not match
# each other. But we can't, because the above would
# only happen if there's an infix binop between the
# two pairs of parens, and *that* means that the whole
# expression would be parenthesized as well.]
#
$idx =~ s/^\((.*)\)$/$1/ if $self->{'parens'};
# Hash-element braces will autoquote a bareword inside themselves.
# We need to make sure that C<$hash{warn()}> doesn't come out as
# C<$hash{warn}>, which has a quite different meaning. Currently
# B::Deparse will always quote strings, even if the string was a
# bareword in the original (i.e. the OPpCONST_BARE flag is ignored
# for constant strings.) So we can cheat slightly here - if we see
# a bareword, we know that it is supposed to be a function call.
#
$idx =~ s/^([A-Za-z_]\w*)$/$1()/;
return $idx;
}
sub elem {
my $self = shift;
my ($op, $cx, $left, $right, $padname) = @_;
my($array, $idx) = ($op->first, $op->first->sibling);
$idx = $self->elem_or_slice_single_index($idx);
unless ($array->name eq $padname) { # Maybe this has been fixed
$array = $array->first; # skip rv2av (or ex-rv2av in _53+)
}
if (my $array_name=$self->elem_or_slice_array_name
($array, $left, $padname, 1)) {
return ($array_name =~ /->\z/
? $array_name
: $array_name eq '#' ? '${#}' : "\$" . $array_name)
. $left . $idx . $right;
} else {
# $x[20][3]{hi} or expr->[20]
my $arrow = is_subscriptable($array) ? "" : "->";
return $self->deparse($array, 24) . $arrow . $left . $idx . $right;
}
}
# a simplified version of elem_or_slice_array_name()
# for the use of pp_multideref
sub multideref_var_name {
my $self = shift;
my ($gv, $is_hash) = @_;
my ($name, $quoted) =
$self->stash_variable_name( $is_hash ? '%' : '@', $gv);
return $quoted ? "$name->"
: $name eq '#'
? '${#}' # avoid ${#}[1] => $#[1]
: '$' . $name;
}
# deparse an OP_MULTICONCAT. If $in_dq is 1, we're within
# a double-quoted string, so for example.
# "abc\Qdef$x\Ebar"
# might get compiled as
# multiconcat("abc", metaquote(multiconcat("def", $x)), "bar")
# and the inner multiconcat should be deparsed as C<def$x> rather than
# the normal C<def . $x>
# Ditto if $in_dq is 2, handle qr/...\Qdef$x\E.../.
sub do_multiconcat {
my $self = shift;
my($op, $cx, $in_dq) = @_;
my $kid;
my @kids;
my $assign;
my $append;
my $lhs = "";
for ($kid = $op->first; !null $kid; $kid = $kid->sibling) {
# skip the consts and/or padsv we've optimised away
push @kids, $kid
unless $kid->type == OP_NULL
&& ( $kid->targ == OP_PADSV
|| $kid->targ == OP_CONST
|| $kid->targ == OP_PUSHMARK);
}
$append = ($op->private & OPpMULTICONCAT_APPEND);
if ($op->private & OPpTARGET_MY) {
# '$lex = ...' or '$lex .= ....' or 'my $lex = '
$lhs = $self->padname($op->targ);
$lhs = "my $lhs" if ($op->private & OPpLVAL_INTRO);
$assign = 1;
}
elsif ($op->flags & OPf_STACKED) {
# 'expr = ...' or 'expr .= ....'
my $expr = $append ? shift(@kids) : pop(@kids);
$lhs = $self->deparse($expr, 7);
$assign = 1;
}
if ($assign) {
$lhs .= $append ? ' .= ' : ' = ';
}
my ($nargs, $const_str, @const_lens) = $op->aux_list($self->{curcv});
my @consts;
my $i = 0;
for (@const_lens) {
if ($_ == -1) {
push @consts, undef;
}
else {
push @consts, substr($const_str, $i, $_);
my @args;
$i += $_;
}
}
my $rhs = "";
if ( $in_dq
|| (($op->private & OPpMULTICONCAT_STRINGIFY) && !$self->{'unquote'}))
{
# "foo=$foo bar=$bar "
my $not_first;
while (@consts) {
if ($not_first) {
my $s = $self->dq(shift(@kids), 18);
# don't deparse "a${$}b" as "a$$b"
$s = '${$}' if $s eq '$$';
$rhs = dq_disambiguate($rhs, $s);
}
$not_first = 1;
my $c = shift @consts;
if (defined $c) {
if ($in_dq == 2) {
# in pattern: don't convert newline to '\n' etc etc
my $s = re_uninterp(escape_re(re_unback($c)));
$rhs = re_dq_disambiguate($rhs, $s)
}
else {
my $s = uninterp(escape_str(unback($c)));
$rhs = dq_disambiguate($rhs, $s)
}
}
}
return $rhs if $in_dq;
$rhs = single_delim("qq", '"', $rhs, $self);
}
elsif ($op->private & OPpMULTICONCAT_FAKE) {
# sprintf("foo=%s bar=%s ", $foo, $bar)
my @all;
@consts = map { $_ //= ''; s/%/%%/g; $_ } @consts;
my $fmt = join '%s', @consts;
push @all, $self->quoted_const_str($fmt);
# the following is a stripped down copy of sub listop {}
my $parens = $assign || ($cx >= 5) || $self->{'parens'};
my $fullname = $self->keyword('sprintf');
push @all, map $self->deparse($_, 6), @kids;
$rhs = $parens
? "$fullname(" . join(", ", @all) . ")"
: "$fullname " . join(", ", @all);
}
else {
# "foo=" . $foo . " bar=" . $bar
my @all;
my $not_first;
while (@consts) {
push @all, $self->deparse(shift(@kids), 18) if $not_first;
$not_first = 1;
my $c = shift @consts;
if (defined $c) {
push @all, $self->quoted_const_str($c);
}
}
$rhs .= join ' . ', @all;
}
my $text = $lhs . $rhs;
$text = "($text)" if ($cx >= (($assign) ? 7 : 18+1))
|| $self->{'parens'};
return $text;
}
sub pp_multiconcat {
my $self = shift;
$self->do_multiconcat(@_, 0);
}
sub pp_multideref {
my $self = shift;
my($op, $cx) = @_;
my $text = "";
if ($op->private & OPpMULTIDEREF_EXISTS) {
$text = $self->keyword("exists"). " ";
}
elsif ($op->private & OPpMULTIDEREF_DELETE) {
$text = $self->keyword("delete"). " ";
}
elsif ($op->private & OPpLVAL_INTRO) {
$text = $self->keyword("local"). " ";
}
if ($op->first && ($op->first->flags & OPf_KIDS)) {
# arbitrary initial expression, e.g. f(1,2,3)->[...]
my $expr = $self->deparse($op->first, 24);
# stop "exists (expr)->{...}" being interpreted as
#"(exists (expr))->{...}"
$expr = "+$expr" if $expr =~ /^\(/;
$text .= $expr;
}
my @items = $op->aux_list($self->{curcv});
my $actions = shift @items;
my $is_hash;
my $derefs = 0;
while (1) {
if (($actions & MDEREF_ACTION_MASK) == MDEREF_reload) {
$actions = shift @items;
next;
}
$is_hash = (
($actions & MDEREF_ACTION_MASK) == MDEREF_HV_pop_rv2hv_helem
|| ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvsv_vivify_rv2hv_helem
|| ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padsv_vivify_rv2hv_helem
|| ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_vivify_rv2hv_helem
|| ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padhv_helem
|| ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvhv_helem
);
if ( ($actions & MDEREF_ACTION_MASK) == MDEREF_AV_padav_aelem
|| ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padhv_helem)
{
$derefs = 1;
$text .= '$' . substr($self->padname(shift @items), 1);
}
elsif ( ($actions & MDEREF_ACTION_MASK) == MDEREF_AV_gvav_aelem
|| ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvhv_helem)
{
$derefs = 1;
$text .= $self->multideref_var_name(shift @items, $is_hash);
}
else {
if ( ($actions & MDEREF_ACTION_MASK) ==
MDEREF_AV_padsv_vivify_rv2av_aelem
|| ($actions & MDEREF_ACTION_MASK) ==
MDEREF_HV_padsv_vivify_rv2hv_helem)
{
$text .= $self->padname(shift @items);
}
elsif ( ($actions & MDEREF_ACTION_MASK) ==
MDEREF_AV_gvsv_vivify_rv2av_aelem
|| ($actions & MDEREF_ACTION_MASK) ==
MDEREF_HV_gvsv_vivify_rv2hv_helem)
{
$text .= $self->multideref_var_name(shift @items, $is_hash);
}
elsif ( ($actions & MDEREF_ACTION_MASK) ==
MDEREF_AV_pop_rv2av_aelem
|| ($actions & MDEREF_ACTION_MASK) ==
MDEREF_HV_pop_rv2hv_helem)
{
if ( ($op->flags & OPf_KIDS)
&& ( _op_is_or_was($op->first, OP_RV2AV)
|| _op_is_or_was($op->first, OP_RV2HV))
&& ($op->first->flags & OPf_KIDS)
&& ( _op_is_or_was($op->first->first, OP_AELEM)
|| _op_is_or_was($op->first->first, OP_HELEM))
)
{
$derefs++;
}
}
$text .= '->' if !$derefs++;
}
if (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_none) {
last;
}
$text .= $is_hash ? '{' : '[';
if (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_const) {
my $key = shift @items;
if ($is_hash) {
$text .= $self->const($key, $cx);
}
else {
$text .= $key;
}
}
elsif (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_padsv) {
$text .= $self->padname(shift @items);
}
elsif (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_gvsv) {
$text .= '$' . ($self->stash_variable_name('$', shift @items))[0];
}
$text .= $is_hash ? '}' : ']';
if ($actions & MDEREF_FLAG_last) {
last;
}
$actions >>= MDEREF_SHIFT;
}
return $text;
}
sub pp_aelem { maybe_local(@_, elem(@_, "[", "]", "padav")) }
sub pp_helem { maybe_local(@_, elem(@_, "{", "}", "padhv")) }
sub pp_gelem {
my $self = shift;
my($op, $cx) = @_;
my($glob, $part) = ($op->first, $op->last);
$glob = $glob->first; # skip rv2gv
$glob = $glob->first if $glob->name eq "rv2gv"; # this one's a bug
my $scope = is_scope($glob);
$glob = $self->deparse($glob, 0);
$part = $self->deparse($part, 1);
$glob =~ s/::\z// unless $scope;
return "*" . ($scope ? "{$glob}" : $glob) . "{$part}";
}
sub slice {
my $self = shift;
my ($op, $cx, $left, $right, $regname, $padname) = @_;
my $last;
my(@elems, $kid, $array, $list);
if (class($op) eq "LISTOP") {
$last = $op->last;
} else { # ex-hslice inside delete()
for ($kid = $op->first; !null $kid->sibling; $kid = $kid->sibling) {}
$last = $kid;
}
$array = $last;
$array = $array->first
if $array->name eq $regname or $array->name eq "null";
$array = $self->elem_or_slice_array_name($array,$left,$padname,0);
$kid = $op->first->sibling; # skip pushmark
if ($kid->name eq "list") {
$kid = $kid->first->sibling; # skip list, pushmark
for (; !null $kid; $kid = $kid->sibling) {
push @elems, $self->deparse($kid, 6);
}
$list = join(", ", @elems);
} else {
$list = $self->elem_or_slice_single_index($kid);
}
my $lead = ( _op_is_or_was($op, OP_KVHSLICE)
|| _op_is_or_was($op, OP_KVASLICE))
? '%' : '@';
return $lead . $array . $left . $list . $right;
}
sub pp_aslice { maybe_local(@_, slice(@_, "[", "]", "rv2av", "padav")) }
sub pp_kvaslice { slice(@_, "[", "]", "rv2av", "padav") }
sub pp_hslice { maybe_local(@_, slice(@_, "{", "}", "rv2hv", "padhv")) }
sub pp_kvhslice { slice(@_, "{", "}", "rv2hv", "padhv") }
sub pp_lslice {
my $self = shift;
my($op, $cx) = @_;
my $idx = $op->first;
my $list = $op->last;
my(@elems, $kid);
$list = $self->deparse($list, 1);
$idx = $self->deparse($idx, 1);
return "($list)" . "[$idx]";
}
sub want_scalar {
my $op = shift;
return ($op->flags & OPf_WANT) == OPf_WANT_SCALAR;
}
sub want_list {
my $op = shift;
return ($op->flags & OPf_WANT) == OPf_WANT_LIST;
}
sub _method {
my $self = shift;
my($op, $cx) = @_;
my $kid = $op->first->sibling; # skip pushmark
my($meth, $obj, @exprs);
if ($kid->name eq "list" and want_list $kid) {
# When an indirect object isn't a bareword but the args are in
# parens, the parens aren't part of the method syntax (the LLAFR
# doesn't apply), but they make a list with OPf_PARENS set that
# doesn't get flattened by the append_elem that adds the method,
# making a (object, arg1, arg2, ...) list where the object
# usually is. This can be distinguished from
# '($obj, $arg1, $arg2)->meth()' (which is legal if $arg2 is an
# object) because in the later the list is in scalar context
# as the left side of -> always is, while in the former
# the list is in list context as method arguments always are.
# (Good thing there aren't method prototypes!)
$meth = $kid->sibling;
$kid = $kid->first->sibling; # skip pushmark
$obj = $kid;
$kid = $kid->sibling;
for (; not null $kid; $kid = $kid->sibling) {
push @exprs, $kid;
}
} else {
$obj = $kid;
$kid = $kid->sibling;
for (; !null ($kid->sibling) && $kid->name!~/^method(?:_named)?\z/;
$kid = $kid->sibling) {
push @exprs, $kid
}
$meth = $kid;
}
if ($meth->name eq "method_named") {
$meth = $self->meth_sv($meth)->PV;
} elsif ($meth->name eq "method_super") {
$meth = "SUPER::".$self->meth_sv($meth)->PV;
} elsif ($meth->name eq "method_redir") {
$meth = $self->meth_rclass_sv($meth)->PV.'::'.$self->meth_sv($meth)->PV;
} elsif ($meth->name eq "method_redir_super") {
$meth = $self->meth_rclass_sv($meth)->PV.'::SUPER::'.
$self->meth_sv($meth)->PV;
} else {
$meth = $meth->first;
if ($meth->name eq "const") {
# As of 5.005_58, this case is probably obsoleted by the
# method_named case above
$meth = $self->const_sv($meth)->PV; # needs to be bare
}
}
return { method => $meth, variable_method => ref($meth),
object => $obj, args => \@exprs },
$cx;
}
# compat function only
sub method {
my $self = shift;
my $info = $self->_method(@_);
return $self->e_method( $self->_method(@_) );
}
sub e_method {
my ($self, $info, $cx) = @_;
my $obj = $self->deparse($info->{object}, 24);
my $meth = $info->{method};
$meth = $self->deparse($meth, 1) if $info->{variable_method};
my $args = join(", ", map { $self->deparse($_, 6) } @{$info->{args}} );
if ($info->{object}->name eq 'scope' && want_list $info->{object}) {
# method { $object }
# This must be deparsed this way to preserve list context
# of $object.
my $need_paren = $cx >= 6;
return '(' x $need_paren
. $meth . substr($obj,2) # chop off the "do"
. " $args"
. ')' x $need_paren;
}
my $kid = $obj . "->" . $meth;
if (length $args) {
return $kid . "(" . $args . ")"; # parens mandatory
} else {
return $kid;
}
}
# returns "&" if the prototype doesn't match the args,
# or ("", $args_after_prototype_demunging) if it does.
sub check_proto {
my $self = shift;
return "&" if $self->{'noproto'};
my($proto, @args) = @_;
my($arg, $real);
my $doneok = 0;
my @reals;
# An unbackslashed @ or % gobbles up the rest of the args
1 while $proto =~ s/(?<!\\)([@%])[^\]]+$/$1/;
$proto =~ s/^\s*//;
while ($proto) {
$proto =~ s/^(\\?[\$\@&%*_]|\\\[[\$\@&%*]+\]|;|)\s*//;
my $chr = $1;
if ($chr eq "") {
return "&" if @args;
} elsif ($chr eq ";") {
$doneok = 1;
} elsif ($chr eq "@" or $chr eq "%") {
push @reals, map($self->deparse($_, 6), @args);
@args = ();
} else {
$arg = shift @args;
last unless $arg;
if ($chr eq "\$" || $chr eq "_") {
if (want_scalar $arg) {
push @reals, $self->deparse($arg, 6);
} else {
return "&";
}
} elsif ($chr eq "&") {
if ($arg->name =~ /^(s?refgen|undef)$/) {
push @reals, $self->deparse($arg, 6);
} else {
return "&";
}
} elsif ($chr eq "*") {
if ($arg->name =~ /^s?refgen$/
and $arg->first->first->name eq "rv2gv")
{
$real = $arg->first->first; # skip refgen, null
if ($real->first->name eq "gv") {
push @reals, $self->deparse($real, 6);
} else {
push @reals, $self->deparse($real->first, 6);
}
} else {
return "&";
}
} elsif (substr($chr, 0, 1) eq "\\") {
$chr =~ tr/\\[]//d;
if ($arg->name =~ /^s?refgen$/ and
!null($real = $arg->first) and
($chr =~ /\$/ && is_scalar($real->first)
or ($chr =~ /@/
&& class($real->first->sibling) ne 'NULL'
&& $real->first->sibling->name
=~ /^(rv2|pad)av$/)
or ($chr =~ /%/
&& class($real->first->sibling) ne 'NULL'
&& $real->first->sibling->name
=~ /^(rv2|pad)hv$/)
#or ($chr =~ /&/ # This doesn't work
# && $real->first->name eq "rv2cv")
or ($chr =~ /\*/
&& $real->first->name eq "rv2gv")))
{
push @reals, $self->deparse($real, 6);
} else {
return "&";
}
}
}
}
return "&" if $proto and !$doneok; # too few args and no ';'
return "&" if @args; # too many args
return ("", join ", ", @reals);
}
sub retscalar {
my $name = $_[0]->name;
# XXX There has to be a better way of doing this scalar-op check.
# Currently PL_opargs is not exposed.
if ($name eq 'null') {
$name = substr B::ppname($_[0]->targ), 3
}
$name =~ /^(?:scalar|pushmark|wantarray|const|gvsv|gv|padsv|rv2gv
|rv2sv|av2arylen|anoncode|prototype|srefgen|ref|bless
|regcmaybe|regcreset|regcomp|qr|subst|substcont|trans
|transr|sassign|chop|schop|chomp|schomp|defined|undef
|study|pos|preinc|i_preinc|predec|i_predec|postinc
|i_postinc|postdec|i_postdec|pow|multiply|i_multiply
|divide|i_divide|modulo|i_modulo|add|i_add|subtract
|i_subtract|concat|multiconcat|stringify|left_shift|right_shift|lt
|i_lt|gt|i_gt|le|i_le|ge|i_ge|eq|i_eq|ne|i_ne|ncmp|i_ncmp
|slt|sgt|sle|sge|seq|sne|scmp|[sn]?bit_(?:and|x?or)|negate
|i_negate|not|[sn]?complement|smartmatch|atan2|sin|cos
|rand|srand|exp|log|sqrt|int|hex|oct|abs|length|substr
|vec|index|rindex|sprintf|formline|ord|chr|crypt|ucfirst
|lcfirst|uc|lc|quotemeta|aelemfast|aelem|exists|helem
|pack|join|anonlist|anonhash|push|pop|shift|unshift|xor
|andassign|orassign|dorassign|warn|die|reset|nextstate
|dbstate|unstack|last|next|redo|dump|goto|exit|open|close
|pipe_op|fileno|umask|binmode|tie|untie|tied|dbmopen
|dbmclose|select|getc|read|enterwrite|prtf|print|say
|sysopen|sysseek|sysread|syswrite|eof|tell|seek|truncate
|fcntl|ioctl|flock|send|recv|socket|sockpair|bind|connect
|listen|accept|shutdown|gsockopt|ssockopt|getsockname
|getpeername|ftrread|ftrwrite|ftrexec|fteread|ftewrite
|fteexec|ftis|ftsize|ftmtime|ftatime|ftctime|ftrowned
|fteowned|ftzero|ftsock|ftchr|ftblk|ftfile|ftdir|ftpipe
|ftsuid|ftsgid|ftsvtx|ftlink|fttty|fttext|ftbinary|chdir
|chown|chroot|unlink|chmod|utime|rename|link|symlink
|readlink|mkdir|rmdir|open_dir|telldir|seekdir|rewinddir
|closedir|fork|wait|waitpid|system|exec|kill|getppid
|getpgrp|setpgrp|getpriority|setpriority|time|alarm|sleep
|shmget|shmctl|shmread|shmwrite|msgget|msgctl|msgsnd
|msgrcv|semop|semget|semctl|hintseval|shostent|snetent
|sprotoent|sservent|ehostent|enetent|eprotoent|eservent
|spwent|epwent|sgrent|egrent|getlogin|syscall|lock|runcv
|fc)\z/x
}
sub pp_entersub {
my $self = shift;
my($op, $cx) = @_;
return $self->e_method($self->_method($op, $cx))
unless null $op->first->sibling;
my $prefix = "";
my $amper = "";
my($kid, @exprs);
if ($op->flags & OPf_SPECIAL && !($op->flags & OPf_MOD)) {
$prefix = "do ";
} elsif ($op->private & OPpENTERSUB_AMPER) {
$amper = "&";
}
$kid = $op->first;
$kid = $kid->first->sibling; # skip ex-list, pushmark
for (; not null $kid->sibling; $kid = $kid->sibling) {
push @exprs, $kid;
}
my $simple = 0;
my $proto = undef;
my $lexical;
if (is_scope($kid)) {
$amper = "&";
$kid = "{" . $self->deparse($kid, 0) . "}";
} elsif ($kid->first->name eq "gv") {
my $gv = $self->gv_or_padgv($kid->first);
my $cv;
if (class($gv) eq 'GV' && class($cv = $gv->CV) ne "SPECIAL"
|| $gv->FLAGS & SVf_ROK && class($cv = $gv->RV) eq 'CV') {
$proto = $cv->PV if $cv->FLAGS & SVf_POK;
}
$simple = 1; # only calls of named functions can be prototyped
$kid = $self->maybe_qualify("!", $self->gv_name($gv));
my $fq;
# Fully qualify any sub name that conflicts with a lexical.
if ($self->lex_in_scope("&$kid")
|| $self->lex_in_scope("&$kid", 1))
{
$fq++;
} elsif (!$amper) {
if ($kid eq 'main::') {
$kid = '::';
}
else {
if ($kid !~ /::/ && $kid ne 'x') {
# Fully qualify any sub name that is also a keyword. While
# we could check the import flag, we cannot guarantee that
# the code deparsed so far would set that flag, so we qual-
# ify the names regardless of importation.
if (exists $feature_keywords{$kid}) {
$fq++ if $self->feature_enabled($kid);
} elsif (do { local $@; local $SIG{__DIE__};
eval { () = prototype "CORE::$kid"; 1 } }) {
$fq++
}
}
if ($kid !~ /^(?:\w|::)(?:[\w\d]|::(?!\z))*\z/) {
$kid = single_delim("q", "'", $kid, $self) . '->';
}
}
}
$fq and substr $kid, 0, 0, = $self->{'curstash'}.'::';
} elsif (is_scalar ($kid->first) && $kid->first->name ne 'rv2cv') {
$amper = "&";
$kid = $self->deparse($kid, 24);
} else {
$prefix = "";
my $grandkid = $kid->first;
my $arrow = ($lexical = $grandkid->name eq "padcv")
|| is_subscriptable($grandkid)
? ""
: "->";
$kid = $self->deparse($kid, 24) . $arrow;
if ($lexical) {
my $padlist = $self->{'curcv'}->PADLIST;
my $padoff = $grandkid->targ;
my $padname = $padlist->ARRAYelt(0)->ARRAYelt($padoff);
my $protocv = $padname->FLAGS & SVpad_STATE
? $padlist->ARRAYelt(1)->ARRAYelt($padoff)
: $padname->PROTOCV;
if ($protocv->FLAGS & SVf_POK) {
$proto = $protocv->PV
}
$simple = 1;
}
}
# Doesn't matter how many prototypes there are, if
# they haven't happened yet!
my $declared = $lexical || exists $self->{'subs_declared'}{$kid};
if (not $declared and $self->{'in_coderef2text'}) {
no strict 'refs';
no warnings 'uninitialized';
$declared =
(
defined &{ ${$self->{'curstash'}."::"}{$kid} }
&& !exists
$self->{'subs_deparsed'}{$self->{'curstash'}."::".$kid}
&& defined prototype $self->{'curstash'}."::".$kid
);
}
if (!$declared && defined($proto)) {
# Avoid "too early to check prototype" warning
($amper, $proto) = ('&');
}
my $args;
my $listargs = 1;
if ($declared and defined $proto and not $amper) {
($amper, $args) = $self->check_proto($proto, @exprs);
$listargs = $amper;
}
if ($listargs) {
$args = join(", ", map(
($_->flags & OPf_WANT) == OPf_WANT_SCALAR
&& !retscalar($_)
? $self->maybe_parens_unop('scalar', $_, 6)
: $self->deparse($_, 6),
@exprs
));
}
if ($prefix or $amper) {
if ($kid eq '&') { $kid = "{$kid}" } # &{&} cannot be written as &&
if ($op->flags & OPf_STACKED) {
return $prefix . $amper . $kid . "(" . $args . ")";
} else {
return $prefix . $amper. $kid;
}
} else {
# It's a syntax error to call CORE::GLOBAL::foo with a prefix,
# so it must have been translated from a keyword call. Translate
# it back.
$kid =~ s/^CORE::GLOBAL:://;
my $dproto = defined($proto) ? $proto : "undefined";
my $scalar_proto = $dproto =~ /^;*(?:[\$*_+]|\\.|\\\[[^]]\])\z/;
if (!$declared) {
return "$kid(" . $args . ")";
} elsif ($dproto =~ /^\s*\z/) {
return $kid;
} elsif ($scalar_proto and is_scalar($exprs[0])) {
# is_scalar is an excessively conservative test here:
# really, we should be comparing to the precedence of the
# top operator of $exprs[0] (ala unop()), but that would
# take some major code restructuring to do right.
return $self->maybe_parens_func($kid, $args, $cx, 16);
} elsif (not $scalar_proto and defined($proto) || $simple) { #'
return $self->maybe_parens_func($kid, $args, $cx, 5);
} else {
return "$kid(" . $args . ")";
}
}
}
sub pp_enterwrite { unop(@_, "write") }
# escape things that cause interpolation in double quotes,
# but not character escapes
sub uninterp {
my($str) = @_;
$str =~ s/(^|\G|[^\\])((?:\\\\)*)([\$\@]|\\[uUlLQE])/$1$2\\$3/g;
return $str;
}
{
my $bal;
BEGIN {
use re "eval";
# Matches any string which is balanced with respect to {braces}
$bal = qr(
(?:
[^\\{}]
| \\\\
| \\[{}]
| \{(??{$bal})\}
)*
)x;
}
# the same, but treat $|, $), $( and $ at the end of the string differently
# and leave comments unmangled for the sake of /x and (?x).
sub re_uninterp {
my($str) = @_;
$str =~ s/
( ^|\G # $1
| [^\\]
)
( # $2
(?:\\\\)*
)
( # $3
( \(\?\??\{$bal\}\) # $4 (skip over (?{}) and (??{}) blocks)
| \#[^\n]* # (skip over comments)
)
| [\$\@]
(?!\||\)|\(|$|\s)
| \\[uUlLQE]
)
/defined($4) && length($4) ? "$1$2$4" : "$1$2\\$3"/xeg;
return $str;
}
}
# character escapes, but not delimiters that might need to be escaped
sub escape_str { # ASCII, UTF8
my($str) = @_;
$str =~ s/(.)/ord($1) > 255 ? sprintf("\\x{%x}", ord($1)) : $1/eg;
$str =~ s/\a/\\a/g;
# $str =~ s/\cH/\\b/g; # \b means something different in a regex; and \cH
# isn't a backspace in EBCDIC
$str =~ s/\t/\\t/g;
$str =~ s/\n/\\n/g;
$str =~ s/\e/\\e/g;
$str =~ s/\f/\\f/g;
$str =~ s/\r/\\r/g;
$str =~ s/([\cA-\cZ])/'\\c' . $unctrl{$1}/ge;
$str =~ s/([[:^print:]])/sprintf("\\%03o", ord($1))/age;
return $str;
}
# For regexes. Leave whitespace unmangled in case of /x or (?x).
sub escape_re {
my($str) = @_;
$str =~ s/(.)/ord($1) > 255 ? sprintf("\\x{%x}", ord($1)) : $1/eg;
$str =~ s/([[:^print:]])/
($1 =~ y! \t\n!!) ? $1 : sprintf("\\%03o", ord($1))/age;
$str =~ s/\n/\n\f/g;
return $str;
}
# Don't do this for regexen
sub unback {
my($str) = @_;
$str =~ s/\\/\\\\/g;
return $str;
}
# Remove backslashes which precede literal control characters,
# to avoid creating ambiguity when we escape the latter.
#
# Don't remove a backslash from escaped whitespace: where the T represents
# a literal tab character, /T/x is not equivalent to /\T/x
sub re_unback {
my($str) = @_;
# the insane complexity here is due to the behaviour of "\c\"
$str =~ s/
# these two lines ensure that the backslash we're about to
# remove isn't preceeded by something which makes it part
# of a \c
(^ | [^\\] | \\c\\) # $1
(?<!\\c)
# the backslash to remove
\\
# keep pairs of backslashes
(\\\\)* # $2
# only remove if the thing following is a control char
(?=[[:^print:]])
# and not whitespace
(?=\S)
/$1$2/xg;
return $str;
}
sub balanced_delim {
my($str) = @_;
my @str = split //, $str;
my($ar, $open, $close, $fail, $c, $cnt, $last_bs);
for $ar (['[',']'], ['(',')'], ['<','>'], ['{','}']) {
($open, $close) = @$ar;
$fail = 0; $cnt = 0; $last_bs = 0;
for $c (@str) {
if ($c eq $open) {
$fail = 1 if $last_bs;
$cnt++;
} elsif ($c eq $close) {
$fail = 1 if $last_bs;
$cnt--;
if ($cnt < 0) {
# qq()() isn't ")("
$fail = 1;
last;
}
}
$last_bs = $c eq '\\';
}
$fail = 1 if $cnt != 0;
return ($open, "$open$str$close") if not $fail;
}
return ("", $str);
}
sub single_delim {
my($q, $default, $str, $self) = @_;
return "$default$str$default" if $default and index($str, $default) == -1;
my $coreq = $self->keyword($q); # maybe CORE::q
if ($q ne 'qr') {
(my $succeed, $str) = balanced_delim($str);
return "$coreq$str" if $succeed;
}
for my $delim ('/', '"', '#') {
return "$coreq$delim" . $str . $delim if index($str, $delim) == -1;
}
if ($default) {
$str =~ s/$default/\\$default/g;
return "$default$str$default";
} else {
$str =~ s[/][\\/]g;
return "$coreq/$str/";
}
}
my $max_prec;
BEGIN { $max_prec = int(0.999 + 8*length(pack("F", 42))*log(2)/log(10)); }
# Split a floating point number into an integer mantissa and a binary
# exponent. Assumes you've already made sure the number isn't zero or
# some weird infinity or NaN.
sub split_float {
my($f) = @_;
my $exponent = 0;
if ($f == int($f)) {
while ($f % 2 == 0) {
$f /= 2;
$exponent++;
}
} else {
while ($f != int($f)) {
$f *= 2;
$exponent--;
}
}
my $mantissa = sprintf("%.0f", $f);
return ($mantissa, $exponent);
}
# suitably single- or double-quote a literal constant string
sub quoted_const_str {
my ($self, $str) =@_;
if ($str =~ /[[:^print:]]/a) {
return single_delim("qq", '"',
uninterp(escape_str unback $str), $self);
} else {
return single_delim("q", "'", unback($str), $self);
}
}
sub const {
my $self = shift;
my($sv, $cx) = @_;
if ($self->{'use_dumper'}) {
return $self->const_dumper($sv, $cx);
}
if (class($sv) eq "SPECIAL") {
# sv_undef, sv_yes, sv_no
return $$sv == 3 ? $self->maybe_parens("!1", $cx, 21)
: ('undef', '1')[$$sv-1];
}
if (class($sv) eq "NULL") {
return 'undef';
}
# convert a version object into the "v1.2.3" string in its V magic
if ($sv->FLAGS & SVs_RMG) {
for (my $mg = $sv->MAGIC; $mg; $mg = $mg->MOREMAGIC) {
return $mg->PTR if $mg->TYPE eq 'V';
}
}
if ($sv->FLAGS & SVf_IOK) {
my $str = $sv->int_value;
$str = $self->maybe_parens($str, $cx, 21) if $str < 0;
return $str;
} elsif ($sv->FLAGS & SVf_NOK) {
my $nv = $sv->NV;
if ($nv == 0) {
if (pack("F", $nv) eq pack("F", 0)) {
# positive zero
return "0";
} else {
# negative zero
return $self->maybe_parens("-.0", $cx, 21);
}
} elsif (1/$nv == 0) {
if ($nv > 0) {
# positive infinity
return $self->maybe_parens("9**9**9", $cx, 22);
} else {
# negative infinity
return $self->maybe_parens("-9**9**9", $cx, 21);
}
} elsif ($nv != $nv) {
# NaN
if (pack("F", $nv) eq pack("F", sin(9**9**9))) {
# the normal kind
return "sin(9**9**9)";
} elsif (pack("F", $nv) eq pack("F", -sin(9**9**9))) {
# the inverted kind
return $self->maybe_parens("-sin(9**9**9)", $cx, 21);
} else {
# some other kind
my $hex = unpack("h*", pack("F", $nv));
return qq'unpack("F", pack("h*", "$hex"))';
}
}
# first, try the default stringification
my $str = "$nv";
if ($str != $nv) {
# failing that, try using more precision
$str = sprintf("%.${max_prec}g", $nv);
# if (pack("F", $str) ne pack("F", $nv)) {
if ($str != $nv) {
# not representable in decimal with whatever sprintf()
# and atof() Perl is using here.
my($mant, $exp) = split_float($nv);
return $self->maybe_parens("$mant * 2**$exp", $cx, 19);
}
}
$str = $self->maybe_parens($str, $cx, 21) if $nv < 0;
return $str;
} elsif ($sv->FLAGS & SVf_ROK && $sv->can("RV")) {
my $ref = $sv->RV;
my $class = class($ref);
if ($class eq "AV") {
return "[" . $self->list_const(2, $ref->ARRAY) . "]";
} elsif ($class eq "HV") {
my %hash = $ref->ARRAY;
my @elts;
for my $k (sort keys %hash) {
push @elts, "$k => " . $self->const($hash{$k}, 6);
}
return "{" . join(", ", @elts) . "}";
} elsif ($class eq "CV") {
no overloading;
if ($self->{curcv} &&
$self->{curcv}->object_2svref == $ref->object_2svref) {
return $self->keyword("__SUB__");
}
return "sub " . $self->deparse_sub($ref);
}
if ($class ne 'SPECIAL' and $ref->FLAGS & SVs_SMG) {
for (my $mg = $ref->MAGIC; $mg; $mg = $mg->MOREMAGIC) {
if ($mg->TYPE eq 'r') {
my $re = re_uninterp(escape_re(re_unback($mg->precomp)));
return single_delim("qr", "", $re, $self);
}
}
}
my $const = $self->const($ref, 20);
if ($self->{in_subst_repl} && $const =~ /^[0-9]/) {
$const = "($const)";
}
return $self->maybe_parens("\\$const", $cx, 20);
} elsif ($sv->FLAGS & SVf_POK) {
my $str = $sv->PV;
return $self->quoted_const_str($str);
} else {
return "undef";
}
}
sub const_dumper {
my $self = shift;
my($sv, $cx) = @_;
my $ref = $sv->object_2svref();
my $dumper = Data::Dumper->new([$$ref], ['$v']);
$dumper->Purity(1)->Terse(1)->Deparse(1)->Indent(0)->Useqq(1)->Sortkeys(1);
my $str = $dumper->Dump();
if ($str =~ /^\$v/) {
return '${my ' . $str . ' \$v}';
} else {
return $str;
}
}
sub const_sv {
my $self = shift;
my $op = shift;
my $sv = $op->sv;
# the constant could be in the pad (under useithreads)
$sv = $self->padval($op->targ) unless $$sv;
return $sv;
}
sub meth_sv {
my $self = shift;
my $op = shift;
my $sv = $op->meth_sv;
# the constant could be in the pad (under useithreads)
$sv = $self->padval($op->targ) unless $$sv;
return $sv;
}
sub meth_rclass_sv {
my $self = shift;
my $op = shift;
my $sv = $op->rclass;
# the constant could be in the pad (under useithreads)
$sv = $self->padval($sv) unless ref $sv;
return $sv;
}
sub pp_const {
my $self = shift;
my($op, $cx) = @_;
# if ($op->private & OPpCONST_BARE) { # trouble with '=>' autoquoting
# return $self->const_sv($op)->PV;
# }
my $sv = $self->const_sv($op);
return $self->const($sv, $cx);
}
# Join two components of a double-quoted string, disambiguating
# "${foo}bar", "${foo}{bar}", "${foo}[1]", "$foo\::bar"
sub dq_disambiguate {
my ($first, $last) = @_;
($last =~ /^[A-Z\\\^\[\]_?]/ &&
$first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc
|| ($last =~ /^[:'{\[\w_]/ && #'
$first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/);
return $first . $last;
}
# Deparse a double-quoted optree. For example, "$a[0]\Q$b\Efo\"o" gets
# compiled to concat(concat($[0],quotemeta($b)),const("fo\"o")), and this
# sub deparses it back to $a[0]\Q$b\Efo"o
# (It does not add delimiters)
sub dq {
my $self = shift;
my $op = shift;
my $type = $op->name;
if ($type eq "const") {
return uninterp(escape_str(unback($self->const_sv($op)->as_string)));
} elsif ($type eq "concat") {
return dq_disambiguate($self->dq($op->first), $self->dq($op->last));
} elsif ($type eq "multiconcat") {
return $self->do_multiconcat($op, 26, 1);
} elsif ($type eq "uc") {
return '\U' . $self->dq($op->first->sibling) . '\E';
} elsif ($type eq "lc") {
return '\L' . $self->dq($op->first->sibling) . '\E';
} elsif ($type eq "ucfirst") {
return '\u' . $self->dq($op->first->sibling);
} elsif ($type eq "lcfirst") {
return '\l' . $self->dq($op->first->sibling);
} elsif ($type eq "quotemeta") {
return '\Q' . $self->dq($op->first->sibling) . '\E';
} elsif ($type eq "fc") {
return '\F' . $self->dq($op->first->sibling) . '\E';
} elsif ($type eq "join") {
return $self->deparse($op->last, 26); # was join($", @ary)
} else {
return $self->deparse($op, 26);
}
}
sub pp_backtick {
my $self = shift;
my($op, $cx) = @_;
# skip pushmark if it exists (readpipe() vs ``)
my $child = $op->first->sibling->isa('B::NULL')
? $op->first : $op->first->sibling;
if ($self->pure_string($child)) {
return single_delim("qx", '`', $self->dq($child, 1), $self);
}
unop($self, @_, "readpipe");
}
sub dquote {
my $self = shift;
my($op, $cx) = @_;
my $kid = $op->first->sibling; # skip ex-stringify, pushmark
return $self->deparse($kid, $cx) if $self->{'unquote'};
$self->maybe_targmy($kid, $cx,
sub {single_delim("qq", '"', $self->dq($_[1]),
$self)});
}
# OP_STRINGIFY is a listop, but it only ever has one arg
sub pp_stringify {
my ($self, $op, $cx) = @_;
my $kid = $op->first->sibling;
while ($kid->name eq 'null' && !null($kid->first)) {
$kid = $kid->first;
}
if ($kid->name =~ /^(?:const|padsv|rv2sv|av2arylen|gvsv|multideref
|aelemfast(?:_lex)?|[ah]elem|join|concat)\z/x) {
maybe_targmy(@_, \&dquote);
}
else {
# Actually an optimised join.
my $result = listop(@_,"join");
$result =~ s/join([( ])/join$1$self->{'ex_const'}, /;
$result;
}
}
# tr/// and s/// (and tr[][], tr[]//, tr###, etc)
# note that tr(from)/to/ is OK, but not tr/from/(to)
sub double_delim {
my($from, $to) = @_;
my($succeed, $delim);
if ($from !~ m[/] and $to !~ m[/]) {
return "/$from/$to/";
} elsif (($succeed, $from) = balanced_delim($from) and $succeed) {
if (($succeed, $to) = balanced_delim($to) and $succeed) {
return "$from$to";
} else {
for $delim ('/', '"', '#') { # note no "'" -- s''' is special
return "$from$delim$to$delim" if index($to, $delim) == -1;
}
$to =~ s[/][\\/]g;
return "$from/$to/";
}
} else {
for $delim ('/', '"', '#') { # note no '
return "$delim$from$delim$to$delim"
if index($to . $from, $delim) == -1;
}
$from =~ s[/][\\/]g;
$to =~ s[/][\\/]g;
return "/$from/$to/";
}
}
# Escape a characrter.
# Only used by tr///, so backslashes hyphens
sub pchr { # ASCII
my($n) = @_;
if ($n == ord '\\') {
return '\\\\';
} elsif ($n == ord "-") {
return "\\-";
} elsif (utf8::native_to_unicode($n) >= utf8::native_to_unicode(ord(' '))
and utf8::native_to_unicode($n) <= utf8::native_to_unicode(ord('~')))
{
# I'm presuming a regex is not ok here, otherwise we could have used
# /[[:print:]]/a to get here
return chr($n);
} elsif ($n == ord "\a") {
return '\\a';
} elsif ($n == ord "\b") {
return '\\b';
} elsif ($n == ord "\t") {
return '\\t';
} elsif ($n == ord "\n") {
return '\\n';
} elsif ($n == ord "\e") {
return '\\e';
} elsif ($n == ord "\f") {
return '\\f';
} elsif ($n == ord "\r") {
return '\\r';
} elsif ($n >= ord("\cA") and $n <= ord("\cZ")) {
return '\\c' . $unctrl{chr $n};
} else {
# return '\x' . sprintf("%02x", $n);
return '\\' . sprintf("%03o", $n);
}
}
# Convert a list of characters into a string suitable for tr/// search or
# replacement, with suitable escaping and collapsing of ranges
sub collapse {
my(@chars) = @_;
my($str, $c, $tr) = ("");
for ($c = 0; $c < @chars; $c++) {
$tr = $chars[$c];
$str .= pchr($tr);
if ($c <= $#chars - 2 and $chars[$c + 1] == $tr + 1 and
$chars[$c + 2] == $tr + 2)
{
for (; $c <= $#chars-1 and $chars[$c + 1] == $chars[$c] + 1; $c++)
{}
$str .= "-";
$str .= pchr($chars[$c]);
}
}
return $str;
}
sub tr_decode_byte {
my($table, $flags) = @_;
my $ssize_t = $Config{ptrsize} == 8 ? 'q' : 'l';
my ($size, @table) = unpack("${ssize_t}s*", $table);
pop @table; # remove the wildcard final entry
my($c, $tr, @from, @to, @delfrom, $delhyphen);
if ($table[ord "-"] != -1 and
$table[ord("-") - 1] == -1 || $table[ord("-") + 1] == -1)
{
$tr = $table[ord "-"];
$table[ord "-"] = -1;
if ($tr >= 0) {
@from = ord("-");
@to = $tr;
} else { # -2 ==> delete
$delhyphen = 1;
}
}
for ($c = 0; $c < @table; $c++) {
$tr = $table[$c];
if ($tr >= 0) {
push @from, $c; push @to, $tr;
} elsif ($tr == -2) {
push @delfrom, $c;
}
}
@from = (@from, @delfrom);
if ($flags & OPpTRANS_COMPLEMENT) {
unless ($flags & OPpTRANS_DELETE) {
@to = () if ("@from" eq "@to");
}
my @newfrom = ();
my %from;
@from{@from} = (1) x @from;
for ($c = 0; $c < 256; $c++) {
push @newfrom, $c unless $from{$c};
}
@from = @newfrom;
}
unless ($flags & OPpTRANS_DELETE || !@to) {
pop @to while $#to and $to[$#to] == $to[$#to -1];
}
my($from, $to);
$from = collapse(@from);
$to = collapse(@to);
$from .= "-" if $delhyphen;
return ($from, $to);
}
sub tr_chr {
my $x = shift;
if ($x == ord "-") {
return "\\-";
} elsif ($x == ord "\\") {
return "\\\\";
} else {
return chr $x;
}
}
sub tr_invmap {
my ($invlist_ref, $map_ref) = @_;
my $infinity = ~0 >> 1; # IV_MAX
my $from = "";
my $to = "";
for my $i (0.. @$invlist_ref - 1) {
my $this_from = $invlist_ref->[$i];
my $map = $map_ref->[$i];
my $upper = ($i < @$invlist_ref - 1)
? $invlist_ref->[$i+1]
: $infinity;
my $range = $upper - $this_from - 1;
if (DEBUG) {
print STDERR "i=$i, from=$this_from, upper=$upper, range=$range\n";
}
next if $map == ~0;
next if $map == ~0 - 1;
$from .= tr_chr($this_from);
$to .= tr_chr($map);
next if $range == 0; # Single code point
if ($range == 1) { # Adjacent code points
$from .= tr_chr($this_from + 1);
$to .= tr_chr($map + 1);
}
elsif ($upper != $infinity) {
$from .= "-" . tr_chr($this_from + $range);
$to .= "-" . tr_chr($map + $range);
}
else {
$from .= "-INFTY";
$to .= "-INFTY";
}
}
return ($from, $to);
}
sub tr_decode_utf8 {
my($tr_av, $flags) = @_;
printf STDERR "flags=0x%x\n", $flags if DEBUG;
my $invlist = $tr_av->ARRAYelt(0);
my @invlist = unpack("J*", $invlist->PV);
my @map = unpack("J*", $tr_av->ARRAYelt(1)->PV);
if (DEBUG) {
for my $i (0 .. @invlist - 1) {
printf STDERR "[%d]\t%x\t", $i, $invlist[$i];
my $map = $map[$i];
if ($map == ~0) {
print STDERR "TR_UNMAPPED\n";
}
elsif ($map == ~0 - 1) {
print STDERR "TR_SPECIAL\n";
}
else {
printf STDERR "%x\n", $map;
}
}
}
my ($from, $to) = tr_invmap(\@invlist, \@map);
if ($flags & OPpTRANS_COMPLEMENT) {
shift @map;
pop @invlist;
my $throw_away;
($from, $throw_away) = tr_invmap(\@invlist, \@map);
}
if (DEBUG) {
print STDERR "Returning ", escape_str($from), "/",
escape_str($to), "\n";
}
return (escape_str($from), escape_str($to));
}
sub pp_trans {
my $self = shift;
my($op, $cx, $morflags) = @_;
my($from, $to);
my $class = class($op);
my $priv_flags = $op->private;
if ($class eq "PVOP") {
($from, $to) = tr_decode_byte($op->pv, $priv_flags);
} elsif ($class eq "PADOP") {
($from, $to)
= tr_decode_utf8($self->padval($op->padix), $priv_flags);
} else { # class($op) eq "SVOP"
($from, $to) = tr_decode_utf8($op->sv, $priv_flags);
}
my $flags = "";
$flags .= "c" if $priv_flags & OPpTRANS_COMPLEMENT;
$flags .= "d" if $priv_flags & OPpTRANS_DELETE;
$to = "" if $from eq $to and $flags eq "";
$flags .= "s" if $priv_flags & OPpTRANS_SQUASH;
$flags .= $morflags if defined $morflags;
my $ret = $self->keyword("tr") . double_delim($from, $to) . $flags;
if (my $targ = $op->targ) {
return $self->maybe_parens($self->padname($targ) . " =~ $ret",
$cx, 20);
}
return $ret;
}
sub pp_transr { push @_, 'r'; goto &pp_trans }
# Join two components of a double-quoted re, disambiguating
# "${foo}bar", "${foo}{bar}", "${foo}[1]".
sub re_dq_disambiguate {
my ($first, $last) = @_;
($last =~ /^[A-Z\\\^\[\]_?]/ &&
$first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc
|| ($last =~ /^[{\[\w_]/ &&
$first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/);
return $first . $last;
}
# Like dq(), but different
sub re_dq {
my $self = shift;
my ($op) = @_;
my $type = $op->name;
if ($type eq "const") {
my $unbacked = re_unback($self->const_sv($op)->as_string);
return re_uninterp(escape_re($unbacked));
} elsif ($type eq "concat") {
my $first = $self->re_dq($op->first);
my $last = $self->re_dq($op->last);
return re_dq_disambiguate($first, $last);
} elsif ($type eq "multiconcat") {
return $self->do_multiconcat($op, 26, 2);
} elsif ($type eq "uc") {
return '\U' . $self->re_dq($op->first->sibling) . '\E';
} elsif ($type eq "lc") {
return '\L' . $self->re_dq($op->first->sibling) . '\E';
} elsif ($type eq "ucfirst") {
return '\u' . $self->re_dq($op->first->sibling);
} elsif ($type eq "lcfirst") {
return '\l' . $self->re_dq($op->first->sibling);
} elsif ($type eq "quotemeta") {
return '\Q' . $self->re_dq($op->first->sibling) . '\E';
} elsif ($type eq "fc") {
return '\F' . $self->re_dq($op->first->sibling) . '\E';
} elsif ($type eq "join") {
return $self->deparse($op->last, 26); # was join($", @ary)
} else {
my $ret = $self->deparse($op, 26);
$ret =~ s/^\$([(|)])\z/\${$1}/ # $( $| $) need braces
or $ret =~ s/^\@([-+])\z/\@{$1}/; # @- @+ need braces
return $ret;
}
}
sub pure_string {
my ($self, $op) = @_;
return 0 if null $op;
my $type = $op->name;
if ($type eq 'const' || $type eq 'av2arylen') {
return 1;
}
elsif ($type =~ /^(?:[ul]c(first)?|fc)$/ || $type eq 'quotemeta') {
return $self->pure_string($op->first->sibling);
}
elsif ($type eq 'join') {
my $join_op = $op->first->sibling; # Skip pushmark
return 0 unless $join_op->name eq 'null' && $join_op->targ == OP_RV2SV;
my $gvop = $join_op->first;
return 0 unless $gvop->name eq 'gvsv';
return 0 unless '"' eq $self->gv_name($self->gv_or_padgv($gvop));
return 0 unless ${$join_op->sibling} eq ${$op->last};
return 0 unless $op->last->name =~ /^(?:[ah]slice|(?:rv2|pad)av)$/;
}
elsif ($type eq 'concat') {
return $self->pure_string($op->first)
&& $self->pure_string($op->last);
}
elsif ($type eq 'multiconcat') {
my ($kid, @kids);
for ($kid = $op->first; !null $kid; $kid = $kid->sibling) {
# skip the consts and/or padsv we've optimised away
push @kids, $kid
unless $kid->type == OP_NULL
&& ( $kid->targ == OP_PADSV
|| $kid->targ == OP_CONST
|| $kid->targ == OP_PUSHMARK);
}
if ($op->flags & OPf_STACKED) {
# remove expr from @kids where 'expr = ...' or 'expr .= ....'
if ($op->private & OPpMULTICONCAT_APPEND) {
shift(@kids);
}
else {
pop(@kids);
}
}
for (@kids) {
return 0 unless $self->pure_string($_);
}
return 1;
}
elsif (is_scalar($op) || $type =~ /^[ah]elem$/) {
return 1;
}
elsif ($type eq "null" and $op->can('first') and not null $op->first) {
my $first = $op->first;
return 1 if $first->name eq "multideref";
return 1 if $first->name eq "aelemfast_lex";
if ( $first->name eq "null"
and $first->can('first')
and not null $first->first
and $first->first->name eq "aelemfast"
)
{
return 1;
}
}
return 0;
}
sub code_list {
my ($self,$op,$cv) = @_;
# localise stuff relating to the current sub
$cv and
local($self->{'curcv'}) = $cv,
local($self->{'curcvlex'}),
local(@$self{qw'curstash warnings hints hinthash curcop'})
= @$self{qw'curstash warnings hints hinthash curcop'};
my $re;
for ($op = $op->first->sibling; !null($op); $op = $op->sibling) {
if ($op->name eq 'null' and $op->flags & OPf_SPECIAL) {
my $scope = $op->first;
# 0 context (last arg to scopeop) means statement context, so
# the contents of the block will not be wrapped in do{...}.
my $block = scopeop($scope->first->name eq "enter", $self,
$scope, 0);
# next op is the source code of the block
$op = $op->sibling;
$re .= ($self->const_sv($op)->PV =~ m|^(\(\?\??\{)|)[0];
my $multiline = $block =~ /\n/;
$re .= $multiline ? "\n\t" : ' ';
$re .= $block;
$re .= $multiline ? "\n\b})" : " })";
} else {
$re = re_dq_disambiguate($re, $self->re_dq($op));
}
}
$re;
}
sub regcomp {
my $self = shift;
my($op, $cx) = @_;
my $kid = $op->first;
$kid = $kid->first if $kid->name eq "regcmaybe";
$kid = $kid->first if $kid->name eq "regcreset";
my $kname = $kid->name;
if ($kname eq "null" and !null($kid->first)
and $kid->first->name eq 'pushmark')
{
my $str = '';
$kid = $kid->first->sibling;
while (!null($kid)) {
my $first = $str;
my $last = $self->re_dq($kid);
$str = re_dq_disambiguate($first, $last);
$kid = $kid->sibling;
}
return $str, 1;
}
return ($self->re_dq($kid), 1)
if $kname =~ /^(?:rv2|pad)av/ or $self->pure_string($kid);
return ($self->deparse($kid, $cx), 0);
}
sub pp_regcomp {
my ($self, $op, $cx) = @_;
return (($self->regcomp($op, $cx, 0))[0]);
}
sub re_flags {
my ($self, $op) = @_;
my $flags = '';
my $pmflags = $op->pmflags;
if (!$pmflags) {
my $re = $op->pmregexp;
if ($$re) {
$pmflags = $re->compflags;
}
}
$flags .= "g" if $pmflags & PMf_GLOBAL;
$flags .= "i" if $pmflags & PMf_FOLD;
$flags .= "m" if $pmflags & PMf_MULTILINE;
$flags .= "o" if $pmflags & PMf_KEEP;
$flags .= "s" if $pmflags & PMf_SINGLELINE;
$flags .= "x" if $pmflags & PMf_EXTENDED;
$flags .= "x" if $pmflags & PMf_EXTENDED_MORE;
$flags .= "p" if $pmflags & PMf_KEEPCOPY;
$flags .= "n" if $pmflags & PMf_NOCAPTURE;
if (my $charset = $pmflags & PMf_CHARSET) {
# Hardcoding this is fragile, but B does not yet export the
# constants we need.
$flags .= qw(d l u a aa)[$charset >> 7]
}
# The /d flag is indicated by 0; only show it if necessary.
elsif ($self->{hinthash} and
$self->{hinthash}{reflags_charset}
|| $self->{hinthash}{feature_unicode}
or $self->{hints} & $feature::hint_mask
&& ($self->{hints} & $feature::hint_mask)
!= $feature::hint_mask
&& $self->{hints} & $feature::hint_uni8bit
) {
$flags .= 'd';
}
$flags;
}
# osmic acid -- see osmium tetroxide
my %matchwords;
map($matchwords{join "", sort split //, $_} = $_, 'cig', 'cog', 'cos', 'cogs',
'cox', 'go', 'is', 'ism', 'iso', 'mig', 'mix', 'osmic', 'ox', 'sic',
'sig', 'six', 'smog', 'so', 'soc', 'sog', 'xi', 'soup', 'soupmix');
# When deparsing a regular expression with code blocks, we have to look in
# various places to find the blocks.
#
# For qr/(?{...})/ without interpolation, the CV is under $qr->qr_anoncv
# and the code list (list of blocks and constants, maybe vars) is under
# $cv->ROOT->first->code_list:
# ./perl -Ilib -MB -e 'use O "Concise", B::svref_2object(sub {qr/(?{die})/})->ROOT->first->first->sibling->pmregexp->qr_anoncv->object_2svref'
#
# For qr/$a(?{...})/ with interpolation, the code list is more accessible,
# under $pmop->code_list, but the $cv is something you have to dig for in
# the regcomp op’s kids:
# ./perl -Ilib -mO=Concise -e 'qr/$a(?{die})/'
#
# For m// and split //, things are much simpler. There is no CV. The code
# list is under $pmop->code_list.
sub matchop {
my $self = shift;
my($op, $cx, $name, $delim) = @_;
my $kid = $op->first;
my ($binop, $var, $re) = ("", "", "");
if ($op->name ne 'split' && $op->flags & OPf_STACKED) {
$binop = 1;
$var = $self->deparse($kid, 20);
$kid = $kid->sibling;
}
# not $name; $name will be 'm' for both match and split
elsif ($op->name eq 'match' and my $targ = $op->targ) {
$binop = 1;
$var = $self->padname($targ);
}
my $quote = 1;
my $pmflags = $op->pmflags;
my $rhs_bound_to_defsv;
my ($cv, $bregexp);
my $have_kid = !null $kid;
# Check for code blocks first
if (not null my $code_list = $op->code_list) {
$re = $self->code_list($code_list,
$op->name eq 'qr'
? $self->padval(
$kid->first # ex-list
->first # pushmark
->sibling # entersub
->first # ex-list
->first # pushmark
->sibling # srefgen
->first # ex-list
->first # anoncode
->targ
)
: undef);
} elsif (${$bregexp = $op->pmregexp} && ${$cv = $bregexp->qr_anoncv}) {
my $patop = $cv->ROOT # leavesub
->first # qr
->code_list;# list
$re = $self->code_list($patop, $cv);
} elsif (!$have_kid) {
$re = re_uninterp(escape_re(re_unback($op->precomp)));
} elsif ($kid->name ne 'regcomp') {
if ($op->name eq 'split') {
# split has other kids, not just regcomp
$re = re_uninterp(escape_re(re_unback($op->precomp)));
}
else {
carp("found ".$kid->name." where regcomp expected");
}
} else {
($re, $quote) = $self->regcomp($kid, 21);
}
if ($have_kid and $kid->name eq 'regcomp') {
my $matchop = $kid->first;
if ($matchop->name eq 'regcreset') {
$matchop = $matchop->first;
}
if ($matchop->name =~ /^(?:match|transr?|subst)\z/
&& $matchop->flags & OPf_SPECIAL) {
$rhs_bound_to_defsv = 1;
}
}
my $flags = "";
$flags .= "c" if $pmflags & PMf_CONTINUE;
$flags .= $self->re_flags($op);
$flags = join '', sort split //, $flags;
$flags = $matchwords{$flags} if $matchwords{$flags};
if ($pmflags & PMf_ONCE) { # only one kind of delimiter works here
$re =~ s/\?/\\?/g;
$re = $self->keyword("m") . "?$re?"; # explicit 'm' is required
} elsif ($quote) {
$re = single_delim($name, $delim, $re, $self);
}
$re = $re . $flags if $quote;
if ($binop) {
return
$self->maybe_parens(
$rhs_bound_to_defsv
? "$var =~ (\$_ =~ $re)"
: "$var =~ $re",
$cx, 20
);
} else {
return $re;
}
}
sub pp_match { matchop(@_, "m", "/") }
sub pp_qr { matchop(@_, "qr", "") }
sub pp_runcv { unop(@_, "__SUB__"); }
sub pp_split {
my $self = shift;
my($op, $cx) = @_;
my($kid, @exprs, $ary, $expr);
my $stacked = $op->flags & OPf_STACKED;
$kid = $op->first;
$kid = $kid->sibling if $kid->name eq 'regcomp';
for (; !null($kid); $kid = $kid->sibling) {
push @exprs, $self->deparse($kid, 6);
}
unshift @exprs, $self->matchop($op, $cx, "m", "/");
if ($op->private & OPpSPLIT_ASSIGN) {
# With C<@array = split(/pat/, str);>,
# array is stored in split's pmreplroot; either
# as an integer index into the pad (for a lexical array)
# or as GV for a package array (which will be a pad index
# on threaded builds)
# With my/our @array = split(/pat/, str), the array is instead
# accessed via an extra padav/rv2av op at the end of the
# split's kid ops.
if ($stacked) {
$ary = pop @exprs;
}
else {
if ($op->private & OPpSPLIT_LEX) {
$ary = $self->padname($op->pmreplroot);
}
else {
# union with op_pmtargetoff, op_pmtargetgv
my $gv = $op->pmreplroot;
$gv = $self->padval($gv) if !ref($gv);
$ary = $self->maybe_local(@_,
$self->stash_variable('@',
$self->gv_name($gv),
$cx))
}
if ($op->private & OPpLVAL_INTRO) {
$ary = $op->private & OPpSPLIT_LEX ? "my $ary" : "local $ary";
}
}
}
# handle special case of split(), and split(' ') that compiles to /\s+/
$exprs[0] = q{' '} if ($op->reflags // 0) & RXf_SKIPWHITE();
$expr = "split(" . join(", ", @exprs) . ")";
if ($ary) {
return $self->maybe_parens("$ary = $expr", $cx, 7);
} else {
return $expr;
}
}
# oxime -- any of various compounds obtained chiefly by the action of
# hydroxylamine on aldehydes and ketones and characterized by the
# bivalent grouping C=NOH [Webster's Tenth]
my %substwords;
map($substwords{join "", sort split //, $_} = $_, 'ego', 'egoism', 'em',
'es', 'ex', 'exes', 'gee', 'go', 'goes', 'ie', 'ism', 'iso', 'me',
'meese', 'meso', 'mig', 'mix', 'os', 'ox', 'oxime', 'see', 'seem',
'seg', 'sex', 'sig', 'six', 'smog', 'sog', 'some', 'xi', 'rogue',
'sir', 'rise', 'smore', 'more', 'seer', 'rome', 'gore', 'grim', 'grime',
'or', 'rose', 'rosie');
sub pp_subst {
my $self = shift;
my($op, $cx) = @_;
my $kid = $op->first;
my($binop, $var, $re, $repl) = ("", "", "", "");
if ($op->flags & OPf_STACKED) {
$binop = 1;
$var = $self->deparse($kid, 20);
$kid = $kid->sibling;
}
elsif (my $targ = $op->targ) {
$binop = 1;
$var = $self->padname($targ);
}
my $flags = "";
my $pmflags = $op->pmflags;
if (null($op->pmreplroot)) {
$repl = $kid;
$kid = $kid->sibling;
} else {
$repl = $op->pmreplroot->first; # skip substcont
}
while ($repl->name eq "entereval") {
$repl = $repl->first;
$flags .= "e";
}
{
local $self->{in_subst_repl} = 1;
if ($pmflags & PMf_EVAL) {
$repl = $self->deparse($repl->first, 0);
} else {
$repl = $self->dq($repl);
}
}
if (not null my $code_list = $op->code_list) {
$re = $self->code_list($code_list);
} elsif (null $kid) {
$re = re_uninterp(escape_re(re_unback($op->precomp)));
} else {
($re) = $self->regcomp($kid, 1);
}
$flags .= "r" if $pmflags & PMf_NONDESTRUCT;
$flags .= "e" if $pmflags & PMf_EVAL;
$flags .= $self->re_flags($op);
$flags = join '', sort split //, $flags;
$flags = $substwords{$flags} if $substwords{$flags};
my $core_s = $self->keyword("s"); # maybe CORE::s
if ($binop) {
return $self->maybe_parens("$var =~ $core_s"
. double_delim($re, $repl) . $flags,
$cx, 20);
} else {
return "$core_s". double_delim($re, $repl) . $flags;
}
}
sub is_lexical_subs {
my (@ops) = shift;
for my $op (@ops) {
return 0 if $op->name !~ /\A(?:introcv|clonecv)\z/;
}
return 1;
}
# Pretend these two ops do not exist. The perl parser adds them to the
# beginning of any block containing my-sub declarations, whereas we handle
# the subs in pad_subs and next_todo.
*pp_clonecv = *pp_introcv;
sub pp_introcv {
my $self = shift;
my($op, $cx) = @_;
# For now, deparsing doesn't worry about the distinction between introcv
# and clonecv, so pretend this op doesn't exist:
return '';
}
sub pp_padcv {
my $self = shift;
my($op, $cx) = @_;
return $self->padany($op);
}
my %lvref_funnies = (
OPpLVREF_SV, => '$',
OPpLVREF_AV, => '@',
OPpLVREF_HV, => '%',
OPpLVREF_CV, => '&',
);
sub pp_refassign {
my ($self, $op, $cx) = @_;
my $left;
if ($op->private & OPpLVREF_ELEM) {
$left = $op->first->sibling;
$left = maybe_local(@_, elem($self, $left, undef,
$left->targ == OP_AELEM
? qw([ ] padav)
: qw({ } padhv)));
} elsif ($op->flags & OPf_STACKED) {
$left = maybe_local(@_,
$lvref_funnies{$op->private & OPpLVREF_TYPE}
. $self->deparse($op->first->sibling));
} else {
$left = &pp_padsv;
}
my $right = $self->deparse_binop_right($op, $op->first, 7);
return $self->maybe_parens("\\$left = $right", $cx, 7);
}
sub pp_lvref {
my ($self, $op, $cx) = @_;
my $code;
if ($op->private & OPpLVREF_ELEM) {
$code = $op->first->name =~ /av\z/ ? &pp_aelem : &pp_helem;
} elsif ($op->flags & OPf_STACKED) {
$code = maybe_local(@_,
$lvref_funnies{$op->private & OPpLVREF_TYPE}
. $self->deparse($op->first));
} else {
$code = &pp_padsv;
}
"\\$code";
}
sub pp_lvrefslice {
my ($self, $op, $cx) = @_;
'\\' . ($op->last->name =~ /av\z/ ? &pp_aslice : &pp_hslice);
}
sub pp_lvavref {
my ($self, $op, $cx) = @_;
'\\(' . ($op->flags & OPf_STACKED
? maybe_local(@_, rv2x(@_, "\@"))
: &pp_padsv) . ')'
}
sub pp_argcheck {
my $self = shift;
my($op, $cx) = @_;
my ($params, $opt_params, $slurpy) = $op->aux_list($self->{curcv});
my $mandatory = $params - $opt_params;
my $check = '';
$check .= <<EOF if !$slurpy;
die sprintf("Too many arguments for subroutine at %s line %d.\\n", (caller)[1, 2]) unless \@_ <= $params;
EOF
$check .= <<EOF if $mandatory > 0;
die sprintf("Too few arguments for subroutine at %s line %d.\\n", (caller)[1, 2]) unless \@_ >= $mandatory;
EOF
my $cond = ($params & 1) ? 'unless' : 'if';
$check .= <<EOF if $slurpy eq '%';
die sprintf("Odd name/value argument for subroutine at %s line %d.\\n", (caller)[1, 2]) if \@_ > $params && ((\@_ - $params) & 1);
EOF
$check =~ s/;\n\z//;
return $check;
}
sub pp_argelem {
my $self = shift;
my($op, $cx) = @_;
my $var = $self->padname($op->targ);
my $ix = $op->string($self->{curcv});
my $expr;
if ($op->flags & OPf_KIDS) {
$expr = $self->deparse($op->first, 7);
}
elsif ($var =~ /^[@%]/) {
$expr = $ix ? "\@_[$ix .. \$#_]" : '@_';
}
else {
$expr = "\$_[$ix]";
}
return "my $var = $expr";
}
sub pp_argdefelem {
my $self = shift;
my($op, $cx) = @_;
my $ix = $op->targ;
my $expr = "\@_ >= " . ($ix+1) . " ? \$_[$ix] : ";
my $def = $self->deparse($op->first, 7);
$def = "($def)" if $op->first->flags & OPf_PARENS;
$expr .= $self->deparse($op->first, $cx);
return $expr;
}
1;
__END__
=head1 NAME
B::Deparse - Perl compiler backend to produce perl code
=head1 SYNOPSIS
B<perl> B<-MO=Deparse>[B<,-d>][B<,-f>I<FILE>][B<,-p>][B<,-q>][B<,-l>]
[B<,-s>I<LETTERS>][B<,-x>I<LEVEL>] I<prog.pl>
=head1 DESCRIPTION
B::Deparse is a backend module for the Perl compiler that generates
perl source code, based on the internal compiled structure that perl
itself creates after parsing a program. The output of B::Deparse won't
be exactly the same as the original source, since perl doesn't keep
track of comments or whitespace, and there isn't a one-to-one
correspondence between perl's syntactical constructions and their
compiled form, but it will often be close. When you use the B<-p>
option, the output also includes parentheses even when they are not
required by precedence, which can make it easy to see if perl is
parsing your expressions the way you intended.
While B::Deparse goes to some lengths to try to figure out what your
original program was doing, some parts of the language can still trip
it up; it still fails even on some parts of Perl's own test suite. If
you encounter a failure other than the most common ones described in
the BUGS section below, you can help contribute to B::Deparse's
ongoing development by submitting a bug report with a small
example.
=head1 OPTIONS
As with all compiler backend options, these must follow directly after
the '-MO=Deparse', separated by a comma but not any white space.
=over 4
=item B<-d>
Output data values (when they appear as constants) using Data::Dumper.
Without this option, B::Deparse will use some simple routines of its
own for the same purpose. Currently, Data::Dumper is better for some
kinds of data (such as complex structures with sharing and
self-reference) while the built-in routines are better for others
(such as odd floating-point values).
=item B<-f>I<FILE>
Normally, B::Deparse deparses the main code of a program, and all the subs
defined in the same file. To include subs defined in
other files, pass the B<-f> option with the filename.
You can pass the B<-f> option several times, to
include more than one secondary file. (Most of the time you don't want to
use it at all.) You can also use this option to include subs which are
defined in the scope of a B<#line> directive with two parameters.
=item B<-l>
Add '#line' declarations to the output based on the line and file
locations of the original code.
=item B<-p>
Print extra parentheses. Without this option, B::Deparse includes
parentheses in its output only when they are needed, based on the
structure of your program. With B<-p>, it uses parentheses (almost)
whenever they would be legal. This can be useful if you are used to
LISP, or if you want to see how perl parses your input. If you say
if ($var & 0x7f == 65) {print "Gimme an A!"}
print ($which ? $a : $b), "\n";
$name = $ENV{USER} or "Bob";
C<B::Deparse,-p> will print
if (($var & 0)) {
print('Gimme an A!')
};
(print(($which ? $a : $b)), '???');
(($name = $ENV{'USER'}) or '???')
which probably isn't what you intended (the C<'???'> is a sign that
perl optimized away a constant value).
=item B<-P>
Disable prototype checking. With this option, all function calls are
deparsed as if no prototype was defined for them. In other words,
perl -MO=Deparse,-P -e 'sub foo (\@) { 1 } foo @x'
will print
sub foo (\@) {
1;
}
&foo(\@x);
making clear how the parameters are actually passed to C<foo>.
=item B<-q>
Expand double-quoted strings into the corresponding combinations of
concatenation, uc, ucfirst, lc, lcfirst, quotemeta, and join. For
instance, print
print "Hello, $world, @ladies, \u$gentlemen\E, \u\L$me!";
as
print 'Hello, ' . $world . ', ' . join($", @ladies) . ', '
. ucfirst($gentlemen) . ', ' . ucfirst(lc $me . '!');
Note that the expanded form represents the way perl handles such
constructions internally -- this option actually turns off the reverse
translation that B::Deparse usually does. On the other hand, note that
C<$x = "$y"> is not the same as C<$x = $y>: the former makes the value
of $y into a string before doing the assignment.
=item B<-s>I<LETTERS>
Tweak the style of B::Deparse's output. The letters should follow
directly after the 's', with no space or punctuation. The following
options are available:
=over 4
=item B<C>
Cuddle C<elsif>, C<else>, and C<continue> blocks. For example, print
if (...) {
...
} else {
...
}
instead of
if (...) {
...
}
else {
...
}
The default is not to cuddle.
=item B<i>I<NUMBER>
Indent lines by multiples of I<NUMBER> columns. The default is 4 columns.
=item B<T>
Use tabs for each 8 columns of indent. The default is to use only spaces.
For instance, if the style options are B<-si4T>, a line that's indented
3 times will be preceded by one tab and four spaces; if the options were
B<-si8T>, the same line would be preceded by three tabs.
=item B<v>I<STRING>B<.>
Print I<STRING> for the value of a constant that can't be determined
because it was optimized away (mnemonic: this happens when a constant
is used in B<v>oid context). The end of the string is marked by a period.
The string should be a valid perl expression, generally a constant.
Note that unless it's a number, it probably needs to be quoted, and on
a command line quotes need to be protected from the shell. Some
conventional values include 0, 1, 42, '', 'foo', and
'Useless use of constant omitted' (which may need to be
B<-sv"'Useless use of constant omitted'.">
or something similar depending on your shell). The default is '???'.
If you're using B::Deparse on a module or other file that's require'd,
you shouldn't use a value that evaluates to false, since the customary
true constant at the end of a module will be in void context when the
file is compiled as a main program.
=back
=item B<-x>I<LEVEL>
Expand conventional syntax constructions into equivalent ones that expose
their internal operation. I<LEVEL> should be a digit, with higher values
meaning more expansion. As with B<-q>, this actually involves turning off
special cases in B::Deparse's normal operations.
If I<LEVEL> is at least 3, C<for> loops will be translated into equivalent
while loops with continue blocks; for instance
for ($i = 0; $i < 10; ++$i) {
print $i;
}
turns into
$i = 0;
while ($i < 10) {
print $i;
} continue {
++$i
}
Note that in a few cases this translation can't be perfectly carried back
into the source code -- if the loop's initializer declares a my variable,
for instance, it won't have the correct scope outside of the loop.
If I<LEVEL> is at least 5, C<use> declarations will be translated into
C<BEGIN> blocks containing calls to C<require> and C<import>; for
instance,
use strict 'refs';
turns into
sub BEGIN {
require strict;
do {
'strict'->import('refs')
};
}
If I<LEVEL> is at least 7, C<if> statements will be translated into
equivalent expressions using C<&&>, C<?:> and C<do {}>; for instance
print 'hi' if $nice;
if ($nice) {
print 'hi';
}
if ($nice) {
print 'hi';
} else {
print 'bye';
}
turns into
$nice and print 'hi';
$nice and do { print 'hi' };
$nice ? do { print 'hi' } : do { print 'bye' };
Long sequences of elsifs will turn into nested ternary operators, which
B::Deparse doesn't know how to indent nicely.
=back
=head1 USING B::Deparse AS A MODULE
=head2 Synopsis
use B::Deparse;
$deparse = B::Deparse->new("-p", "-sC");
$body = $deparse->coderef2text(\&func);
eval "sub func $body"; # the inverse operation
=head2 Description
B::Deparse can also be used on a sub-by-sub basis from other perl
programs.
=head2 new
$deparse = B::Deparse->new(OPTIONS)
Create an object to store the state of a deparsing operation and any
options. The options are the same as those that can be given on the
command line (see L</OPTIONS>); options that are separated by commas
after B<-MO=Deparse> should be given as separate strings.
=head2 ambient_pragmas
$deparse->ambient_pragmas(strict => 'all', '$[' => $[);
The compilation of a subroutine can be affected by a few compiler
directives, B<pragmas>. These are:
=over 4
=item *
use strict;
=item *
use warnings;
=item *
Assigning to the special variable $[
=item *
use integer;
=item *
use bytes;
=item *
use utf8;
=item *
use re;
=back
Ordinarily, if you use B::Deparse on a subroutine which has
been compiled in the presence of one or more of these pragmas,
the output will include statements to turn on the appropriate
directives. So if you then compile the code returned by coderef2text,
it will behave the same way as the subroutine which you deparsed.
However, you may know that you intend to use the results in a
particular context, where some pragmas are already in scope. In
this case, you use the B<ambient_pragmas> method to describe the
assumptions you wish to make.
Not all of the options currently have any useful effect. See
L</BUGS> for more details.
The parameters it accepts are:
=over 4
=item strict
Takes a string, possibly containing several values separated
by whitespace. The special values "all" and "none" mean what you'd
expect.
$deparse->ambient_pragmas(strict => 'subs refs');
=item $[
Takes a number, the value of the array base $[.
Obsolete: cannot be non-zero.
=item bytes
=item utf8
=item integer
If the value is true, then the appropriate pragma is assumed to
be in the ambient scope, otherwise not.
=item re
Takes a string, possibly containing a whitespace-separated list of
values. The values "all" and "none" are special. It's also permissible
to pass an array reference here.
$deparser->ambient_pragmas(re => 'eval');
=item warnings
Takes a string, possibly containing a whitespace-separated list of
values. The values "all" and "none" are special, again. It's also
permissible to pass an array reference here.
$deparser->ambient_pragmas(warnings => [qw[void io]]);
If one of the values is the string "FATAL", then all the warnings
in that list will be considered fatal, just as with the B<warnings>
pragma itself. Should you need to specify that some warnings are
fatal, and others are merely enabled, you can pass the B<warnings>
parameter twice:
$deparser->ambient_pragmas(
warnings => 'all',
warnings => [FATAL => qw/void io/],
);
See L<warnings> for more information about lexical warnings.
=item hint_bits
=item warning_bits
These two parameters are used to specify the ambient pragmas in
the format used by the special variables $^H and ${^WARNING_BITS}.
They exist principally so that you can write code like:
{ my ($hint_bits, $warning_bits);
BEGIN {($hint_bits, $warning_bits) = ($^H, ${^WARNING_BITS})}
$deparser->ambient_pragmas (
hint_bits => $hint_bits,
warning_bits => $warning_bits,
'$[' => 0 + $[
); }
which specifies that the ambient pragmas are exactly those which
are in scope at the point of calling.
=item %^H
This parameter is used to specify the ambient pragmas which are
stored in the special hash %^H.
=back
=head2 coderef2text
$body = $deparse->coderef2text(\&func)
$body = $deparse->coderef2text(sub ($$) { ... })
Return source code for the body of a subroutine (a block, optionally
preceded by a prototype in parens), given a reference to the
sub. Because a subroutine can have no names, or more than one name,
this method doesn't return a complete subroutine definition -- if you
want to eval the result, you should prepend "sub subname ", or "sub "
for an anonymous function constructor. Unless the sub was defined in
the main:: package, the code will include a package declaration.
=head1 BUGS
=over 4
=item *
The only pragmas to
be completely supported are: C<use warnings>,
C<use strict>, C<use bytes>, C<use integer>
and C<use feature>.
Excepting those listed above, we're currently unable to guarantee that
B::Deparse will produce a pragma at the correct point in the program.
(Specifically, pragmas at the beginning of a block often appear right
before the start of the block instead.)
Since the effects of pragmas are often lexically scoped, this can mean
that the pragma holds sway over a different portion of the program
than in the input file.
=item *
In fact, the above is a specific instance of a more general problem:
we can't guarantee to produce BEGIN blocks or C<use> declarations in
exactly the right place. So if you use a module which affects compilation
(such as by over-riding keywords, overloading constants or whatever)
then the output code might not work as intended.
=item *
Some constants don't print correctly either with or without B<-d>.
For instance, neither B::Deparse nor Data::Dumper know how to print
dual-valued scalars correctly, as in:
use constant E2BIG => ($!=7); $y = E2BIG; print $y, 0+$y;
use constant H => { "#" => 1 }; H->{"#"};
=item *
An input file that uses source filtering probably won't be deparsed into
runnable code, because it will still include the B<use> declaration
for the source filtering module, even though the code that is
produced is already ordinary Perl which shouldn't be filtered again.
=item *
Optimized-away statements are rendered as
'???'. This includes statements that
have a compile-time side-effect, such as the obscure
my $x if 0;
which is not, consequently, deparsed correctly.
foreach my $i (@_) { 0 }
=>
foreach my $i (@_) { '???' }
=item *
Lexical (my) variables declared in scopes external to a subroutine
appear in coderef2text output text as package variables. This is a tricky
problem, as perl has no native facility for referring to a lexical variable
defined within a different scope, although L<PadWalker> is a good start.
See also L<Data::Dump::Streamer>, which combines B::Deparse and
L<PadWalker> to serialize closures properly.
=item *
There are probably many more bugs on non-ASCII platforms (EBCDIC).
=back
=head1 AUTHOR
Stephen McCamant <smcc@CSUA.Berkeley.EDU>, based on an earlier version
by Malcolm Beattie <mbeattie@sable.ox.ac.uk>, with contributions from
Gisle Aas, James Duncan, Albert Dvornik, Robin Houston, Dave Mitchell,
Hugo van der Sanden, Gurusamy Sarathy, Nick Ing-Simmons, and Rafael
Garcia-Suarez.
=cut
|