File: Transform.pm

package info (click to toggle)
libmarc-transform-perl 0.003007-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 388 kB
  • sloc: perl: 1,617; makefile: 2
file content (3488 lines) | stat: -rw-r--r-- 116,009 bytes parent folder | download
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
# vim: sw=4
package MARC::Transform;
use 5.10.0;
use warnings;
use strict;
use Carp;
use MARC::Record;
use YAML;
use Scalar::Util qw< reftype >;
our $VERSION = '0.003007';
our $DEBUG = 0;
sub debug { $DEBUG and say STDERR @_ }

my %fields;
my $globalcondition;
my $record;
our $mth;
my $globalsubs;
my $verbose=0;
my @errors;
my $global_LUT;
our $this="";

sub new {
    my ($self,$recordsource,$yaml,$mthsource,$verb) = @_;
    my @yaml;
    no warnings 'redefine';
    no warnings 'newline';
    my $yamltoload;
    if ( -e $yaml ) {
        open my $yamls, "< $yaml" or die "can't open file: $!";
        my $yamlline;
        while ($yamlline = <$yamls>){ $yamltoload.=$yamlline; }
        close $yamls;
        #@yaml = YAML::LoadFile($yamls);
    }
    else {
        $yamltoload=$yaml;
        #@yaml = YAML::Load($yaml);
    }
    $yamltoload=~s/#_dollars_#/\\#_dollars_\\#/g;
    $yamltoload=~s/#_dbquote_#/\\#_dbquote_\\#/g;
    @yaml = YAML::Load($yamltoload);
    #warn "================". Data::Dumper::Dumper (\@yaml)."------------------";
    $record=$recordsource;
    $mth=$mthsource;
    $$mth{"_defaultLUT_to_mth_"}={} if $mth;
    ReplaceAllInRecord("before");
    $verbose = 1 if ($verb);
    foreach my $rulesub(@yaml) {
        if ( ref($rulesub) eq "HASH" ) {
            if ( defnonull ( $$rulesub{'global_subs'} ) ) {
                $globalsubs = $$rulesub{'global_subs'};
                eval ($globalsubs);
            }
            if ( defnonull ( $$rulesub{'global_LUT'} ) ) {
                if (ref($$rulesub{'global_LUT'}) eq "HASH") {
                    $global_LUT=$$rulesub{'global_LUT'};
                }
            }
        }
    }
    foreach my $rule(@yaml) {
        #print Data::Dumper::Dumper ($rule);
        if ( ref($rule) eq "ARRAY" ) {
            my $subs="";
            foreach my $rul ( @$rule ) {
                if ( defnonull ( $$rul{'subs'} ) ) {
                    $subs.=$$rul{'subs'};
                }
                if ( defnonull ( $$rul{'LUT'} ) ) {
                    $$global_LUT{"lookuptableforthis"}=$$rul{'LUT'};#warn Data::Dumper::Dumper $global_LUT;
                }
            }
            foreach my $rul ( @$rule ) {
                my ($actionsin, $actionsinter, $actionsout)= parseactions($rul);#warn Data::Dumper::Dumper ($rul);
                my $boolcondition = testrule($rul, $actionsin, $actionsinter, $actionsout, $subs);
                #warn $boolcondition;warn "actionsin : ".$actionsin;warn "actionsout : ".$actionsout;
                if ($boolcondition) {
                    last;
                }
            }
        }
        elsif ( ref($rule) eq "HASH" ) {
            my $subs="";
            if ( defnonull ( $$rule{'subs'} ) ) {
                $subs.=$$rule{'subs'};
            }
            if ( defnonull ( $$rule{'LUT'} ) ) {
                $$global_LUT{"lookuptableforthis"}=$$rule{'LUT'};
            }
            my ($actionsin, $actionsinter, $actionsout)= parseactions($rule);
            my $boolcondition = testrule($rule, $actionsin, $actionsinter, $actionsout, $subs);
        }
        else {
            push(@errors, 'Invalid yaml : you try to use a scalar rule.'); #error
        }
    }
    foreach my $error (@errors) {
        print "\n$error";
    }
    ReplaceAllInRecord("after");
    $record;
}

sub defnonull { my $var = shift; if (defined $var and $var ne "") { return 1; } else { return 0; } }

sub LUT {
    my ( $inLUT, $type ) = @_;
    if (!defined($type)) {
        $type = "lookuptableforthis";
    }
    my $outLUT=$inLUT;
    my $boolnocor = 1;
    if ( ref($global_LUT) eq "HASH") {
        if (exists($$global_LUT{$type})) {
            my $correspondance=$$global_LUT{$type};
            if ( ref($correspondance) eq "HASH") {
                foreach my $cor (keys(%$correspondance)) {
                    if ($inLUT eq $cor) {
                        $outLUT=$$correspondance{$cor};
                        $boolnocor = 0;
                    }
                }
                if ($boolnocor) {
                    $outLUT=$$correspondance{"_default_value_"} if (defnonull($$correspondance{"_default_value_"}));
                    push (@{$$mth{"_defaultLUT_to_mth_"}->{"$type"}} , $inLUT) if $mth;
                }
            }
        }
    }
    return $outLUT;
}

sub update {
    my ($field,$subfields)=@_;
    transform ("update",$field,$subfields);
    return 1;
}
sub forceupdate {
    my ($field,$subfields)=@_;
    transform ("forceupdate",$field,$subfields);
    return 1;
}
sub updatefirst {
    my ($field,$subfields)=@_;
    transform ("updatefirst",$field,$subfields);
    return 1;
}
sub forceupdatefirst {
    my ($field,$subfields)=@_;
    transform ("forceupdatefirst",$field,$subfields);
    return 1;
}
sub create {
    my ($field,$subfields)=@_;
    transform ("create",$field,$subfields);
    return 1;
}

sub transform {
    my ($ttype,$field,$subfields)=@_;
    #print "\n------------$ttype------------ : \n".Data::Dumper::Dumper (@_);
    if ($ttype eq "forceupdate" or $ttype eq "forceupdatefirst" ) {
        if (ref($field) eq "" or ref($field) eq "SCALAR") {
            if (!defined $record->field($field) ) {$ttype="create"}
        }
    }
    if (ref($field) eq "MARC::Field") {
        #print "\n------------$ttype------------ : \n".Data::Dumper::Dumper ($subfields);
        foreach my $tag(keys(%$subfields)) {
            if ( $tag eq 'i1' or  $tag eq 'µ') {
                #print "\n------------$ttype------------ : \n";
                $this=$field->indicator(1);
                my $finalvalue=parsestringactions($$subfields{$tag});
                $field->update( ind1 => $finalvalue ) if ( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" );
            }
            elsif ( $tag eq 'i2' or  $tag eq '£') {
                $this=$field->indicator(2);
                my $finalvalue=parsestringactions($$subfields{$tag});
                $field->update( ind2 => $finalvalue ) if ( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" );
            }
            else {
                if( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" ) {
                    if($field->is_control_field()) {
                        $this=$field->data();
                        my $finalvalue=parsestringactions($$subfields{$tag});
                        $field->update($finalvalue);
                    }
                    else {
                        if ($ttype eq "create") {
                            $this="";
                            my $finalvalue=parsestringactions($$subfields{$tag});
                            $field->add_subfields( $tag => $finalvalue );
                        }
                        elsif ($ttype eq "updatefirst") {
                            if ( defined $field->subfield( $tag ) ) {
                                $this=$field->subfield( $tag );
                                my $finalvalue=parsestringactions($$subfields{$tag});
                                $field->update( $tag => $finalvalue );
                            }
                            #warn $tag.$$subfields{$tag};
                        }
                        elsif ($ttype eq "forceupdatefirst") {
                            if ( defined $field->subfield( $tag ) ) {
                                $this=$field->subfield( $tag );
                                my $finalvalue=parsestringactions($$subfields{$tag});
                                $field->update( $tag => $finalvalue );
                            }
                            else {
                                $this="";
                                my $finalvalue=parsestringactions($$subfields{$tag});
                                $field->add_subfields( $tag => $finalvalue );
                            }
                        }
                    }
                }
                elsif( ref($$subfields{$tag}) eq "ARRAY" ) {
                    if(!$field->is_control_field()) {
                        foreach my $subfield(@{$$subfields{$tag}}) {
                            if ($ttype eq "create") {
                                $this="";
                                my $finalvalue=parsestringactions($subfield);
                                $field->add_subfields( $tag => $finalvalue );
                            }
                            elsif ($ttype eq "updatefirst") {
                                if ( defined $field->subfield( $tag ) ) {
                                    $this=$field->subfield( $tag );
                                    my $finalvalue=parsestringactions($subfield);
                                    $field->update( $tag => $finalvalue );
                                }
                            }
                            elsif ($ttype eq "forceupdatefirst") {
                                if ( defined $field->subfield( $tag ) ) {
                                    $this=$field->subfield( $tag );
                                    my $finalvalue=parsestringactions($subfield);
                                    $field->update( $tag => $finalvalue );
                                }
                                else {
                                    $this="";
                                    my $finalvalue=parsestringactions($subfield);
                                    $field->add_subfields( $tag => $finalvalue );
                                }
                            }
                        }
                    }
                    else {
                        push(@errors, 'Invalid yaml : you try to use an array to '.$ttype.' in existing condition\'s controlfield value.'); #error
                    }
                }
            }
        }
        if((!$field->is_control_field()) and ($ttype eq "update" or $ttype eq "forceupdate" )) {
            my @usubfields;
            foreach my $subfield ( $field->subfields() ) {
                if ( exists($$subfields{$$subfield[0]}) ) {
                    #implementation de l'eval des fonctions et de $this
                    $this=$$subfield[1];
                    my $finalvalue=parsestringactions($$subfields{$$subfield[0]});
                    push @usubfields, ( $$subfield[0],$finalvalue );
                }
                else {
                    push @usubfields, ( $$subfield[0], $$subfield[1] );
                }
            }
            my $newfield = MARC::Field->new( $field->tag(), $field->indicator(1), $field->indicator(2), @usubfields );
            foreach my $tag(keys(%$subfields)) {
                if($tag ne 'i1' and $tag ne 'µ' and $tag ne 'i2' and $tag ne '£' and !defined($newfield->subfield( $tag )) ) {
                    if( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" ) {
                        $this="";
                        my $finalvalue=parsestringactions($$subfields{$tag});
                        $newfield->add_subfields( $tag => $finalvalue ) if $ttype eq "forceupdate";
                    }
                    else {
                        push(@errors, 'Invalid yaml : you try to use a non-scalar value to '.$ttype.' in existing condition\'s field value.'); #error
                    }
                }
            }
            $field->replace_with($newfield);
        }
    }
    elsif (ref($field) eq "" or ref($field) eq "SCALAR") {
        #print "\n------------$ttype------------ : \n".Data::Dumper::Dumper (@_);
        if ($ttype eq "update" or $ttype eq "updatefirst" or $ttype eq "forceupdate" or $ttype eq "forceupdatefirst") {
            if ( defined $record->field($field) ) {
                for my $updatefield ( $record->field($field) ) {
                    foreach my $tag(keys(%$subfields)) {
                        if ( $tag eq 'i1' or  $tag eq 'µ') {
                            $this=$updatefield->indicator(1);
                            my $finalvalue=parsestringactions($$subfields{$tag});
                            $updatefield->update( ind1 => $finalvalue ) if ( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" );
                        }
                        elsif ( $tag eq 'i2' or  $tag eq '£') {
                            $this=$updatefield->indicator(2);
                            my $finalvalue=parsestringactions($$subfields{$tag});
                            $updatefield->update( ind2 => $finalvalue ) if ( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" );
                        }
                        elsif( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" ) {
                            if($updatefield->is_control_field()) {
                                $this=$updatefield->data();
                                my $finalvalue=parsestringactions($$subfields{$tag});
                                $updatefield->update($finalvalue);
                            }
                            elsif ( $ttype eq "updatefirst" ) {
                                if ( defined $updatefield->subfield( $tag ) ) {
                                    $this=$updatefield->subfield( $tag );
                                    my $finalvalue=parsestringactions($$subfields{$tag});
                                    $updatefield->update( $tag => $finalvalue );
                                }
                            }
                            elsif ($ttype eq "forceupdatefirst") {
                                if ( defined $updatefield->subfield( $tag ) ) {
                                    $this=$updatefield->subfield( $tag );
                                    my $finalvalue=parsestringactions($$subfields{$tag});
                                    $updatefield->update( $tag => $finalvalue );
                                }
                                else {
                                    $this="";
                                    my $finalvalue=parsestringactions($$subfields{$tag});
                                    $updatefield->add_subfields( $tag => $finalvalue );
                                }
                            }
                        }
                        else {
                            push(@errors, 'Invalid yaml : you try to use a non-scalar value to '.$ttype.' field.');#error
                        }
                    }
                    if((!$updatefield->is_control_field()) and ($ttype eq "update" or $ttype eq "forceupdate" )) {
                        my @usubfields;
                        foreach my $subfield ( $updatefield->subfields() ) {
                            if ( exists($$subfields{$$subfield[0]}) ) {
                                $this=$$subfield[1];
                                my $finalvalue=parsestringactions($$subfields{$$subfield[0]});
                                push @usubfields, ( $$subfield[0],$finalvalue );
                            }
                            else {
                                push @usubfields, ( $$subfield[0], $$subfield[1] );
                            }
                        }
                        my $newfield = MARC::Field->new( $updatefield->tag(), $updatefield->indicator(1), $updatefield->indicator(2), @usubfields );
                        foreach my $tag(keys(%$subfields)) {
                            if($tag ne 'i1' and $tag ne 'µ' and $tag ne 'i2' and $tag ne '£' and !defined($newfield->subfield( $tag )) ) {
                                if( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" ) {
                                    $this="";
                                    my $finalvalue=parsestringactions($$subfields{$tag});
                                    $newfield->add_subfields( $tag => $finalvalue ) if $ttype eq "forceupdate";
                                }
                                else {
                                    push(@errors, 'Invalid yaml : you try to use a non-scalar value to '.$ttype.' field.');#error
                                }
                            }
                        }
                        $updatefield->replace_with($newfield);
                    }
                }
            }
        }
        elsif ($ttype eq "create") {
            my $newfield;
            $this="";
            if ($field < "010" ) {
                $newfield = MARC::Field->new( $field, 'temp');
            }
            else {
                $newfield = MARC::Field->new( $field, '', '', '0'=>'temp');
            }
            
            foreach my $tag(keys(%$subfields)) {
                if ( $tag eq 'i1' or  $tag eq 'µ') {
                    my $finalvalue=parsestringactions($$subfields{$tag});
                    $newfield->update( ind1 => $finalvalue ) if ( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" );
                }
                elsif ( $tag eq 'i2' or  $tag eq '£') {
                    my $finalvalue=parsestringactions($$subfields{$tag});
                    $newfield->update( ind2 => $finalvalue ) if ( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" );
                }
                else {
                    if( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" ) {
                        if($newfield->is_control_field()) {
                            my $finalvalue=parsestringactions($$subfields{$tag});
                            $newfield->update($finalvalue);
                        }
                        else {
                            my $finalvalue=parsestringactions($$subfields{$tag});
                            $newfield->add_subfields( $tag => $finalvalue );
                        }
                    }
                    elsif( ref($$subfields{$tag}) eq "ARRAY" ) {
                        if(!$newfield->is_control_field()) {
                            foreach my $subfield(@{$$subfields{$tag}}) {
                                my $finalvalue=parsestringactions($subfield);
                                $newfield->add_subfields( $tag => $finalvalue );
                            }
                        }
                    }
                }
            }
            if (!$newfield->is_control_field()) {
                $newfield->delete_subfield(pos => '0');
            }
            $record->insert_fields_ordered($newfield);
        }
    }
    else {
        push(@errors, 'Invalid yaml : you try to use an array or hash value to '.$ttype.' field.');#error
    }
    return 1;
}

sub parsestringactions {
    my $subfieldtemp=shift;
    $subfieldtemp=~s/tempnameforcurrentvalueofthissubfield/\$this/g;
    $subfieldtemp=~s/temporarycallfunction/\\&/g;
    my $finalvalue;
    if ($subfieldtemp=~/\\&/) {
        $subfieldtemp=~s/\\&/&/g;
        $finalvalue = eval ($subfieldtemp);
    }
    else {
        $finalvalue = eval '"'.$subfieldtemp.'"';
    }
    return $finalvalue;
}

sub parseactions {
    my $rul = shift;
    my $actionsintemp="";
    my $actionsin="";
    my $actionsinter="";
    my $actionsouttemp="";
    my $actionsout="";
    #print "\n".Data::Dumper::Dumper $rul;
    #create duplicatefield forceupdate forceupdatefirst update updatefirst execute delete 
    if ( defnonull ( $$rul{'create'} ) ) {
        ($actionsintemp,$actionsouttemp)=parsesubaction ($$rul{'create'},'create');
        $actionsin.=$actionsintemp;    $actionsout.=$actionsouttemp;
    }
    if ( defnonull ( $$rul{'duplicatefield'} ) ) {
        ($actionsintemp,$actionsouttemp)=parsesubaction ($$rul{'duplicatefield'},'duplicatefield');
        $actionsinter.=$actionsintemp;    $actionsout.=$actionsouttemp;
    }
    if ( defnonull ( $$rul{'forceupdate'} ) ) {
        ($actionsintemp,$actionsouttemp)=parsesubaction ($$rul{'forceupdate'},'forceupdate');
        $actionsin.=$actionsintemp;    $actionsout.=$actionsouttemp;
    }
    if ( defnonull ( $$rul{'forceupdatefirst'} ) ) {
        ($actionsintemp,$actionsouttemp)=parsesubaction ($$rul{'forceupdatefirst'},'forceupdatefirst');
        $actionsin.=$actionsintemp;    $actionsout.=$actionsouttemp;
    }
    if ( defnonull ( $$rul{'update'} ) ) {
        ($actionsintemp,$actionsouttemp)=parsesubaction ($$rul{'update'},'update');
        $actionsin.=$actionsintemp;    $actionsout.=$actionsouttemp;
    }
    if ( defnonull ( $$rul{'updatefirst'} ) ) {
        ($actionsintemp,$actionsouttemp)=parsesubaction ($$rul{'updatefirst'},'updatefirst');
        $actionsin.=$actionsintemp;    $actionsout.=$actionsouttemp;
    }
    if ( defnonull ( $$rul{'execute'} ) ) {
        ($actionsintemp,$actionsouttemp)=parsesubaction ($$rul{'execute'},'execute');
        $actionsin.=$actionsintemp;    $actionsout.=$actionsouttemp;
    }
    if ( defnonull ( $$rul{'delete'} ) ) {
        ($actionsintemp,$actionsouttemp)=parsesubaction ($$rul{'delete'},'delete');
        $actionsin.=$actionsintemp;    $actionsout.=$actionsouttemp;
    }
    #print "\n----------------------actionsin---------------------- : \n$actionsin\n\n----------------------actionsout---------------------- : \n$actionsout\n----------------------actionsend----------------------";
    return ($actionsin, $actionsinter, $actionsout)
}

sub parsesubaction {
    my ($intaction,$type)=@_;
    my $actionsin="";
    my $actionsout="";
    my $boolin=0;
    my $specaction="";
    my $currentaction="";#warn ref($intaction);
    $specaction=" $type";
    #print "\n".Data::Dumper::Dumper $intaction;
    if ($type eq "create" or $type eq "forceupdate" or $type eq "update" or $type eq "forceupdatefirst" or $type eq "updatefirst") {
        if ( ref($intaction) eq "HASH" ) {
            foreach my $kint (keys(%$intaction)) {
                if( ref($$intaction{$kint}) eq "HASH" ) {
                    my $ftag;
                    $currentaction="";
                    $boolin=0;
                    if($kint=~/^\$f(\d{3})$/) {
                        $boolin=1;
                        $ftag=$kint;
                    }
                    elsif($kint=~/^f(\d{3})$/) {
                        $ftag='"'.$1.'"';
                    }
                    else {
                        push(@errors, 'Invalid yaml : your field reference is not valid in '.$type.' action.');#error
                        next;
                    }
                    $currentaction.=$specaction.'('.$ftag.',{';
                    my $subint=$$intaction{$kint};
                    foreach my $k (keys(%$subint)) {
                        if( ref($$subint{$k}) eq "" or ref($$subint{$k}) eq "SCALAR" ) {
                            $$subint{$k}=~s/"/\\"/g;
                            $boolin=1 if($$subint{$k}=~/\$f/);#print $k." eq. ".$$subint{$k}."\n";
                            $$subint{$k}=~s/\$this/tempnameforcurrentvalueofthissubfield/g;
                            $$subint{$k}=~s/\\&/temporarycallfunction/g;
                            $currentaction.='"'.$k.'"=> "'.$$subint{$k}.'",';
                        }
                        elsif( ref($$subint{$k}) eq "ARRAY" ) {
                            $currentaction.='"'.$k.'"=>[';
                            foreach my $ssubint(@{$$subint{$k}}) {
                                $ssubint=~s/"/\\"/g;
                                $boolin=1 if($ssubint=~/\$f/);
                                $ssubint=~s/\$this/tempnameforcurrentvalueofthissubfield/g;
                                $ssubint=~s/\\&/temporarycallfunction/g;
                                $currentaction.='"'.$ssubint.'",';
                            }
                            $currentaction.='],';
                        }
                        else {
                            push(@errors, 'Invalid yaml : you try to use a hash inside another hash in '.$type.' action.');#error
                        }
                    }
                    $currentaction.='});'."\n";
                    if ($boolin) { $actionsin.=$currentaction; } else { $actionsout.=$currentaction; }
                }
                elsif( ref($$intaction{$kint}) eq "" or ref($$intaction{$kint}) eq "SCALAR" ) {
                    $currentaction="";
                    $boolin=0;
                    my $ftag;
                    my $stag;
                    if($kint=~/^\$f(\d{3})(\w)$/) {
                        $boolin=1;
                        $ftag='$f'.$1;
                        $stag=$2;
                    }
                    elsif($kint=~/^\$i(\d{3})(\w)$/) {
                        $boolin=1;
                        $ftag='$f'.$1;
                        $stag='µ';
                        $stag='µ' if($2 eq "1");
                        $stag='£' if($2 eq "2");
                    }
                    elsif($kint=~/^f(\d{3})(\w)$/) {
                        $ftag='"'.$1.'"';
                        $stag=$2;
                    }
                    elsif($kint=~/^i(\d{3})(\w)$/) {
                        $ftag='"'.$1.'"';
                        $stag='µ';
                        $stag='µ' if($2 eq "1");
                        $stag='£' if($2 eq "2");
                    }
                    elsif($kint=~/^i(\d)$/) {
                        $ftag='$$currentfield';
                        $stag='µ';
                        $stag='µ' if($1 eq "1");
                        $stag='£' if($1 eq "2");
                        $boolin=1;
                    }
                    elsif($kint=~/^(\w)$/) {
                        $ftag='$$currentfield';
                        $boolin=1;
                        $stag=$kint;
                    }
                    else {
                        push(@errors, 'Invalid yaml : your field reference is not valid in '.$type.' action.');#error
                        next;
                    }
                    $$intaction{$kint}=~s/"/\\"/g;
                    $boolin=1 if($$intaction{$kint}=~/\$f/);
                    $$intaction{$kint}=~s/\$this/tempnameforcurrentvalueofthissubfield/g;
                    $$intaction{$kint}=~s/\\&/temporarycallfunction/g;
                    $currentaction.=$specaction.'('.$ftag.',{"'.$stag.'"=>"'.$$intaction{$kint}.'"});'."\n";
                    if ($boolin) { $actionsin.=$currentaction; } else { $actionsout.=$currentaction; }
                }
                elsif( ref($$intaction{$kint}) eq "ARRAY" ) {
                    $currentaction="";
                    $boolin=0;
                    my $ftag;
                    my $stag;
                    if($kint=~/^\$f(\d{3})(\w)$/) {
                        $boolin=1;
                        $ftag='$f'.$1;
                        $stag=$2;
                    }
                    elsif($kint=~/^\$i(\d{3})(\w)$/) {
                        $boolin=1;
                        $ftag='$f'.$1;
                        $stag='µ';
                        $stag='µ' if($2 eq "1");
                        $stag='£' if($2 eq "2");
                    }
                    elsif($kint=~/^f(\d{3})(\w)$/) {
                        $ftag='"'.$1.'"';
                        $stag=$2;
                    }
                    elsif($kint=~/^i(\d{3})(\w)$/) {
                        $ftag='"'.$1.'"';
                        $stag='µ';
                        $stag='µ' if($2 eq "1");
                        $stag='£' if($2 eq "2");
                    }
                    elsif($kint=~/^(\w)$/) {
                        $ftag='$$currentfield';
                        $boolin=1;
                        $stag=$kint;
                    }
                    else {
                        push(@errors, 'Invalid yaml : your field reference is not valid in '.$type.' action.');#error
                        next;
                    }
                    $currentaction.=$specaction.'('.$ftag.',{"'.$stag.'"=>[';
                    foreach my $sintaction(@{$$intaction{$kint}}) {
                        $sintaction=~s/"/\\"/g;
                        $boolin=1 if($sintaction=~/\$f/);
                        $sintaction=~s/\$this/tempnameforcurrentvalueofthissubfield/g;
                        $sintaction=~s/\\&/temporarycallfunction/g;
                        $currentaction.='"'.$sintaction.'",';
                    }
                    $currentaction.=']});'."\n";
                    if ($boolin) { $actionsin.=$currentaction; } else { $actionsout.=$currentaction; }
                }
            }
        }
        else {
            push(@errors, 'Invalid yaml : you try to use non hash context in '.$type.' action.');#error
        }
    }
    elsif ($type eq "duplicatefield") {
        if ( ref($intaction) eq "ARRAY" ) {
            foreach my $vint (@$intaction) {
                if( ref($vint) eq "" or ref($vint) eq "SCALAR" ) {
                    if($vint=~/^\$f(\d{3})\s?>\s?f(\d{3})$/) {
                        if ($1 < "010" and $2 < "010" ) {
                            $actionsin.='$record->insert_fields_ordered( MARC::Field->new( "'.$2.'", $f'.$1.'->data() ) );';
                        }
                        elsif ($1 >= "010" and $2 >= "010" ) {
                            $actionsin.='my @dsubfields; foreach my $subfield ( $f'.$1.'->subfields() ) { push @dsubfields, ( $$subfield[0], $$subfield[1] );}'."\n";
                            $actionsin.='$record->insert_fields_ordered( MARC::Field->new( "'.$2.'", $f'.$1.'->indicator(1), $f'.$1.'->indicator(2), @dsubfields ) );';
                        }
                        else {
                            push(@errors, 'Invalid yaml : you want to duplicate a controlfield with a non-controlfield ');#error
                        }
                    }
                    elsif($vint=~/^f(\d{3})\s?>\s?f(\d{3})$/) {
                        if ($1 < "010" and $2 < "010" ) {
                            $actionsout.=' for my $fielddup($record->field("'.$1.'")){';
                            $actionsout.='$record->insert_fields_ordered( MARC::Field->new( "'.$2.'", $fielddup->data() ) );';
                            $actionsout.='}'."\n";
                        }
                        elsif ($1 >= "010" and $2 >= "010" ) {
                            $actionsout.=' for my $fielddup($record->field("'.$1.'")){';
                            $actionsout.='my @dsubfields; foreach my $subfield ( $fielddup->subfields() ) { push @dsubfields, ( $$subfield[0], $$subfield[1] );}'."\n";
                            $actionsout.='$record->insert_fields_ordered( MARC::Field->new( "'.$2.'", $fielddup->indicator(1), $fielddup->indicator(2), @dsubfields ) );';
                            $actionsout.='}'."\n";
                        }
                        else {
                            push(@errors, 'Invalid yaml : you want to duplicate a controlfield with a non-controlfield ');#error
                        }
                    }
                    else {
                        push(@errors, 'Invalid yaml : your field reference is not valid in '.$type.' action.');#error
                    }
                }
                else {
                    push(@errors, 'Invalid yaml : you try to use non scalar value in '.$type.' action.');#error
                }
            }
        }
        elsif ( ref($intaction) eq "" or ref($intaction) eq "SCALAR" ) {
            my $vint=$intaction;
            if($vint=~/^\$f(\d{3})\s?>\s?f(\d{3})$/) {
                if ($1 < "010" and $2 < "010" ) {
                    $actionsin.='$record->insert_fields_ordered( MARC::Field->new( "'.$2.'", $f'.$1.'->data() ) );';
                }
                elsif ($1 >= "010" and $2 >= "010" ) {
                    $actionsin.='my @dsubfields; foreach my $subfield ( $f'.$1.'->subfields() ) { push @dsubfields, ( $$subfield[0], $$subfield[1] );}'."\n";
                    $actionsin.='$record->insert_fields_ordered( MARC::Field->new( "'.$2.'", $f'.$1.'->indicator(1), $f'.$1.'->indicator(2), @dsubfields ) );';
                }
                else {
                    push(@errors, 'Invalid yaml : you want to duplicate a controlfield with a non-controlfield ');#error
                }
            }
            elsif($vint=~/^f(\d{3})\s?>\s?f(\d{3})$/) {
                if ($1 < "010" and $2 < "010" ) {
                    $actionsout.=' for my $fielddup($record->field("'.$1.'")){';
                    $actionsout.='$record->insert_fields_ordered( MARC::Field->new( "'.$2.'", $fielddup->data() ) );';
                    $actionsout.='}'."\n";
                }
                elsif ($1 >= "010" and $2 >= "010" ) {
                    $actionsout.=' for my $fielddup($record->field("'.$1.'")){';
                    $actionsout.='my @dsubfields; foreach my $subfield ( $fielddup->subfields() ) { push @dsubfields, ( $$subfield[0], $$subfield[1] );}'."\n";
                    $actionsout.='$record->insert_fields_ordered( MARC::Field->new( "'.$2.'", $fielddup->indicator(1), $fielddup->indicator(2), @dsubfields ) );';
                    $actionsout.='}'."\n";
                }
                else {
                    push(@errors, 'Invalid yaml : you want to duplicate a controlfield with a non-controlfield ');#error
                }
            }
            else {
                push(@errors, 'Invalid yaml : your field reference is not valid in '.$type.' action.');#error
            }
        }
        else {
            push(@errors, 'Invalid yaml : you try to use a hash value in '.$type.' action.');#error
        }
    }
    elsif ($type eq "delete") {
        if ( ref($intaction) eq "ARRAY" ) {
            foreach my $vint (@$intaction) {
                if( ref($vint) eq "" or ref($vint) eq "SCALAR" ) {
                    #print "$vint\n";
                    if($vint=~/^\$f(\d{3})(\w)$/) {
                        $actionsin.=' if ( defined $f'.$1.'->subfield("'.$2.'") ) { if (scalar($f'.$1.'->subfields())==1) { $record->delete_field($f'.$1.'); } else { $f'.$1.'->delete_subfield(code => "'.$2.'"); } }'."\n";
                    }
                    elsif($vint=~/^\$f(\d{3})$/) {
                        $actionsin.=' $record->delete_field('.$vint.');'."\n";
                    }
                    elsif($vint=~/^f(\d{3})(\w)$/) {
                        $actionsout.=' for my $fieldel($record->field("'.$1.'")){if ( defined $fieldel->subfield("'.$2.'") ) { if (scalar($fieldel->subfields())==1) { $record->delete_field($fieldel); } else { $fieldel->delete_subfield(code => "'.$2.'"); } }}'."\n";
                    }
                    elsif($vint=~/^f(\d{3})$/) {
                        $actionsout.=' $record->delete_fields($record->field("'.$1.'"));'."\n";
                    }
                    elsif($vint=~/^(\w)$/) {
                        $actionsin.=' if ( defined $$currentfield->subfield("'.$vint.'") ) { if (scalar($$currentfield->subfields())==1) { $record->delete_field($$currentfield); } else { $$currentfield->delete_subfield(code => "'.$vint.'"); } }'."\n";
                    }
                    else {
                        push(@errors, 'Invalid yaml : your field reference is not valid in '.$type.' action.');#error
                    }
                }
                else {
                    push(@errors, 'Invalid yaml : you try to use non scalar value in '.$type.' action.');#error
                }
            }
        }
        elsif ( ref($intaction) eq "" or ref($intaction) eq "SCALAR" ) {
            my $vint=$intaction;
            if($vint=~/^\$f(\d{3})(\w)$/) {
                $actionsin.=' if ( defined $f'.$1.'->subfield("'.$2.'") ) { if (scalar($f'.$1.'->subfields())==1) { $record->delete_field($f'.$1.'); } else { $f'.$1.'->delete_subfield(code => "'.$2.'"); } }'."\n";
            }
            elsif($vint=~/^\$f(\d{3})$/) {
                $actionsin.=' $record->delete_field('.$vint.');'."\n";
            }
            elsif($vint=~/^f(\d{3})(\w)$/) {
                $actionsout.=' for my $fieldel($record->field("'.$1.'")){if ( defined $fieldel->subfield("'.$2.'") ) { if (scalar($fieldel->subfields())==1) { $record->delete_field($fieldel); } else { $fieldel->delete_subfield(code => "'.$2.'"); } }}'."\n";
            }
            elsif($vint=~/^f(\d{3})$/) {
                $actionsout.=' $record->delete_fields($record->field("'.$1.'"));'."\n";
            }
            elsif($vint=~/^(\w)$/) {
                $actionsin.=' if ( defined $$currentfield->subfield("'.$vint.'") ) { if (scalar($$currentfield->subfields())==1) { $record->delete_field($$currentfield); } else { $$currentfield->delete_subfield(code => "'.$vint.'"); } }'."\n";
            }
            else {
                push(@errors, 'Invalid yaml : your field reference is not valid in '.$type.' action.');#error
            }
        }
        else {
            push(@errors, 'Invalid yaml : you try to use a hash value in '.$type.' action.');#error
        }
    }
    elsif ($type eq "execute") {
        if( ref($intaction) eq "" or ref($intaction) eq "SCALAR" ) {
            if($intaction=~/\$(f|i)(\d{3})/) {
                $actionsin.=' eval ('.$intaction.');';
            }
            else {
                $actionsout.=' eval ('.$intaction.');';
            }
        }
        elsif( ref($intaction) eq "ARRAY" ) {
            foreach my $sintaction(@$intaction) {
                if($sintaction=~/\$(f|i)(\d{3})/) {
                    $actionsin.=' eval ('.$sintaction.');';
                }
                else {
                    $actionsout.=' eval ('.$sintaction.');';
                }
            }
        }
        else {
            push(@errors, 'Invalid yaml : you try to use a hash value in '.$type.' action.');#error
        }
    }
    else {
        push(@errors, 'Invalid yaml : this action : '.$type.' is not valid.');#error
    }
    return ($actionsin,$actionsout);
}

sub testrule {
    my ($rul, $actionsin, $actionsinter, $actionsout, $subs) = @_;
    $globalcondition="";
    $subs="no warnings 'redefine';".$subs if $subs ne "";
    my $globalconditionstart='{ '."\n".$subs."\n".'my $boolcond=0;my $boolcondint=0;my $currentfield;no warnings \'substr\';no warnings \'uninitialized\';'."\n";
    my $globalconditionint="";
    my $globalconditionend="";#print Data::Dumper::Dumper ($rul);
    if ( defnonull ( $$rul{'condition'} ) ) {
        my @listconditiontags=grep( $_ , map({ $_=~/^(f|i)(\d{3})(.*)$/;$2 } (split(/\$/,$$rul{'condition'}))));#print $$rul{'condition'};
        my @listconditionsubtags=grep( $_ , map({ if($_=~/^(f)(\d{3}\w)(.*)$/){$2}elsif($_=~/^(i)(\d{3})(.*)$/){$2} } (split(/\$/,$$rul{'condition'}))));
        my %tag_names = map( { $_ => 1 } @listconditiontags);
        my %tag_list;
        @listconditiontags = keys(%tag_names);
        foreach my $tag(@listconditiontags) {
            $tag_list{$tag}=[];
            foreach my $subtag(@listconditionsubtags) {
                if (substr($subtag,0,3) eq $tag) {
                    if(length($subtag) == 3) {
                        push (@{$tag_list{$tag}}, "tempvalueforcurrentfield");
                    }
                    else {
                        push (@{$tag_list{$tag}}, substr($subtag,3,1));
                    }
                }
            }
        }
        my $condition=$$rul{'condition'};
        $condition=~s/(\$ldr(\d{1,2}))/\(substr\(\$record->leader\(\),$2,1)\)/g;
        $condition=~s/(\$ldr)/(\$record->leader\(\)\)/g;
        $condition=~s/(\$f\d{3})(\w)/defined($1$2) and $1$2/g;
        $condition=~s/(\$f\d{3})(\w)(\d{1,2})/\(substr($1$2,$3,1\)\)/g;
        $condition=~s/(\$f\d{3})(\w)/$1$2/g;#I can't remember why I did this
        $condition=~s/(\$i(\d{3}))(\d)/\(\$f$2->indicator\($3\)\)/g;
        my $booltagrule=0;
        my $boolsubtagrule=0;
        foreach my $tag (sort {$a cmp $b} keys(%tag_list)) {
            my %tag_listtag = map { $_, 1 } @{$tag_list{$tag}};
            $boolsubtagrule=0;
            my @tag_listtagunique = keys %tag_listtag;
            $globalconditionstart.='my $f'.$tag.';';
            foreach my $subtag (@tag_listtagunique) {
                my $matchdelaration='my \$f'.$tag.$subtag.';';
                $globalconditionstart.='my $f'.$tag.$subtag.';' if $globalconditionstart!~$matchdelaration;
            }
            if ( defined $record->field($tag) ) {
                $booltagrule=1;
                $globalconditionint.="\n".'for $f'.$tag.' ( $record->field("'.$tag.'") ) {'."\n".'$currentfield=\$f'.$tag.';'."\n";
                foreach my $subtag (@tag_listtagunique) {
                    $boolsubtagrule=1;
                    if ($subtag ne "tempvalueforcurrentfield" and $tag > "010") {
                        $globalconditionint.='for $f'.$tag.$subtag.' ( $f'.$tag.'->subfield("'.$subtag.'"), my $tmpintesta=1 ) { my $tmpintestb=0; if ($tmpintesta==1) { $tmpintesta=undef;$tmpintestb=1; }'."\n";
                        $globalconditionint.='if ('.$condition.') {$boolcond=1;$boolcondint=1; eval{'.$actionsin.'}}else{$boolcondint=0 unless (!defined($tmpintesta) and $tmpintestb==0 );}'."\n";
                        $globalconditionend.='}'."\n";
                    }
                    elsif ($subtag ne "tempvalueforcurrentfield") {
                        $globalconditionint.='$f'.$tag.$subtag.' = $f'.$tag.'->data(); '."\n";
                        $globalconditionint.='if ('.$condition.') {$boolcond=1;$boolcondint=1; eval{'.$actionsin.'}}else{$boolcondint=0;}'."\n";
                    }
                }
                $globalconditionint.='if ('.$condition.')'."\n".'{$boolcond=1;$boolcondint=1; eval{'.$actionsin.'}}else{$boolcondint=0;}' unless $boolsubtagrule;
                $globalconditionend.='if ($boolcondint){ eval{'.$actionsinter.'};}}'."\n";
            }#else { $globalconditionint.='if ('.$condition.') {$boolcond=1;$boolcondint=1; eval{'.$actionsin.'}}else{$boolcondint=0;}'."\n"; }
        }
        $globalconditionint.='if ('.$condition.')'."\n".'{$boolcond=1;$boolcondint=1; eval{'.$actionsin.'}}else{$boolcondint=0;}' unless $booltagrule;
        $globalconditionend.="\n".' if ($boolcond){eval{'.$actionsout.'}}'."\n".' return $boolcond;}';#if ($boolcond or ('.$condition.'))
        $globalcondition=$globalconditionstart.$globalconditionint.$globalconditionend;
        print "\n--------globalcondition----------\n$globalcondition\n---------globalcondition---------\n" if $verbose;
        return eval($globalcondition);
    }
    else {
        print "\n--------actionsout----------\n$globalconditionstart$actionsout}\n---------actionsout---------\n" if $verbose;
        eval($globalconditionstart.$actionsout.'}');
        return 1;
    }
    return 1;
}

sub ReplaceAllInRecord {
    my ($pos) = @_;
    return unless ( $record && $record->fields() );
    foreach my $field ( $record->fields() ) {
        my @subfields;
        if(!$field->is_control_field()) {
            if (scalar($field->subfields()) > 0) {
                foreach my $subfield ( $field->subfields() ) {
                    my $newval=$$subfield[1];
                    if ($pos eq "before") {
                        #$newval=~s/\$/#_dollarsd_#/g;#to force warn
                        $newval=~s/\$/#_dollars_#/g;
                        $newval=~s/"/#_dbquote_#/g;
                    }
                    elsif ($pos eq "after") {
                        $newval=~s/#_dollars_#/\$/g;
                        $newval=~s/#_dbquote_#/"/g;
                    }
                    push @subfields, ( $$subfield[0], $newval );
                }
                my $newfield = MARC::Field->new( $field->tag(), $field->indicator(1), $field->indicator(2), @subfields );
                $field->replace_with($newfield);
            }
        }
        else {
            my $newval=$field->data();
            if ($pos eq "before") {
                $newval=~s/\$/#_dollars_#/g;
                $newval=~s/"/#_dbquote_#/g;
            }
            elsif ($pos eq "after") {
                $newval=~s/#_dollars_#/\$/g;
                $newval=~s/#_dbquote_#/"/g;
            }
            $field->update($newval);
        }
    }
}

1;
__END__

=head1 NAME

=encoding utf8

MARC::Transform - Perl module to transform a MARC record using a YAML configuration file

=head1 VERSION

Version 0.003007

=head1 SYNOPSIS

B<Perl script:>

    use MARC::Transform;

    # For this synopsis, we create a small record:
    my $record = MARC::Record->new();
    $record->insert_fields_ordered( MARC::Field->new( 
                                    '501', '', '', 
                                    'a' => 'foo', 
                                    'b' => '1', 
                                    'c' => 'bar', 
                                    'd' => 'bor' ) );

    print "--init record--\n". $record->as_formatted ."\n";

    # We transform our record with our YAML configuration file
    # with its absolute path (or relative if called 
    # from the right path ) :
    $record = MARC::Transform->new ( $record, "/path/conf.yaml" );

    # You can also define your YAML into a variable:
    my $yaml="delete : f501d\n";
    # and use it to transform the record:
    $record = MARC::Transform->new ( $record, $yaml );

    print "\n--transformed record--\n". $record->as_formatted ."\n";

B<conf.yaml:>

    ---
    condition : $f501a eq "foo"
    create :
     f502a : New 502a subfield's value
    update :
      $f501b : \&LUT("$this")
    LUT :
     1 : first
     2 : second value in this LUT (LookUp Table)
    ---
    delete : f501c

B<Result> (with C<< $record->as_formatted >>):

    --init record--
    LDR                         
    501    _afoo
           _b1
           _cbar
           _dbor
    
    --transformed record--
    LDR                         
    501    _afoo
           _bfirst
    502    _aNew 502a subfield's value

=head1 DESCRIPTION

This is a Perl module to transform a MARC record using a YAML configuration file.

It allows you to B<create> , B<update> , B<delete> , B<duplicate> fields and subfields of a record. You can also use B<scripts> and B<lookup tables>. You can specify B<conditions> to execute these actions.

All conditions, actions, functions and lookup tables are B<defined in the YAML>.

MARC::Transform use MARC::Record.

=head1 METHOD

=head2 new()

    $record = MARC::Transform->new($record, "/path/conf.yaml" );

This is the only method you'll use. It takes a MARC::Record object and a YAML path as arguments. You can also define your YAML into a variable and use it to transform the record like this : 

    my $yaml="delete : f501d\n";
    $record = MARC::Transform->new ( $record, $yaml );

=head3 Optional hash reference

As we will see in more detail below, it is possible to add a hash reference (named $mth into yaml) as the third optional argument.

    my $record = MARC::Record->new(); my $hashref = {'var' => 'foo'};
    my $yaml = 'create :
     f500a : $$mth{"var"}
    ';
    $record = MARC::Transform->new($record,$yaml,$hashref);
    #the new 500$a subfield's value is "foo"

=head3 Verbose mode

Each YAML rule (see basis below to understand what is a rule) generates a script that is evaluated, in the record, for each field and subfield specified in the condition (If there is a condition). By adding a fourth optional argument B<1> to the method, it displays the generated script. This can be useful to understand what is happening:

    $record = MARC::Transform->new($record,"/path/conf.yaml",0,1);

=head1 YAML

=head2 Basis

- B<YAML is divided in rules> (separated by --- ), each rule is executed one after the other, rules without condition will always be executed:

    ---
    condition : $f501a eq "foo"
    create :
     f600a : new field value
    ---
    delete : f501c
    ---

- B<conditions are written in perl>, which allows great flexibility. They must be defined with C<condition : >

    condition : ($f501a=~/foo/ and $f503a=~/bar/) or ($f102a eq "bib")
    # if a 501$a and 503$a contain foo and bar, or if a 102$a = bib

- Conditions test records B<field by field> (only for fields defined in the condition)

For example, this means, that if we have more '501' fields in the record, if our condition is C<$f501a eq "foo" and $f501b eq "bar">, that condition will be true only if a '501' field has a 'a' subfield = "foo" AND a 'b' subfield = 'bar' (it will be false if there is a '501' field with a 'a' subfield = "foo" and ANOTHER '501' field with a 'b' subfield = "bar").

- It's possible to run more than one different actions in a single rule:

    ---
    condition : $f501a eq "foo"
    create :
     f600a : new field value
    delete : f501c
    ---
    
- The order in which actions are written does not matter. Actions will always be executed in the following order: 

=over 4

=item * create

=item * duplicatefield

=item * forceupdate

=item * forceupdatefirst

=item * update

=item * updatefirst

=item * execute

=item * delete

=back

- B<Each rule can be divided into sub-rules> (separated by - ) similar to 'if,elsif' or 'switch,case' scripts. If the first sub-rule's condition is false, the following sub-rule's condition is tested. When the sub-rule's condition is true (or if a sub-rule has no condition), the following sub-rules are not read.

    ---
    -
     condition : $f501a eq "foo"
     create :
      f502a : value if foo
    -
     condition : $f501a eq "bar"
     create :
      f502a : value elsif bar
    -
     create :
      f502a : value else
    ---
    # It is obvious that if a sub-rule has no condition, it will be
    # considered as an 'else' (following sub-rules will not be read)

- It is not allowed to define more than one similar action into a single (sub-)rule. However, it remains possible to execute a similar action several times in a single rule (refer to the specific syntax of each action in order to see how to do this):

.   this is B<not> allowed:

    ---
    delete : f501b
    delete : f501c

.   it works:

    ---
    delete :
     - f501b
     - f501c

=head3 a small script to test your rules

- B<it is strongly recommended to test each rule on a test record before using it on a large batch of records>. You can create a script (e.g. C<< test.pl >>) with the contents below (that you will adapt to test your rules) and run it with C<< perl ./test.pl >> :

    #!/usr/bin/perl
    use MARC::Transform;
    my $record = MARC::Record->new();
    $record->leader('optional leader');
    $record->insert_fields_ordered( MARC::Field->new('005', 'controlfield_content'));
    $record->insert_fields_ordered( MARC::Field->new('501', '', '', 'a' => 'foo', 'b' => 'bar') );
    print "\n--init record--\n". $record->as_formatted ."\n";
    my $yaml='---
    condition : $f501a eq "foo"
    create :
     f502a : condition is true
    ';
    $record = MARC::Transform->new($record,$yaml);
    print "\n--transformed record--\n". $record->as_formatted ."\n";

=head2 Field's and subfield's naming convention

=head3 In actions

- Field's and subfield's names are very important: 

=over 4

=item * They must begin with the letter B<f> followed by the B<3-digit> field name (e.g. f099), followed, for the subfields, by their B<letter or digit> (e.g. B<f501b>).

=item * Controlfields names begin with the letter B<f> followed by B<3-digit lower than 010> followed by B<underscore> (e.g. B<f005_>). 

=item * B<Indicators> must begin with the letter B<i>, followed by the B<3-digit> field name followed by the indicator's position (B<1 or 2>) (par exemple B<i0991>).

=item * In actions, you can define B<a subfield directly> (or an indicator with i1 or i2). Depending on context, it refers to the condition's field (if we define only one field to be tested in the condition), or to the field currently being processed in action:

    ---
    condition : $f501a eq "foo"
    create :
     b : new 'b' subfield's value in unique condition's field (501)
     f600 :
      i1 : 1
      a : new subfield (a) in this new 600 field
    ---

=back

=head3 In conditions

=over 4

=item * In conditions, Field's and subfield's naming convention follow the B<same rules that actions>, but they must be B<preceded by a dollar signs $> (e.g. C<$f110c> for a subfield or C<$i0991> for an indicator).

=item * The record leader can be defined with B<$ldr>.

=item * It's possible to test only one character's value in subfields or leader. To do this, you have to add the B<this character's position from 0 to 99>:

    #to test the 3rd char. in leader and the 12e char. in '501$a':
    condition : $ldr2 eq "t" and $f501a11 eq "z"

=back

=head3 Run actions only on the condition's fields

We have already seen that to refers to the condition's field in actions, it is possible to define subfields directly. It works only if we define only one field to be tested in the condition. If we ve'got more than one field in condition, their B<names must also begin with $> to refer them (it works also with a unique field in condition).

For example, if you test $f501a value's in condition:

- this will delete 'c' subfields only in the '501' field which is true in the condition:

    condition : $f501a eq "foo" and defined $f501b
    delete : $f501c

- this will delete 'c' subfields in all '501' fields:

    condition : $f501a eq "foo" and defined $f501b
    delete : f501c

- this will create a new '701' field with a 'c' subfield containing '501$a' subfield's value defined in the condition:

    condition : defined $f501a
    create :
     f701c : $f501a

B<WARNING>: To get B<subfield's value of> the condition's fields, these subfields must be defined in the condition:

- it B<doesn't> work:

    condition : $f501a eq "foo"
    create :
     f701a : $f501c
     
- it works (create a new '701' field with a subfield 'a' containing the condition's '501$c' subfield's value ):

    condition : $f501a eq "foo" and defined $f501c
    create :
     f701a : $f501c

- this restriction is true only for the subfield's values, but isn't true to specify the fields affected by an action: the example below will create a new 'c' subfield B<in a field defined in the condition>.

    condition : $f501a eq "foo" and $f110a == 2
    create :
     $f501c : new subfield value
    # If there are multiple '501' fields, only the one with a 
    # subfield 'a'='foo' will have a new 'c' subfield created

=head2 Actions

=head3 create

=over 4

=item * As the name suggests, this action allows you to create new fields and subfields.

=item * Syntax:

    # basic:
    create :
     <subfield name> : <value>
    
    # to create two subfields (in one field) with same name:
    create :
     <subfield name> :
      - <value>
      - <value>
    
    # advanced:
    create :
     <field name> :
      <subfield name> : 
       - <value>
       - <value>
      <subfield name> : <value>

=item * Example:

    ---
    condition : $f501a eq "foo"
    create :
     b : new subfield's value on the condition's field
     f502a : this is the subfield's value of a new 502 field
     f502b : 
      - this is the first 'b' value of another new 502
      - this is the 2nd 'b' value of this another new 502
     f600 :
      a : 
       - first 'a' subfield of this new 600 field
       - second 'a' subfield of this new 600 field
      b : the 600b value

result (with C<< $record->as_formatted >>):

    --init record--
    LDR                         
    501    _afoo
           _b1
           _cbar
    --transformed record--
    LDR                         
    501    _afoo
           _b1
           _cbar
           _bnew subfield's value on the condition's field
    502    _bthis is the first 'b' value of another new 502
           _bthis is the 2nd 'b' value of this another new 502
    502    _athis is the subfield's value of a new 502 field
    600    _afirst 'a' subfield of this new 600 field
           _asecond 'a' subfield of this new 600 field
           _bthe 600b value

=item * be careful: You need to use lists to create several subfields with the same name in a field:

    # does not work:
    create :
     f502b : value
     f502b : value

=back

=head3 update

=over 4

=item * This action allows you to update B<existing> fields. This action updates all the specified subfields of all specified fields (if the specified field is a condition's field, it will be the only one to be updated)

=item * Syntax:

    # basic:
    update :
     <subfield name> : <value>
    
    # advanced:
    update :
     <subfield name> : <value>
     <subfield name> : <value>
     <field name> :
      <subfield name> : <value>
      <subfield name> : <value>

=item * Example:

    ---
    condition : $f502a eq "second a"
    update :
     b : updated value of all 'b' subfields in the condition field
     f502c : updated value of all 'c' subfields into all '502' fields
     f501 :
      a : updated value of all 'a' subfields into all '501' fields
      b : $f502a is the 502a condition's field's value

result (with C<< $record->as_formatted >>):

    --init record--
    LDR                         
    501    _afoo
           _b1
           _cbar
    502    _afirst a
           _asecond a
           _bbbb
           _cccc1
           _cccc2
    502    _apoto
    502    _btruc
           _cbidule
    --transformed record--
    LDR                         
    501    _aupdated value of all 'a' subfields into all '501' fields
           _bsecond a is the 502a condition's field's value
           _cbar
    502    _afirst a
           _asecond a
           _bupdated value of all 'b' subfields in the condition field
           _cupdated value of all 'c' subfields into all '502' fields
           _cupdated value of all 'c' subfields into all '502' fields
    502    _apoto
    502    _btruc
           _cupdated value of all 'c' subfields into all '502' fields

=back

=head3 updatefirst

=over 4

=item * This action is B<identical to the update>, except that it updates only the B<first> subfield of the specified fields

=item * B<Syntax>: except for the action's name, it's the B<same than the update>'s syntax

=item * Example:

    ---
    condition : $f502a eq "second a"
    updatefirst :
     b : updated value of first 'b' subfields in the condition's field
     f502c : updated value of first 'c' subfields into all '502' fields
     f501 :
      a : updated value of first 'a' subfields into all '501' fields
      b : $f502a is the value of 502a conditionnal field

result (with C<< $record->as_formatted >>):

    --init record--
    LDR                         
    501    _afoo
           _b1
           _cbar
    502    _afirst a
           _asecond a
           _bbbb
           _cccc1
           _cccc2
    502    _apoto
    502    _btruc
           _cbidule
    --transformed record--
    LDR                         
    501    _aupdated value of first 'a' subfields into all '501' fields
           _bsecond a is the value of 502a conditionnal field
           _cbar
    502    _afirst a
           _asecond a
           _bupdated value of first 'b' subfields in the condition's field
           _cupdated value of first 'c' subfields into all '502' fields
           _cccc2
    502    _apoto
    502    _btruc
           _cupdated value of first 'c' subfields into all '502' fields

=back

=head3 forceupdate and forceupdatefirst

=over 4

=item * If the specified B<subfields exist>: these actions are identical to the B<update> and the updatefirst actions

=item * If the specified B<subfields doesn't exist>: these actions are identical to the B<create> action

=item * B<Syntax>: except for the action's name, it's the B<same than the update>'s syntax

=item * Example:

    ---
    condition : $f502a eq "second a"
    forceupdate :
     b : 'b' subfield's value in the condition's field
     f502c : '502c' value's
     f503 :
      a : '503a' value's
      b : $f502a is the 502a condition's value

result (with C<< $record->as_formatted >>):

    --init record--
    LDR                         
    501    _afoo
           _b1
           _cbar
    502    _btruc
           _cbidule
    502    _apoto
    502    _afirst a
           _asecond a
           _bbbb
           _ccc1
           _ccc2
    --transformed record--
    LDR                         
    501    _afoo
           _b1
           _cbar
    502    _btruc
           _c'502c' value's
    502    _apoto
           _c'502c' value's
    502    _afirst a
           _asecond a
           _b'b' subfield's value in the condition's field
           _c'502c' value's
           _c'502c' value's
    503    _a'503a' value's
           _bsecond a is the 502a condition's value
    --transformed record if we had used forceupdatefirst--
    LDR                         
    501    _afoo
           _b1
           _cbar
    502    _btruc
           _c'502c' value's
    502    _apoto
           _c'502c' value's
    502    _afirst a
           _asecond a
           _b'b' subfield's value in the condition's field
           _c'502c' value's
           _ccc2
    503    _a'503a' value's
           _bsecond a is the value of 502a conditionnal field

=back

=head3 delete

=over 4

=item * As the name suggests, this action allows you to delete fields and subfields.

=item * Syntax:

    # basic:
    delete : <field or subfield name>
    
    # advanced:
    delete :
     - <field or subfield name>
     - <field or subfield name>
    

=item * Example:

    ---
    condition : $f501a eq "foo"
    delete : $f501
    ---
    condition : $f501a eq "bar"
    delete : b
    ---
    delete : f502
    ---
    delete : 
     - f503
     - f504a

result (with C<< $record->as_formatted >>):

    --init record--
    LDR                         
    501    _abar
           _bbb1
           _bbb2
    501    _afoo
    502    _apata
    502    _apoto
    503    _apata
    504    _aata1
           _aata2
           _btbbt
    --transformed record--
    LDR                         
    501    _abar
    504    _btbbt

=back

=head3 duplicatefield

=over 4

=item * As the name suggests, this action allows you to duplicate entire fields.

=item * Syntax:

    # basic:
    duplicatefield : <field name> > <field name>
    
    # advanced:
    duplicatefield :
     - <field name> > <field name>
     - <field name> > <field name>

=item * Example:

    ---
    condition : $f008_ eq "controlfield_contentb"
    duplicatefield : $f008 > f007
    ---
    condition : $f501a eq "bar"
    duplicatefield : $f501 > f400
    ---
    condition : $f501a eq "foo"
    duplicatefield : 
     - f501 > f401
     - $f501 > f402
     - f005 > f006

result (with C<< $record->as_formatted >>):

    --init record--
    LDR                         
    005     controlfield_content2
    005     controlfield_content1
    008     controlfield_contentb
    008     controlfield_contenta
    501    _afoo
    501 12 _abar
           _bbb1
           _bbb2
    --transformed record--
    LDR                         
    005     controlfield_content2
    005     controlfield_content1
    006     controlfield_content1
    006     controlfield_content2
    007     controlfield_contentb
    008     controlfield_contentb
    008     controlfield_contenta
    400 12 _abar
           _bbb1
           _bbb2
    401 12 _abar
           _bbb1
           _bbb2
    401    _afoo
    402    _afoo
    501    _afoo
    501 12 _abar
           _bbb1
           _bbb2

=back

=head3 execute

=over 4

=item * This action allows you to to define Perl code that will be run with C<< eval >>

You can run functions written directly in the YAML ( for details on writing perl subs in the YAML, refer to next chapter: Use Perl functions and LookUp Tables ).

=item * Syntax:

    # basic:
    execute : <perl code>
    
    # advanced:
    execute :
     - <perl code>
     - <perl code>  

=item * Example:

    ---
    condition : $f501a eq "bar"
    execute : 
     - warn("f501a eq $f501a")
     - warn("barbar")
    ---
    -
     condition : $f501a eq "foo"
     execute : \&warnfoo("f501a eq $f501a")
    -
     subs : >
        sub warnfoo { my $string = shift;warn $string; }

result (in stderr):

    f501a eq bar at (eval 30) line 6, <$yamls> line 1.
    barbar at (eval 30) line 7, <$yamls> line 1.
    f501a eq foo at (eval 33) line 2, <$yamls> line 1.

=back

=head2 Use Perl functions and LookUp Tables

You can use Perl functions (B<subs>) and lookup tables (B<LUT>) to define with greater flexibility values that will be created or updated by the actions: create, forceupdate, forceupdatefirst, update and updatefirst.

These functions (and lookup tables) can be B<written in a rule> (in this case they can be used only by this rule) B<or after the last rule> ( after the last ---, can be used in all rules: B<global_subs> and B<global_LUT> ).

=head3 Variables

Four types of variables can be used:

=head4 $this, and condition's elements

=over 4

=item * variables pointing on the condition's subfield's values are those we have already seen in Chapter 'Run actions only on condition fields' (e.g. B<$f110c>)

=item * B<$this>: this is the variable to use to pointing to the B<value of current subfield>. $this can also be used outside a sub or a LUT.

Example (N.B.: sub 'fromo2e' converts 'o' to 'e'):

    ---
    -
     condition : $f501a eq "foo"
     create : 
      c : \&fromo2e("$f501a")
     update :
      d : this 501d value's is $this
      b : \&fromo2e("$this")
    -
     subs: >
        sub fromo2e { my $string=shift; $string =~ s/o/e/g; $string; }

result (with C<< $record->as_formatted >>):

    --init record--
    LDR                         
    501    _afoo
           _bboo
           _ddoo
    --transformed record--
    LDR                         
    501    _afoo
           _bbee
           _d this 501d value's is doo
           _cfee

=back

=head4 $mth

B<$mth> is the optional hashref add as third optional argument. It can be used in writing (into subs and global_subs) and reading. This allows interaction with the script that calls MARC::Transform.

=over 4

=item * Syntaxe: B<$$mth{foo}>

=item * Example in a perl script :

    my $record = MARC::Record->new();
    $record->leader('optional leader');
    print "--init record--\n". $record->as_formatted;
    my %mth;
    $mth{"inc"}=1;
    $mth{"var"}="a string";
    my $yaml = '
    ---
    condition : $$mth{"var"} eq "a string"
    forceupdate :
     f500a : $$mth{"var"}
    ---
    -
     execute : \&testa()
    -
     subs: >
        sub testa { $$mth{"inc"}++; }
    ---
    forceupdate :
     f600a : \&testb()
    ---
    global_subs: >
        sub testb { $$mth{"inc"}++;$$mth{"inc"}; }
    ';
    $record = MARC::Transform->new($record,$yaml,\%mth);
    print "\n--transformed record-- ".$mth{"inc"}." : \n". $record->as_formatted ."\n";

result :

    --init record--
    LDR optional leader
    --transformed record-- 3 : 
    LDR optional leader
    500    _aa string
    600    _a3

=back

=head4 $record

B<$record> is the current MARC::Record object.

=head3 subs

=head4 Internal rules

=over 4

=item *  Syntax:

    #full rule:
    ---
    -
     <method invocation syntax in the actions values, in sub-rule(s)>
    -
     subs: >
        <one or more Perl subs>
    ---
    
    # method invocation syntax:
    \&<sub name>("<arguments>")

=item * Example:

    ---
    -
     condition : $f501a eq "foo" and defined $f501d
     update :
      b : \&convertbaddate("$this")
      c : \&trim("$f501d")
    -
     subs: >
        sub convertbaddate {
            #this function convert date like "21/2/98" to "1998-02-28"
            my $in = shift;
            if ($in =~/^(\d{1,2})\/(\d{1,2})\/(\d{2}).*/)
            {
                my $day=$1;
                my $month=$2;
                my $year=$3;
                if ($day=~m/^\d$/) {$day="0".$day;}
                if ($month=~m/^\d$/) {$month="0".$month;}
                if (int($year)>12)
                {$year="19".$year;}
                else {$year="20".$year;}
                return "$year-$month-$day";
            }
            else
            {
                return $in;
            }
        }
        
        sub trim {
            # This function removes ",00" at the end of a string
            my $in = shift;
            $in=~s/,00$//;
            return $in;
        }

result (with C<< $record->as_formatted >>):

    --init record--
    LDR                         
    501    _afoo
           _b8/12/10
           _cboo
           _d40,00
    --transformed record--
    LDR                         
    501    _afoo
           _b2010-12-08
           _c40
           _d40,00

=back

=head4 global_subs

=over 4

=item *  Syntax:

    ---
    global_subs: >
        <one or more Perl subs>
    
    # method invocation syntax:
    \&<sub name>("<arguments>")

=item * Example:

    ---
    condition : $f501a eq "foo"
    update :
     b : \&return_record_encoding()
     c : \&trim("$this")
    ---
    global_subs: >
     sub return_record_encoding {
         $record->encoding();
     }
     
     sub trim {
         # This function removes ",00" at the end of a string
         my $in = shift;
         $in=~s/,00$//;
         return $in;
     }

result (with C<< $record->as_formatted >> ):

    --init record--
    LDR                         
    501    _afoo
           _bbar
           _c40,00
    --transformed record--
    LDR                         
    501    _afoo
           _bMARC-8
           _c40

=back

=head3 LUT

If a value has no match in a LookUp Table, it isn't modified. (unless you have defined a default value with C<< _default_value_ >> ).

If you want to use more than one LookUp Table in a rule, you must use a global_LUT because it differentiates tables with titles.

=head4 Internal rules

=over 4

=item *  Syntax:

    #full rule:
    ---
    -
     <LUT invocation syntax in the actions values, inside sub-rule(s)>
    -
     LUT :
       <starting value> : <final value>
       <starting value> : <final value>
       _default_value_ : optional default value
    ---
    
    # LUT invocation syntax:
    \&LUT("<starting value>")

=item * Example:

    ---
    -
     condition : $f501b eq "bar"
     create :
      f604a : \&LUT("$f501b")
     update :
      c : \&LUT("$this")
    -
     LUT :
      1 : first
      2 : second
      bar : openbar

result (with C<< $record->as_formatted >> ):

    --init record--
    LDR                         
    501    _bbar
           _c1
    --transformed record--
    LDR                         
    501    _bbar
           _cfirst
    604    _aopenbar

=back

=head4 global_LUT

=over 4

=item *  Syntax:

    ---
    global_LUT:
     <LUT title> :
      <starting value> : <final value>
      <starting value> : <final value>
      _default_value_ : valeur par défaut optionnelle
     <LUT title> :
      <starting value> : <final value>
      <starting value> : <final value>
    
    # global_LUT invocation syntax:
    \&LUT("<starting value>","<LUT title>")

=item * Example:

    ---
    update :
     f501a : \&LUT("$this","numbers")
     f501b : \&LUT("$this","cities")
     f501c : \&LUT("$this","cities")
    ---
    global_LUT:
     cities:
      NY : New York
      SF : San Fransisco
      TK : Tokyo
      _default_value_ : unknown city
     numbers:
      1 : one
      2 : two

result (with C<< $record->as_formatted >> ):

    --init record--
    LDR                         
    501    _a1
           _a3
           _bfoo
           _cSF
    --transformed record--
    LDR                         
    501    _aone
           _a3
           _bunknown city
           _cSan Fransisco

=back

=head4 $$mth{"_defaultLUT_to_mth_"}

=over 4

=item *  B<Note: >if you call MARC::Transform with the third parameter, the module filled its key named C<< _defaultLUT_to_mth_ >> in case you want to keep, in the calling script, track of the values and types (named C<< lookuptableforthis >> for non-global's LUT) unmatched. C<< $$mth{"_defaultLUT_to_mth_"} >> is cleared each time MARC::Transform->new is called.

=item * If we recall the above example with :

    my %mth;
    $record = MARC::Transform->new($record,$yaml,\%mth);
    print $mth{"_defaultLUT_to_mth_"}->{"cities"}[0];
    print "\n".Data::Dumper::Dumper $mth{"_defaultLUT_to_mth_"};
    
will return in stdout :

    foo
    $VAR1 = {
              'numbers' => [
                             '3'
                           ],
              'cities' => [
                            'foo'
                          ]
            };

try this if you want to get the content of $mth{"_defaultLUT_to_mth_"} instead of the line containing Data::Dumper::Dumper :

    foreach my $k (keys(%{$mth{"_defaultLUT_to_mth_"}}))
    {
        foreach my $value(@{$mth{"_defaultLUT_to_mth_"}->{"$k"}})
        { print "$k : $value \n"; }
    }

=back

=head1 Latest tips and a big YAML example's

=over 4

=item * If your script return an error like "YAML Error: Stream does not end with newline character", it's easy to fix. If you define your YAML into a variable, it must end with a new blank line. If you give a path to your YAML file, it is probably not good : try with an absolute path.

=item * Restriction: the specific case of double-quotes (") and dollar signs ($): 

In YAML, these characters are interpreted differently. To use them in string context, you will need to replace them in YAML by C<#_dbquote_#> (for ") and C<#_dollars_#> (for $):

. Example:

    ---
    condition : $f501a eq "I want #_dbquote_##_dollars_##_dbquote_#"
    create :
     f604a : "#_dbquote_#$f501a#_dbquote_# contain a #_dollars_# sign"

. result (with C<< $record->as_formatted >> ):

    --init record--
    LDR                         
    501    _aI want "$"
    --transformed record--
    LDR                         
    501    _aI want "$"
    604    _a"I want "$"" contain a $ sign

=item * Restriction: You can test if a field or subfield exists, but it is inadvisable to test its absence.

=item * Example: feel free to copy the examples in this documentation. Be aware that I have added four space characters at the beginning of each example's line to make them better displayed by the POD interpreter. If you copy / paste them into your YAML configuration file, Be sure to remove the first four characters of each line (e.g. with vim, C<:%s/^\s\s\s\s//g> ).

=item * 
This yaml was called like this: C<< my %mth; $mth{"var"}="a string"; $record = MARC::Transform->new($record,$yaml,\%mth); >>

    ---
    condition : $f501a eq "foo"
    create :
     f502a : this is the value of a subfield of a new 502 field
    ---
    condition : $f401a=~/foo/
    create :
     b : new value of the 401 condition's field
     f600 :
      a : 
       - first a subfield of this new 600 field
       - second a subfield of this new 600 field
       - $$mth{"var"}
      b : the 600b value
    execute : \&reencodeRecordtoUtf8()
    ---
    -
     condition : $f501a =~/foo/ and $f503a =~/bar/
     forceupdate :
      $f503b : mandatory b in condition's field
      f005_ : mandatory 005
      f006_ : \&return_record_encoding()
      f700 :
       a : the a subfield of this mandatory 700 field
       b : \&sub1("$f503a")
     forceupdatefirst :
      $f501b : update only the first b in condition's field 501
    -
     condition : $f501a =~/foo/
     execute : \&warnfoo("f501a contain foo")
    -
     subs : >
        sub return_record_encoding { $record->encoding(); }
        sub sub1 {my $string=shift;$string =~ s/a/e/g;return $string;}
        sub warnfoo { my $string = shift;warn $string; }
    ---
    -
     condition : $f501b2 eq "o"
     update :
      c : updated value of all c in condition's field
      f504a : updated value of all 504a if exists
      f604 :
       b : \&LUT("$this")
       c : \&LUT("NY","cities")
     updatefirst :
      f604a : update only the first a in 604
    -
     condition : $f501c eq "1"
     delete : $f501
    -
     LUT :
       1 : first
       2 : second
       bar : openbar
    ---
    delete :
     - f401a
     - f005
    ---
    condition : $ldr2 eq "t"
    execute : \&SetRecordToLowerCase($record)
    ---
    condition : $f008_ eq "controlfield_content8b"
    duplicatefield :
     - $f008 > f007
     - f402 > f602
    delete : f402
    ---
    global_subs: >
        sub reencodeRecordtoUtf8 {
            $record->encoding( 'UTF-8' );
        }
        sub warnfee {
            my $string = shift;warn $string;
        }
    global_LUT:
     cities:
      NY : New York
      SF : San Fransisco
     numbers:
      1 : one
      2 : two

result (with C<< $record->as_formatted >> ) :

    --init record--
    LDR optional leader
    005     controlfield_content
    008     controlfield_content8a
    008     controlfield_content8b
    106    _aVaLuE
    401    _aafooa
    402  2 _aa402a2
    402 1  _aa402a1
    501    _c1
    501    _afoo
           _afoao
           _b1
           _bbaoar
           _cbig
    503    _afee
           _ababar
    504    _azut
           _asisi
    604    _afoo
           _afoo
           _bbar
           _ctruc
    
    --transformed record--
    LDR optional leader
    006     UTF-8
    007     controlfield_content8b
    008     controlfield_content8a
    008     controlfield_content8b
    106    _aVaLuE
    401    _bnew value of the 401 condition's field
    501    _c1
    501    _afoo
           _afoao
           _bupdate only the first b in condition's field 501
           _bbaoar
           _cupdated value of all c in condition's field
    502    _athis is the value of a subfield of a new 502 field
    503    _afee
           _ababar
           _bmandatory b in condition's field
    504    _aupdated value of all 504a if exists
           _aupdated value of all 504a if exists
    600    _aa string
           _afirst a subfield of this new 600 field
           _asecond a subfield of this new 600 field
           _bthe 600b value
    602 1  _aa402a1
    602  2 _aa402a2
    604    _aupdate only the first a in 604
           _afoo
           _bopenbar
           _cNew York
    700    _athe a subfield of this mandatory 700 field
           _bbeber

=back

=head1 SEE ALSO

=over 4

=item * MARC::Record (L<http://search.cpan.org/perldoc?MARC::Record>)

=item * MARC::Field (L<http://search.cpan.org/perldoc?MARC::Field>)

=item * MARC::Loader (L<http://search.cpan.org/perldoc?MARC::Loader>)

=item * YAML (L<http://search.cpan.org/perldoc?YAML>)

=item * Library Of Congress MARC pages (L<http://www.loc.gov/marc/>)

The definitive source for all things MARC.

=back

=head1 AUTHOR

Stephane Delaune, (delaune.stephane at gmail.com)

=head1 COPYRIGHT

Copyright 2011-2018 Stephane Delaune for Biblibre.com, all rights reserved.

This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=head1 ===========> DOCUMENTATION FRANCAISE

=head1 - NOM

=encoding utf8

MARC::Transform - Module Perl pour transformer une notice MARC en utilisant un fichier de configuration YAML

=head1 - VERSION

Version 0.003007

=head1 - SYNOPSIS

B<Perl script:>

    use MARC::Transform;

    # Pour ce synopsis, nous créons une petite notice:
    my $record = MARC::Record->new();
    $record->insert_fields_ordered( MARC::Field->new( 
                                    '501', '', '', 
                                    'a' => 'foo', 
                                    'b' => '1', 
                                    'c' => 'bar', 
                                    'd' => 'bor' ) );

    print "--notice d'origine--\n". $record->as_formatted ."\n";

    # Nous transformons notre notice avec notre fichier de 
    # configuration YAML avec son chemin absolu ( ou
    # relatif si il est appelé depuis le bon endroit) :
    $record = MARC::Transform->new ( $record, "/path/conf.yaml" );

    # Vous pouvez aussi écrire votre YAML dans une variable:
    my $yaml="delete : f501d\n";
    # et l'utiliser pour transformer la notice:
    $record = MARC::Transform->new ( $record, $yaml );

    print "\n--notice transformée--\n". $record->as_formatted ."\n";

B<conf.yaml:>

    ---
    condition : $f501a eq "foo"
    create :
     f502a : New 502a subfield's value
    update :
      $f501b : \&LUT("$this")
    LUT :
     1 : first
     2 : second value in this LUT (LookUp Table)
    ---
    delete : f501c

B<Résultat> (avec C<< $record->as_formatted >>):

    --notice d'origine--
    LDR                         
    501    _afoo
           _b1
           _cbar
           _dbor
    
    --notice transformée--
    LDR                         
    501    _afoo
           _bfirst
    502    _aNew 502a subfield's value

=head1 - DESCRIPTION

C'est un module Perl pour transformer une notice MARC en utilisant un fichier de configuration YAML.

Il permet de B<créer> , B<mettre à jour> , B<supprimer> , B<dupliquer> les champs et les sous-champs d'une notice. Vous pouvez aussi utiliser des B<scripts> et des B<tables de correspondance>. Vous pouvez préciser des B<conditions> pour executer ces actions.

Toutes les conditions, actions, fonctions et tables de correspondance sont B<définies dans le YAML>.

MARC::Transform utilise MARC::Record.

=head1 - METHODE

=head2 - new()

    $record = MARC::Transform->new($record, "/path/conf.yaml" );

C'est la seule méthode que vous utiliserez. Elle prend un objet MARC::Record et le chemin vers un YAML comme arguments. Vous pouvez aussi écrire votre YAML dans une variable et l'utiliser pour transformer la notice comme ceci : 

    my $yaml="delete : f501d\n";
    $record = MARC::Transform->new ( $record, $yaml );

=head3 - Référence à un hash optionnel

Comme nous allons le voir plus en détail plus bas, il est possible d'ajouter comme troisième argument une référence à un hash (nommé $mth dans le yaml).

    my $record = MARC::Record->new(); my $hashref = {'var' => 'foo'};
    my $yaml = 'create :
     f500a : $$mth{"var"}
    ';
    $record = MARC::Transform->new($record,$yaml,$hashref);
    #la valeur du nouveau sous-champ 500$a est "foo"

=head3 - Mode verbeux

Chaque règle du YAML (voir les bases plus bas pour comprendre ce qu'est une règle) génère un script qui est évalué, dans la notice, pour chaque champ et sous-champ spécifié dans la condition (si il y a une condition). En ajoutant un quatrième argument optionnel B<1> à la méthode, elle affiche le script généré. Cela peut être utile pour comprendre ce qu'il se passe:

    $record = MARC::Transform->new($record,"/path/conf.yaml",0,1);

=head1 - YAML

=head2 - Bases

- B<Le YAML est divisé en règle> (séparées par --- ), chaque règle est exécutée l'une après l'autre, les règles sans condition sont toujours exécutées:

    ---
    condition : $f501a eq "foo"
    create :
     f600a : new field value
    ---
    delete : f501c
    ---

- B<les conditions sont écrites en perl>, ce qui permet une grande flexibilité. Elles doivent être définies avec C<condition : >

    condition : ($f501a=~/foo/ and $f503a=~/bar/) or ($f102a eq "bib")
    # si un 501$a et un 503$a contiennent foo et bar, ou si un 102$a = bib

- Les conditions testent les notices B<champ par champ> (uniquement sur les champs définis dans la condition)

Cela signifie, par exemple, que si nous avons plusieurs champs '501' dans la notice, si notre condition est C<$f501a eq "foo" and $f501b eq "bar">, cette condition sera vrai uniquement si un champ '501' a un sous-champ 'a' = "foo" ET un sous-champ 'b' = 'bar' (elle sera fausse si il y a un champ '501' avec un sous-champ 'a' = "foo" et UN AUTRE champ '501' avec un sous-champ 'b' = "bar").

- Il est possible de lancer plusieurs actions différentes dans une seule règle:

    ---
    condition : $f501a eq "foo"
    create :
     f600a : new field value
    delete : f501c
    ---
    
- L'ordre dans lequel les actions sont écrites n'a pas d'importance. Les actions seront toujours exécutée dans l'ordre suivant:

=over 4

=item * create

=item * duplicatefield

=item * forceupdate

=item * forceupdatefirst

=item * update

=item * updatefirst

=item * execute

=item * delete

=back

- B<Chaque règle peut être divisée en sous-règles> (séparées par - ) similaires à un 'if,elsif' ou un script 'switch,case'. Si la condition de la première sous-règle est fausse, la condition de la sous-règle suivante est testée. Lorsque la condition d'une sous-règle est vraie (ou si un sous-règle n'a pas de condition), les sous-règles suivantes ne sont pas lues.

    ---
    -
     condition : $f501a eq "foo"
     create :
      f502a : value if foo
    -
     condition : $f501a eq "bar"
     create :
      f502a : value elsif bar
    -
     create :
      f502a : value else
    ---
    # Si une sous-règle n'a pas de condition, elle sera considérée
    # comme un 'else' (les sous-règles suivantes ne seront pas lues)

- Il n'est pas permis de définir plus d'une action similaire dans une seule (sous-)règle. Cependant, il reste possible d'exécuter une action similaire plusieurs fois dans une seule règle (se référer à la syntaxe spécifique à chaque action pour voir comment faire cela):

.   Cela n'est B<pas> permis:

    ---
    delete : f501b
    delete : f501c

.   cela fonctionne:

    ---
    delete :
     - f501b
     - f501c

=head3 - un petit script pour tester vos règles

- B<Il est fortement recommendé de tester chaque règle sur une notice de test avant de l'utiliser sur un large lot de notices>. Vous pouvez créer un script (par exemple C<< test.pl >>) avec le contenu ci-dessous (que vous adapterez pour tester vos règles) et le lancer avec C<< perl ./test.pl >> :

    #!/usr/bin/perl
    use MARC::Transform;
    my $record = MARC::Record->new();
    $record->leader('optional leader');
    $record->insert_fields_ordered( MARC::Field->new('005', 'controlfield_content'));
    $record->insert_fields_ordered( MARC::Field->new('501', '', '', 'a' => 'foo', 'b' => 'bar') );
    print "\n--init record--\n". $record->as_formatted ."\n";
    my $yaml='---
    condition : $f501a eq "foo"
    create :
     f502a : condition is true
    ';
    $record = MARC::Transform->new($record,$yaml);
    print "\n--transformed record--\n". $record->as_formatted ."\n";

=head2 - Convention de nommage des champs et des sous-champs

=head3 - Dans les actions

- Les noms des champs et des sous-champs sont très importants: 

=over 4

=item * Ils doivent commencer par la lettre B<f> suivi des B<3 chiffres> du nom du champ (par exemple f099), suivi, pour les sous-champs, par leur B<lettre ou chiffre> (par exemple B<f501b>).

=item * Les noms des champs de contrôle commencent par la lettre B<f> suivi par B<3 chiffres inférieur à 010> suivi par B<underscore> (par exemple B<f005_>). 

=item * B<Les indicateurs> doivent commencer par la lettre B<i>, suivi des B<3 chiffres> du nom du champ suivi par la position de l'indicateur(B<1 or 2>) (par exemple B<i0991>).

=item * Dans les actions, vous pouvez définir B<un sous-champ directement> (ou un indicateur avec i1 ou i2). Selon le contexte, il se réfère au champ de la condition (si nous n'avons défini qu'un seul champ à tester dans la condition), ou au champ en cours de traitement dans l'action:

    ---
    condition : $f501a eq "foo"
    create :
     b : new 'b' subfield's value in unique condition's field (501)
     f600 :
      i1 : 1
      a : new subfield (a) in this new 600 field
    ---

=back

=head3 - Dans les conditions

=over 4

=item * Dans les conditions la convention de nommage des champs et des sous-champs suit les B<mêmes règles que les actions>, mais ils doivent être B<précédés du symbole dollar $> (par exemple C<$f110c> pour un sous-champ ou C<$i0991> pour un indicateur).

=item * Le leader de la notice peut être défini avec B<$ldr>.

=item * Il est possible de tester la valeur d'un seul caractère dans les sous-champs ou le leader. Pour faire cela, vous devez ajouter la B<position de ce caractère à partir de 0 et jusqu'à 99>:

    #pour tester le 3e caractère du leader et le 12e dans le 501$a:
    condition : $ldr2 eq "t" and $f501a11 eq "z"

=back

=head3 - Lancer des actions uniquement sur les champs de la condition

Nous avons déjà vu que pour se référer au champ de la condition dans les actions, il est possible de définir les sous-champs directement. Cela fonctionne uniquement si nous avons définis seulement un champ à tester dans la condition. Si nous avons plus d'un champ dans la condition, pour s'y référer, leurs B<noms doivent aussi commencer par $> (cela fonctionne également avec un champ unique dans la condition).

Par exemple, si vous testez la valeur du $f501a dans la condition:

- cela va supprimer les sous-champs 'c' uniquement dans le champ '501' qui est vrai dans la condition:

    condition : $f501a eq "foo" and defined $f501b
    delete : $f501c

- cela va supprimer les sous-champs 'c' dans tous les champs '501':

    condition : $f501a eq "foo" and defined $f501b
    delete : f501c

- cela va créer un nouveau champ '701' avec un sous-champ 'c' contenant la valeur du sous-champ 501$a défini dans la condition:

    condition : defined $f501a
    create :
     f701c : $f501a

B<ATTENTION>: Pour avoir B<la valeur> des sous-champs des champs de la condition, ces sous-champs doivent être définis dans la condition:

- cela ne fonctionne B<pas>:

    condition : $f501a eq "foo"
    create :
     f701a : $f501c
     
- cela fonctionne (crée un nouveau champ '701' avec un sous-champ 'a' contenant la valeur du sous-champ 501$c de la condition):

    condition : $f501a eq "foo" and defined $f501c
    create :
     f701a : $f501c

- Cette restriction est vrai uniquement pour les valeurs des sous-champs mais pas pour spécifier les champs affectés par une action : l'exemple ci-dessous va créer un nouveau sous-champ 'c' B<dans un champ défini dans la condition>.

    condition : $f501a eq "foo" and $f110a == 2
    create :
     $f501c : new subfield value
    # Si il y a de multiples champs '501', seuls ceux ayant un
    # sous-champ 'a'='foo' auront un nouveau sous-champ 'c' créé

=head2 - Actions

=head3 - create

=over 4

=item * Comme son nom l'indique, cette action vous permet de créer de nouveaux champs et sous-champs.

=item * Syntaxe:

    # basique:
    create :
     <nom de sous-champ> : <valeur>
    
    # pour créer deux sous-champs (dans un champ) avec le même nom:
    create :
     <nom de sous-champ> :
      - <valeur>
      - <valeur>
    
    # avancée:
    create :
     <nom de champ> :
      <nom de sous-champ> : 
       - <valeur>
       - <valeur>
      <nom de sous-champ> : <valeur>

=item * Exemple:

    ---
    condition : $f501a eq "foo"
    create :
     b : new subfield's value on the condition's field
     f502a : this is the subfield's value of a new 502 field
     f502b : 
      - this is the first 'b' value of another new 502
      - this is the 2nd 'b' value of this another new 502
     f600 :
      a : 
       - first 'a' subfield of this new 600 field
       - second 'a' subfield of this new 600 field
      b : the 600b value

résultat (avec C<< $record->as_formatted >>):

    --notice d'origine--
    LDR                         
    501    _afoo
           _b1
           _cbar
    --notice transformée--
    LDR                         
    501    _afoo
           _b1
           _cbar
           _bnew subfield's value on the condition's field
    502    _bthis is the first 'b' value of another new 502
           _bthis is the 2nd 'b' value of this another new 502
    502    _athis is the subfield's value of a new 502 field
    600    _afirst 'a' subfield of this new 600 field
           _asecond 'a' subfield of this new 600 field
           _bthe 600b value

=item * faites attention: vous devez utilisez des listes pour créer plusieurs sous-champs avec le même nom dans un champ:

    # ne fonctionne pas:
    create :
     f502b : value
     f502b : value

=back

=head3 - update

=over 4

=item * Cette action vous permet de mettre à jour les champs B<existants>. Cette action met à jour tous les sous-champs spécifiés de tous les champs spécifiés (si le champ spécifié est un champ de la condition, il sera le seul à être mis à jour )

=item * Syntaxe:

    # basique:
    update :
     <nom de sous-champ> : <valeur>
    
    # avancée:
    update :
     <nom de sous-champ> : <valeur>
     <nom de sous-champ> : <valeur>
     <nom de champ> :
      <nom de sous-champ> : <valeur>
      <nom de sous-champ> : <valeur>

=item * Exemple:

    ---
    condition : $f502a eq "second a"
    update :
     b : updated value of all 'b' subfields in the condition field
     f502c : updated value of all 'c' subfields into all '502' fields
     f501 :
      a : updated value of all 'a' subfields into all '501' fields
      b : $f502a is the 502a condition's field's value

résultat (avec C<< $record->as_formatted >>):

    --notice d'origine--
    LDR                         
    501    _afoo
           _b1
           _cbar
    502    _afirst a
           _asecond a
           _bbbb
           _cccc1
           _cccc2
    502    _apoto
    502    _btruc
           _cbidule
    --notice transformée--
    LDR                         
    501    _aupdated value of all 'a' subfields into all '501' fields
           _bsecond a is the 502a condition's field's value
           _cbar
    502    _afirst a
           _asecond a
           _bupdated value of all 'b' subfields in the condition field
           _cupdated value of all 'c' subfields into all '502' fields
           _cupdated value of all 'c' subfields into all '502' fields
    502    _apoto
    502    _btruc
           _cupdated value of all 'c' subfields into all '502' fields

=back

=head3 - updatefirst

=over 4

=item * Cette action est B<identique à update>, excepté qu'elle met à jour seulement le B<premier> sous-champ des champs spécifiés

=item * B<Syntaxe>: excepté pour le nom de l'action, c'est la B<même que la syntaxe de update>

=item * Exemple:

    ---
    condition : $f502a eq "second a"
    updatefirst :
     b : updated value of first 'b' subfields in the condition's field
     f502c : updated value of first 'c' subfields into all '502' fields
     f501 :
      a : updated value of first 'a' subfields into all '501' fields
      b : $f502a is the value of 502a conditionnal field

résultat (avec C<< $record->as_formatted >>):

    --notice d'origine--
    LDR                         
    501    _afoo
           _b1
           _cbar
    502    _afirst a
           _asecond a
           _bbbb
           _cccc1
           _cccc2
    502    _apoto
    502    _btruc
           _cbidule
    --notice transformée--
    LDR                         
    501    _aupdated value of first 'a' subfields into all '501' fields
           _bsecond a is the value of 502a conditionnal field
           _cbar
    502    _afirst a
           _asecond a
           _bupdated value of first 'b' subfields in the condition's field
           _cupdated value of first 'c' subfields into all '502' fields
           _cccc2
    502    _apoto
    502    _btruc
           _cupdated value of first 'c' subfields into all '502' fields

=back

=head3 - forceupdate et forceupdatefirst

=over 4

=item * Si le B<sous-champ spécifié existe>: ces actions sont identiques aux actions B<update> et updatefirst

=item * Si le B<sous-champ spécifié n'existe pas>: ces actions sont identiques à l'action B<create>

=item * B<Syntaxe>: excepté pour le nom de l'action, c'est la B<même que la syntaxe de update>

=item * Exemple:

    ---
    condition : $f502a eq "second a"
    forceupdate :
     b : 'b' subfield's value in the condition's field
     f502c : '502c' value's
     f503 :
      a : '503a' value's
      b : $f502a is the 502a condition's value

résultat (avec C<< $record->as_formatted >>):

    --notice d'origine--
    LDR                         
    501    _afoo
           _b1
           _cbar
    502    _btruc
           _cbidule
    502    _apoto
    502    _afirst a
           _asecond a
           _bbbb
           _ccc1
           _ccc2
    --notice transformée--
    LDR                         
    501    _afoo
           _b1
           _cbar
    502    _btruc
           _c'502c' value's
    502    _apoto
           _c'502c' value's
    502    _afirst a
           _asecond a
           _b'b' subfield's value in the condition's field
           _c'502c' value's
           _c'502c' value's
    503    _a'503a' value's
           _bsecond a is the 502a condition's value
    --notice transformée si nous avions utilisé forceupdatefirst--
    LDR                         
    501    _afoo
           _b1
           _cbar
    502    _btruc
           _c'502c' value's
    502    _apoto
           _c'502c' value's
    502    _afirst a
           _asecond a
           _b'b' subfield's value in the condition's field
           _c'502c' value's
           _ccc2
    503    _a'503a' value's
           _bsecond a is the value of 502a conditionnal field

=back

=head3 - delete

=over 4

=item * Comme son nom l'indique, cette action vous permet de supprimer des champs et sous-champs.

=item * Syntaxe:

    # basique:
    delete : <nom de champ ou sous-champ>
    
    # avancée:
    delete :
     - <nom de champ ou sous-champ>
     - <nom de champ ou sous-champ>
    

=item * Exemple:

    ---
    condition : $f501a eq "foo"
    delete : $f501
    ---
    condition : $f501a eq "bar"
    delete : b
    ---
    delete : f502
    ---
    delete : 
     - f503
     - f504a

résultat (avec C<< $record->as_formatted >>):

    --notice d'origine--
    LDR                         
    501    _abar
           _bbb1
           _bbb2
    501    _afoo
    502    _apata
    502    _apoto
    503    _apata
    504    _aata1
           _aata2
           _btbbt
    --notice transformée--
    LDR                         
    501    _abar
    504    _btbbt

=back

=head3 - duplicatefield

=over 4

=item * Comme son nom l'indique, cette action vous permet de dupliquer des champs entiers.

=item * Syntaxe:

    # basique:
    duplicatefield : <nom de champ> > <nom de champ>
    
    # avancée:
    duplicatefield :
     - <nom de champ> > <nom de champ>
     - <nom de champ> > <nom de champ>

=item * Exemple:

    ---
    condition : $f008_ eq "controlfield_contentb"
    duplicatefield : $f008 > f007
    ---
    condition : $f501a eq "bar"
    duplicatefield : $f501 > f400
    ---
    condition : $f501a eq "foo"
    duplicatefield : 
     - f501 > f401
     - $f501 > f402
     - f005 > f006

résultat (avec C<< $record->as_formatted >>):

    --notice d'origine--
    LDR                         
    005     controlfield_content2
    005     controlfield_content1
    008     controlfield_contentb
    008     controlfield_contenta
    501    _afoo
    501 12 _abar
           _bbb1
           _bbb2
    --notice transformée--
    LDR                         
    005     controlfield_content2
    005     controlfield_content1
    006     controlfield_content1
    006     controlfield_content2
    007     controlfield_contentb
    008     controlfield_contentb
    008     controlfield_contenta
    400 12 _abar
           _bbb1
           _bbb2
    401 12 _abar
           _bbb1
           _bbb2
    401    _afoo
    402    _afoo
    501    _afoo
    501 12 _abar
           _bbb1
           _bbb2

=back

=head3 - execute

=over 4

=item * Cette action vous permet de définir du code Perl qui sera exécuté avec C<< eval >>

Vous pouvez executer des fonctions écrites directement dans le YAML ( pour des détails sur l'écriture de subs perl dans le YAML, référez vous au chapitre suivant: Utiliser des fonctions Perl et des tables des correspondance)

=item * Syntaxe:

    # basique:
    execute : <code perl>
    
    # avancée:
    execute :
     - <code perl>
     - <code perl>

=item * Exemple:

    ---
    condition : $f501a eq "bar"
    execute : 
     - warn("f501a eq $f501a")
     - warn("barbar")
    ---
    -
     condition : $f501a eq "foo"
     execute : \&warnfoo("f501a eq $f501a")
    -
     subs : >
        sub warnfoo { my $string = shift;warn $string; }

résultat (dans stderr):

    f501a eq bar at (eval 30) line 6, <$yamls> line 1.
    barbar at (eval 30) line 7, <$yamls> line 1.
    f501a eq foo at (eval 33) line 2, <$yamls> line 1.

=back

=head2 - Utiliser des fonctions Perl et des tables des correspondance

Vous pouvez utiliser des fonctions Perl (B<subs>) et des tables de correspondance (B<LUT> pour LookUp Tables) pour définir avec une plus grande flexibilité les valeurs qui vont être créées ou mises à jour avec les actions: create, forceupdate, forceupdatefirst, update and updatefirst.

Ces fonctions (et tables des correspondance) peuvent être B<écrites dans une règle> (dans ce cas elles ne peuvent être utilisée que par cette règle) B<ou après la dernière règle> ( après le dernier ---, elles peuvent alors être utilisées dans toutes les règles: B<global_subs> et B<global_LUT> ).

=head3 - Variables

Quatre types de variables peuvent être utilisés:

=head4 - $this, et les éléments de la condition

=over 4

=item * les variables pointant sur la valeur des sous-champs de la condition sont celles que nous avons déjà vu dans le chapitre 'Lancer des actions uniquement sur les champs de la condition' (par exemple B<$f110c>)

=item * B<$this>: c'est la variable à utiliser pour pointer sur la B<valeur du sous-champ en cours>. $this peut aussi être utilisée dans une sub ou une LUT.

Exemple (N.B.: la sub 'fromo2e' convertit les 'o' en 'e'):

    ---
    -
     condition : $f501a eq "foo"
     create : 
      c : \&fromo2e("$f501a")
     update :
      d : this 501d value's is $this
      b : \&fromo2e("$this")
    -
     subs: >
        sub fromo2e { my $string=shift; $string =~ s/o/e/g; $string; }

résultat (avec C<< $record->as_formatted >>):

    --notice d'origine--
    LDR                         
    501    _afoo
           _bboo
           _ddoo
    --notice transformée--
    LDR                         
    501    _afoo
           _bbee
           _d this 501d value's is doo
           _cfee

=back

=head4 - $mth

B<$mth> est l'éventuel hashref passé comme troisième argument. Il est utilisable en écriture (dans les subs et les global_subs) et en lecture. Cela permet d'intéragir avec le script qui appelle MARC::Transform.

=over 4

=item * Syntaxe: B<$$mth{foo}>

=item * Exemple dans un script perl :

    my $record = MARC::Record->new();
    $record->leader('optional leader');
    print "--init record--\n". $record->as_formatted;
    my %mth;
    $mth{"inc"}=1;
    $mth{"var"}="a string";
    my $yaml = '
    ---
    condition : $$mth{"var"} eq "a string"
    forceupdate :
     f500a : $$mth{"var"}
    ---
    -
     execute : \&testa()
    -
     subs: >
        sub testa { $$mth{"inc"}++; }
    ---
    forceupdate :
     f600a : \&testb()
    ---
    global_subs: >
        sub testb { $$mth{"inc"}++;$$mth{"inc"}; }
    ';
    $record = MARC::Transform->new($record,$yaml,\%mth);
    print "\n--transformed record-- ".$mth{"inc"}." : \n". $record->as_formatted ."\n";

résultat :

    --init record--
    LDR optional leader
    --transformed record-- 3 : 
    LDR optional leader
    500    _aa string
    600    _a3

=back

=head4 - $record

B<$record> est l'objet MARC::Record en cours de traitement.

=head3 - subs

=head4 - A l'intérieur des règles

=over 4

=item *  Syntaxe:

    #règle entière:
    ---
    -
     <syntaxe d'invocation de la méthode dans les valeurs des actions>
    -
     subs: >
        <une ou plusieurs subs Perl>
    ---
    
    # syntaxe d'invocation de la méthode:
    \&<sub name>("<arguments>")

=item * Exemple:

    ---
    -
     condition : $f501a eq "foo" and defined $f501d
     update :
      b : \&convertbaddate("$this")
      c : \&trim("$f501d")
    -
     subs: >
        sub convertbaddate {
            #convertit les dates du type "21/2/98" en "1998-02-28"
            my $in = shift;
            if ($in =~/^(\d{1,2})\/(\d{1,2})\/(\d{2}).*/)
            {
                my $day=$1;
                my $month=$2;
                my $year=$3;
                if ($day=~m/^\d$/) {$day="0".$day;}
                if ($month=~m/^\d$/) {$month="0".$month;}
                if (int($year)>12)
                {$year="19".$year;}
                else {$year="20".$year;}
                return "$year-$month-$day";
            }
            else
            {
                return $in;
            }
        }
        
        sub trim {
            # Cette fonction enlève ",00" à la fin d'une chaîne
            my $in = shift;
            $in=~s/,00$//;
            return $in;
        }

résultat (avec C<< $record->as_formatted >>):

    --notice d'origine--
    LDR                         
    501    _afoo
           _b8/12/10
           _cboo
           _d40,00
    --notice transformée--
    LDR                         
    501    _afoo
           _b2010-12-08
           _c40
           _d40,00

=back

=head4 - global_subs

=over 4

=item *  Syntaxe:

    ---
    global_subs: >
        <une ou plusieurs subs Perl>
    
    # syntaxe d'invocation de la méthode
    \&<sub name>("<arguments>")

=item * Exemple:

    ---
    condition : $f501a eq "foo"
    update :
     b : \&return_record_encoding()
     c : \&trim("$this")
    ---
    global_subs: >
     sub return_record_encoding {
         $record->encoding();
     }
     
     sub trim {
         # Cette fonction enlève ",00" à la fin d'une chaîne
         my $in = shift;
         $in=~s/,00$//;
         return $in;
     }

résultat (avec C<< $record->as_formatted >> ):

    --notice d'origine--
    LDR                         
    501    _afoo
           _bbar
           _c40,00
    --notice transformée--
    LDR                         
    501    _afoo
           _bMARC-8
           _c40

=back

=head3 - LUT

Si une valeur n'a pas de correspondance dans une table de correspondance, elle n'est pas modifiée (à moins que vous n'ayez définit une valeur par défaut avec C<< _default_value_ >> ).

Si vous voulez utiliser plus d'une table de correspondance dans une règle, vous devez utiliser une global_LUT car elle différencie les tables avec des titres.

=head4 - A l'intérieur des règles

=over 4

=item *  Syntaxe:

    #règle entière:
    ---
    -
     <syntaxe d'invocation de la LUT dans les valeurs des actions>
    -
     LUT :
       <valeur de départ> : <valeur finale>
       <valeur de départ> : <valeur finale>
       _default_value_ : valeur par défaut optionnelle
    ---
    
    # syntaxe d'invocation de la LUT:
    \&LUT("<valeur de départ>")

=item * Exemple:

    ---
    -
     condition : $f501b eq "bar"
     create :
      f604a : \&LUT("$f501b")
     update :
      c : \&LUT("$this")
    -
     LUT :
      1 : first
      2 : second
      bar : openbar

résultat (avec C<< $record->as_formatted >> ):

    --notice d'origine--
    LDR                         
    501    _bbar
           _c1
    --notice transformée--
    LDR                         
    501    _bbar
           _cfirst
    604    _aopenbar

=back

=head4 - global_LUT

=over 4

=item *  Syntaxe:

    ---
    global_LUT:
     <titre de la LUT> :
      <valeur de départ> : <valeur finale>
      <valeur de départ> : <valeur finale>
      _default_value_ : valeur par défaut optionnelle
     <titre de la LUT> :
      <valeur de départ> : <valeur finale>
      <valeur de départ> : <valeur finale>
    
    # syntaxe d'invocation de la global_LUT:
    \&LUT("<valeur de départ>","<titre de la LUT>")

=item * Exemple:

    ---
    update :
     f501a : \&LUT("$this","numbers")
     f501b : \&LUT("$this","cities")
     f501c : \&LUT("$this","cities")
    ---
    global_LUT:
     cities:
      NY : New York
      SF : San Fransisco
      TK : Tokyo
      _default_value_ : unknown city
     numbers:
      1 : one
      2 : two

résultat (avec C<< $record->as_formatted >> ):

    --notice d'origine--
    LDR                         
    501    _a1
           _a3
           _bfoo
           _cSF
    --notice transformée--
    LDR                         
    501    _aone
           _a3
           _bunknown city
           _cSan Fransisco

=back

=head4 - $$mth{"_defaultLUT_to_mth_"}

=over 4

=item *  B<NB : >si vous appelez MARC::Transform avec le troisième paramètre, le module en rempli la clé nommée C<< _defaultLUT_to_mth_ >> au cas où vous souhaiteriez conserver, dans le script d'appel, une trace des valeurs et types (nommé C<< lookuptableforthis >> pour les LUT non globales) sans correspondance. C<< $$mth{"_defaultLUT_to_mth_"} >> est vidée à chaque fois que MARC::Transform->new est appelé.

=item * Si nous rappellons l'exemple ci-dessus avec :

    my %mth;
    $record = MARC::Transform->new($record,$yaml,\%mth);
    print $mth{"_defaultLUT_to_mth_"}->{"cities"}[0];
    print "\n".Data::Dumper::Dumper $mth{"_defaultLUT_to_mth_"};
    
cela renverra sur la sortie standard :

    foo
    $VAR1 = {
              'numbers' => [
                             '3'
                           ],
              'cities' => [
                            'foo'
                          ]
            };

essayez ceci si vous souhaitez récupérer le contenu de $mth{"_defaultLUT_to_mth_"} à la place de la ligne contenant Data::Dumper::Dumper :

    foreach my $k (keys(%{$mth{"_defaultLUT_to_mth_"}}))
    {
        foreach my $value(@{$mth{"_defaultLUT_to_mth_"}->{"$k"}})
        { print "$k : $value \n"; }
    }

=back

=head1 - Dernières astuces et un gros exemple de YAML

=over 4

=item * Si votre script renvoie une erreur du type "YAML Error: Stream does not end with newline character", c'est simple à corriger. Si vous avez défini votre YAML dans une variable, elle doit se terminer par une nouvelle ligne vide. Si vous avez donner un chemin vers votre fichier, il n'est vraisemblablement pas bon : essayez avec un chemin absolu.

=item * Restriction: le cas spécifique des guillemets doubles (") et du symbole dollar ($): 

Dans le YAML, ces caractères sont interprétés différemment. Pour les utiliser dans un contexte de chaîne de caractère, vous devrez les remplacer  dans le YAML par C<#_dbquote_#> (pour ") et C<#_dollars_#> (pour $):

. Exemple:

    ---
    condition : $f501a eq "I want #_dbquote_##_dollars_##_dbquote_#"
    create :
     f604a : "#_dbquote_#$f501a#_dbquote_# contain a #_dollars_# sign"

. résultat (avec C<< $record->as_formatted >> ):

    --notice d'origine--
    LDR                         
    501    _aI want "$"
    --notice transformée--
    LDR                         
    501    _aI want "$"
    604    _a"I want "$"" contain a $ sign

=item * Restriction: Vous pouvez tester si un champ ou sous-champ existe, mais il est déconseillé de tester son absence.

=item * Exemple: n'hésitez pas à copier les exemples de cette documentation. Faite attention car j'ai ajouté quatre caractères espace au début de chaque ligne des exemples pour qu'ils soient mieux affichés par l'interpréteur POD. Si vous les copiez / collez dans votre fichier de configuration YAML, n'oubliez pas de supprimer les quatres premier caractères de chaque ligne (par exemple avec vim, C<:%s/^\s\s\s\s//g> ). 

=item * Ce yaml a été appellé comme ceci: C<< my %mth; $mth{"var"}="a string"; $record = MARC::Transform->new($record,$yaml,\%mth); >>

    ---
    condition : $f501a eq "foo"
    create :
     f502a : this is the value of a subfield of a new 502 field
    ---
    condition : $f401a=~/foo/
    create :
     b : new value of the 401 condition's field
     f600 :
      a : 
       - first a subfield of this new 600 field
       - second a subfield of this new 600 field
       - $$mth{"var"}
      b : the 600b value
    execute : \&reencodeRecordtoUtf8()
    ---
    -
     condition : $f501a =~/foo/ and $f503a =~/bar/
     forceupdate :
      $f503b : mandatory b in condition's field
      f005_ : mandatory 005
      f006_ : \&return_record_encoding()
      f700 :
       a : the a subfield of this mandatory 700 field
       b : \&sub1("$f503a")
     forceupdatefirst :
      $f501b : update only the first b in condition's field 501
    -
     condition : $f501a =~/foo/
     execute : \&warnfoo("f501a contain foo")
    -
     subs : >
        sub return_record_encoding { $record->encoding(); }
        sub sub1 {my $string=shift;$string =~ s/a/e/g;return $string;}
        sub warnfoo { my $string = shift;warn $string; }
    ---
    -
     condition : $f501b2 eq "o"
     update :
      c : updated value of all c in condition's field
      f504a : updated value of all 504a if exists
      f604 :
       b : \&LUT("$this")
       c : \&LUT("NY","cities")
     updatefirst :
      f604a : update only the first a in 604
    -
     condition : $f501c eq "1"
     delete : $f501
    -
     LUT :
       1 : first
       2 : second
       bar : openbar
    ---
    delete :
     - f401a
     - f005
    ---
    condition : $ldr2 eq "t"
    execute : \&SetRecordToLowerCase($record)
    ---
    condition : $f008_ eq "controlfield_content8b"
    duplicatefield :
     - $f008 > f007
     - f402 > f602
    delete : f402
    ---
    global_subs: >
        sub reencodeRecordtoUtf8 {
            $record->encoding( 'UTF-8' );
        }
        sub warnfee {
            my $string = shift;warn $string;
        }
    global_LUT:
     cities:
      NY : New York
      SF : San Fransisco
     numbers:
      1 : one
      2 : two

résultat (avec C<< $record->as_formatted >> ) :

    --notice d'origine--
    LDR optional leader
    005     controlfield_content
    008     controlfield_content8a
    008     controlfield_content8b
    106    _aVaLuE
    401    _aafooa
    402  2 _aa402a2
    402 1  _aa402a1
    501    _c1
    501    _afoo
           _afoao
           _b1
           _bbaoar
           _cbig
    503    _afee
           _ababar
    504    _azut
           _asisi
    604    _afoo
           _afoo
           _bbar
           _ctruc
    
    --notice transformée--
    LDR optional leader
    006     UTF-8
    007     controlfield_content8b
    008     controlfield_content8a
    008     controlfield_content8b
    106    _aVaLuE
    401    _bnew value of the 401 condition's field
    501    _c1
    501    _afoo
           _afoao
           _bupdate only the first b in condition's field 501
           _bbaoar
           _cupdated value of all c in condition's field
    502    _athis is the value of a subfield of a new 502 field
    503    _afee
           _ababar
           _bmandatory b in condition's field
    504    _aupdated value of all 504a if exists
           _aupdated value of all 504a if exists
    600    _aa string
           _afirst a subfield of this new 600 field
           _asecond a subfield of this new 600 field
           _bthe 600b value
    602 1  _aa402a1
    602  2 _aa402a2
    604    _aupdate only the first a in 604
           _afoo
           _bopenbar
           _cNew York
    700    _athe a subfield of this mandatory 700 field
           _bbeber

=back

=head1 - VOIR AUSSI

=over 4

=item * MARC::Record (L<http://search.cpan.org/perldoc?MARC::Record>)

=item * MARC::Field (L<http://search.cpan.org/perldoc?MARC::Field>)

=item * MARC::Loader (L<http://search.cpan.org/perldoc?MARC::Loader>)

=item * YAML (L<http://search.cpan.org/perldoc?YAML>)

=item * Library Of Congress MARC pages (L<http://www.loc.gov/marc/>)

The definitive source for all things MARC.

=back

=head1 - AUTEUR

Stéphane Delaune, (delaune.stephane at gmail.com)

=head1 - COPYRIGHT

Copyright 2011-2018 Stephane Delaune for Biblibre.com, all rights reserved.

This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=cut