summaryrefslogtreecommitdiffstats
path: root/upstream/archlinux/man3/Text::Balanced.3perl
blob: e79a987f8e19dacedfae8a483ec36b3627c977d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
.\" -*- mode: troff; coding: utf-8 -*-
.\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
.ie n \{\
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds C`
.    ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
.    if \nF \{\
.        de IX
.        tm Index:\\$1\t\\n%\t"\\$2"
..
.        if !\nF==2 \{\
.            nr % 0
.            nr F 2
.        \}
.    \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "Text::Balanced 3perl"
.TH Text::Balanced 3perl 2024-02-11 "perl v5.38.2" "Perl Programmers Reference Guide"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH NAME
Text::Balanced \- Extract delimited text sequences from strings.
.SH SYNOPSIS
.IX Header "SYNOPSIS"
.Vb 11
\&    use Text::Balanced qw (
\&        extract_delimited
\&        extract_bracketed
\&        extract_quotelike
\&        extract_codeblock
\&        extract_variable
\&        extract_tagged
\&        extract_multiple
\&        gen_delimited_pat
\&        gen_extract_tagged
\&    );
\&
\&    # Extract the initial substring of $text that is delimited by
\&    # two (unescaped) instances of the first character in $delim.
\&
\&    ($extracted, $remainder) = extract_delimited($text,$delim);
\&
\&    # Extract the initial substring of $text that is bracketed
\&    # with a delimiter(s) specified by $delim (where the string
\&    # in $delim contains one or more of \*(Aq(){}[]<>\*(Aq).
\&
\&    ($extracted, $remainder) = extract_bracketed($text,$delim);
\&
\&    # Extract the initial substring of $text that is bounded by
\&    # an XML tag.
\&
\&    ($extracted, $remainder) = extract_tagged($text);
\&
\&    # Extract the initial substring of $text that is bounded by
\&    # a C<BEGIN>...C<END> pair. Don\*(Aqt allow nested C<BEGIN> tags
\&
\&    ($extracted, $remainder) =
\&        extract_tagged($text,"BEGIN","END",undef,{bad=>["BEGIN"]});
\&
\&    # Extract the initial substring of $text that represents a
\&    # Perl "quote or quote\-like operation"
\&
\&    ($extracted, $remainder) = extract_quotelike($text);
\&
\&    # Extract the initial substring of $text that represents a block
\&    # of Perl code, bracketed by any of character(s) specified by $delim
\&    # (where the string $delim contains one or more of \*(Aq(){}[]<>\*(Aq).
\&
\&    ($extracted, $remainder) = extract_codeblock($text,$delim);
\&
\&    # Extract the initial substrings of $text that would be extracted by
\&    # one or more sequential applications of the specified functions
\&    # or regular expressions
\&
\&    @extracted = extract_multiple($text,
\&                                  [ \e&extract_bracketed,
\&                                    \e&extract_quotelike,
\&                                    \e&some_other_extractor_sub,
\&                                    qr/[xyz]*/,
\&                                    \*(Aqliteral\*(Aq,
\&                                  ]);
\&
\&    # Create a string representing an optimized pattern (a la Friedl)
\&    # that matches a substring delimited by any of the specified characters
\&    # (in this case: any type of quote or a slash)
\&
\&    $patstring = gen_delimited_pat(q{\*(Aq"\`/});
\&
\&    # Generate a reference to an anonymous sub that is just like extract_tagged
\&    # but pre\-compiled and optimized for a specific pair of tags, and
\&    # consequently much faster (i.e. 3 times faster). It uses qr// for better
\&    # performance on repeated calls.
\&
\&    $extract_head = gen_extract_tagged(\*(Aq<HEAD>\*(Aq,\*(Aq</HEAD>\*(Aq);
\&    ($extracted, $remainder) = $extract_head\->($text);
.Ve
.SH DESCRIPTION
.IX Header "DESCRIPTION"
The various \f(CW\*(C`extract_...\*(C'\fR subroutines may be used to
extract a delimited substring, possibly after skipping a
specified prefix string. By default, that prefix is
optional whitespace (\f(CW\*(C`/\es*/\*(C'\fR), but you can change it to whatever
you wish (see below).
.PP
The substring to be extracted must appear at the
current \f(CW\*(C`pos\*(C'\fR location of the string's variable
(or at index zero, if no \f(CW\*(C`pos\*(C'\fR position is defined).
In other words, the \f(CW\*(C`extract_...\*(C'\fR subroutines \fIdon't\fR
extract the first occurrence of a substring anywhere
in a string (like an unanchored regex would). Rather,
they extract an occurrence of the substring appearing
immediately at the current matching position in the
string (like a \f(CW\*(C`\eG\*(C'\fR\-anchored regex would).
.SS "General Behaviour in List Contexts"
.IX Subsection "General Behaviour in List Contexts"
In a list context, all the subroutines return a list, the first three
elements of which are always:
.IP [0] 4
.IX Item "[0]"
The extracted string, including the specified delimiters.
If the extraction fails \f(CW\*(C`undef\*(C'\fR is returned.
.IP [1] 4
.IX Item "[1]"
The remainder of the input string (i.e. the characters after the
extracted string). On failure, the entire string is returned.
.IP [2] 4
.IX Item "[2]"
The skipped prefix (i.e. the characters before the extracted string).
On failure, \f(CW\*(C`undef\*(C'\fR is returned.
.PP
Note that in a list context, the contents of the original input text (the first
argument) are not modified in any way.
.PP
However, if the input text was passed in a variable, that variable's
\&\f(CW\*(C`pos\*(C'\fR value is updated to point at the first character after the
extracted text. That means that in a list context the various
subroutines can be used much like regular expressions. For example:
.PP
.Vb 4
\&    while ( $next = (extract_quotelike($text))[0] )
\&    {
\&        # process next quote\-like (in $next)
\&    }
.Ve
.SS "General Behaviour in Scalar and Void Contexts"
.IX Subsection "General Behaviour in Scalar and Void Contexts"
In a scalar context, the extracted string is returned, having first been
removed from the input text. Thus, the following code also processes
each quote-like operation, but actually removes them from \f(CW$text:\fR
.PP
.Vb 4
\&    while ( $next = extract_quotelike($text) )
\&    {
\&        # process next quote\-like (in $next)
\&    }
.Ve
.PP
Note that if the input text is a read-only string (i.e. a literal),
no attempt is made to remove the extracted text.
.PP
In a void context the behaviour of the extraction subroutines is
exactly the same as in a scalar context, except (of course) that the
extracted substring is not returned.
.SS "A Note About Prefixes"
.IX Subsection "A Note About Prefixes"
Prefix patterns are matched without any trailing modifiers (\f(CW\*(C`/gimsox\*(C'\fR etc.)
This can bite you if you're expecting a prefix specification like
\&'.*?(?=<H1>)' to skip everything up to the first <H1> tag. Such a prefix
pattern will only succeed if the <H1> tag is on the current line, since
\&. normally doesn't match newlines.
.PP
To overcome this limitation, you need to turn on /s matching within
the prefix pattern, using the \f(CW\*(C`(?s)\*(C'\fR directive: '(?s).*?(?=<H1>)'
.SS Functions
.IX Subsection "Functions"
.ie n .IP """extract_delimited""" 4
.el .IP \f(CWextract_delimited\fR 4
.IX Item "extract_delimited"
The \f(CW\*(C`extract_delimited\*(C'\fR function formalizes the common idiom
of extracting a single-character-delimited substring from the start of
a string. For example, to extract a single-quote delimited string, the
following code is typically used:
.Sp
.Vb 2
\&    ($remainder = $text) =~ s/\eA(\*(Aq(\e\e.|[^\*(Aq])*\*(Aq)//s;
\&    $extracted = $1;
.Ve
.Sp
but with \f(CW\*(C`extract_delimited\*(C'\fR it can be simplified to:
.Sp
.Vb 1
\&    ($extracted,$remainder) = extract_delimited($text, "\*(Aq");
.Ve
.Sp
\&\f(CW\*(C`extract_delimited\*(C'\fR takes up to four scalars (the input text, the
delimiters, a prefix pattern to be skipped, and any escape characters)
and extracts the initial substring of the text that
is appropriately delimited. If the delimiter string has multiple
characters, the first one encountered in the text is taken to delimit
the substring.
The third argument specifies a prefix pattern that is to be skipped
(but must be present!) before the substring is extracted.
The final argument specifies the escape character to be used for each
delimiter.
.Sp
All arguments are optional. If the escape characters are not specified,
every delimiter is escaped with a backslash (\f(CW\*(C`\e\*(C'\fR).
If the prefix is not specified, the
pattern \f(CW\*(Aq\es*\*(Aq\fR \- optional whitespace \- is used. If the delimiter set
is also not specified, the set \f(CW\*(C`/["\*(Aq\`]/\*(C'\fR is used. If the text to be processed
is not specified either, \f(CW$_\fR is used.
.Sp
In list context, \f(CW\*(C`extract_delimited\*(C'\fR returns a array of three
elements, the extracted substring (\fIincluding the surrounding
delimiters\fR), the remainder of the text, and the skipped prefix (if
any). If a suitable delimited substring is not found, the first
element of the array is the empty string, the second is the complete
original text, and the prefix returned in the third element is an
empty string.
.Sp
In a scalar context, just the extracted substring is returned. In
a void context, the extracted substring (and any prefix) are simply
removed from the beginning of the first argument.
.Sp
Examples:
.Sp
.Vb 1
\&    # Remove a single\-quoted substring from the very beginning of $text:
\&
\&        $substring = extract_delimited($text, "\*(Aq", \*(Aq\*(Aq);
\&
\&    # Remove a single\-quoted Pascalish substring (i.e. one in which
\&    # doubling the quote character escapes it) from the very
\&    # beginning of $text:
\&
\&        $substring = extract_delimited($text, "\*(Aq", \*(Aq\*(Aq, "\*(Aq");
\&
\&    # Extract a single\- or double\- quoted substring from the
\&    # beginning of $text, optionally after some whitespace
\&    # (note the list context to protect $text from modification):
\&
\&        ($substring) = extract_delimited $text, q{"\*(Aq};
\&
\&    # Delete the substring delimited by the first \*(Aq/\*(Aq in $text:
\&
\&        $text = join \*(Aq\*(Aq, (extract_delimited($text,\*(Aq/\*(Aq,\*(Aq[^/]*\*(Aq)[2,1];
.Ve
.Sp
Note that this last example is \fInot\fR the same as deleting the first
quote-like pattern. For instance, if \f(CW$text\fR contained the string:
.Sp
.Vb 1
\&    "if (\*(Aq./cmd\*(Aq =~ m/$UNIXCMD/s) { $cmd = $1; }"
.Ve
.Sp
then after the deletion it would contain:
.Sp
.Vb 1
\&    "if (\*(Aq.$UNIXCMD/s) { $cmd = $1; }"
.Ve
.Sp
not:
.Sp
.Vb 1
\&    "if (\*(Aq./cmd\*(Aq =~ ms) { $cmd = $1; }"
.Ve
.Sp
See "extract_quotelike" for a (partial) solution to this problem.
.ie n .IP """extract_bracketed""" 4
.el .IP \f(CWextract_bracketed\fR 4
.IX Item "extract_bracketed"
Like \f(CW"extract_delimited"\fR, the \f(CW\*(C`extract_bracketed\*(C'\fR function takes
up to three optional scalar arguments: a string to extract from, a delimiter
specifier, and a prefix pattern. As before, a missing prefix defaults to
optional whitespace and a missing text defaults to \f(CW$_\fR. However, a missing
delimiter specifier defaults to \f(CW\*(Aq{}()[]<>\*(Aq\fR (see below).
.Sp
\&\f(CW\*(C`extract_bracketed\*(C'\fR extracts a balanced-bracket-delimited
substring (using any one (or more) of the user-specified delimiter
brackets: '(..)', '{..}', '[..]', or '<..>'). Optionally it will also
respect quoted unbalanced brackets (see below).
.Sp
A "delimiter bracket" is a bracket in list of delimiters passed as
\&\f(CW\*(C`extract_bracketed\*(C'\fR's second argument. Delimiter brackets are
specified by giving either the left or right (or both!) versions
of the required bracket(s). Note that the order in which
two or more delimiter brackets are specified is not significant.
.Sp
A "balanced-bracket-delimited substring" is a substring bounded by
matched brackets, such that any other (left or right) delimiter
bracket \fIwithin\fR the substring is also matched by an opposite
(right or left) delimiter bracket \fIat the same level of nesting\fR. Any
type of bracket not in the delimiter list is treated as an ordinary
character.
.Sp
In other words, each type of bracket specified as a delimiter must be
balanced and correctly nested within the substring, and any other kind of
("non-delimiter") bracket in the substring is ignored.
.Sp
For example, given the string:
.Sp
.Vb 1
\&    $text = "{ an \*(Aq[irregularly :\-(] {} parenthesized >:\-)\*(Aq string }";
.Ve
.Sp
then a call to \f(CW\*(C`extract_bracketed\*(C'\fR in a list context:
.Sp
.Vb 1
\&    @result = extract_bracketed( $text, \*(Aq{}\*(Aq );
.Ve
.Sp
would return:
.Sp
.Vb 1
\&    ( "{ an \*(Aq[irregularly :\-(] {} parenthesized >:\-)\*(Aq string }" , "" , "" )
.Ve
.Sp
since both sets of \f(CW\*(Aq{..}\*(Aq\fR brackets are properly nested and evenly balanced.
(In a scalar context just the first element of the array would be returned. In
a void context, \f(CW$text\fR would be replaced by an empty string.)
.Sp
Likewise the call in:
.Sp
.Vb 1
\&    @result = extract_bracketed( $text, \*(Aq{[\*(Aq );
.Ve
.Sp
would return the same result, since all sets of both types of specified
delimiter brackets are correctly nested and balanced.
.Sp
However, the call in:
.Sp
.Vb 1
\&    @result = extract_bracketed( $text, \*(Aq{([<\*(Aq );
.Ve
.Sp
would fail, returning:
.Sp
.Vb 1
\&    ( undef , "{ an \*(Aq[irregularly :\-(] {} parenthesized >:\-)\*(Aq string }"  );
.Ve
.Sp
because the embedded pairs of \f(CW\*(Aq(..)\*(Aq\fRs and \f(CW\*(Aq[..]\*(Aq\fRs are "cross-nested" and
the embedded \f(CW\*(Aq>\*(Aq\fR is unbalanced. (In a scalar context, this call would
return an empty string. In a void context, \f(CW$text\fR would be unchanged.)
.Sp
Note that the embedded single-quotes in the string don't help in this
case, since they have not been specified as acceptable delimiters and are
therefore treated as non-delimiter characters (and ignored).
.Sp
However, if a particular species of quote character is included in the
delimiter specification, then that type of quote will be correctly handled.
for example, if \f(CW$text\fR is:
.Sp
.Vb 1
\&    $text = \*(Aq<A HREF=">>>>">link</A>\*(Aq;
.Ve
.Sp
then
.Sp
.Vb 1
\&    @result = extract_bracketed( $text, \*(Aq<">\*(Aq );
.Ve
.Sp
returns:
.Sp
.Vb 1
\&    ( \*(Aq<A HREF=">>>>">\*(Aq, \*(Aqlink</A>\*(Aq, "" )
.Ve
.Sp
as expected. Without the specification of \f(CW\*(C`"\*(C'\fR as an embedded quoter:
.Sp
.Vb 1
\&    @result = extract_bracketed( $text, \*(Aq<>\*(Aq );
.Ve
.Sp
the result would be:
.Sp
.Vb 1
\&    ( \*(Aq<A HREF=">\*(Aq, \*(Aq>>>">link</A>\*(Aq, "" )
.Ve
.Sp
In addition to the quote delimiters \f(CW\*(C`\*(Aq\*(C'\fR, \f(CW\*(C`"\*(C'\fR, and \f(CW\*(C`\`\*(C'\fR, full Perl quote-like
quoting (i.e. q{string}, qq{string}, etc) can be specified by including the
letter 'q' as a delimiter. Hence:
.Sp
.Vb 1
\&    @result = extract_bracketed( $text, \*(Aq<q>\*(Aq );
.Ve
.Sp
would correctly match something like this:
.Sp
.Vb 1
\&    $text = \*(Aq<leftop: conj /and/ conj>\*(Aq;
.Ve
.Sp
See also: \f(CW"extract_quotelike"\fR and \f(CW"extract_codeblock"\fR.
.ie n .IP """extract_variable""" 4
.el .IP \f(CWextract_variable\fR 4
.IX Item "extract_variable"
\&\f(CW\*(C`extract_variable\*(C'\fR extracts any valid Perl variable or
variable-involved expression, including scalars, arrays, hashes, array
accesses, hash look-ups, method calls through objects, subroutine calls
through subroutine references, etc.
.Sp
The subroutine takes up to two optional arguments:
.RS 4
.IP 1. 4
A string to be processed (\f(CW$_\fR if the string is omitted or \f(CW\*(C`undef\*(C'\fR)
.IP 2. 4
A string specifying a pattern to be matched as a prefix (which is to be
skipped). If omitted, optional whitespace is skipped.
.RE
.RS 4
.Sp
On success in a list context, an array of 3 elements is returned. The
elements are:
.IP [0] 4
.IX Item "[0]"
the extracted variable, or variablish expression
.IP [1] 4
.IX Item "[1]"
the remainder of the input text,
.IP [2] 4
.IX Item "[2]"
the prefix substring (if any),
.RE
.RS 4
.Sp
On failure, all of these values (except the remaining text) are \f(CW\*(C`undef\*(C'\fR.
.Sp
In a scalar context, \f(CW\*(C`extract_variable\*(C'\fR returns just the complete
substring that matched a variablish expression. \f(CW\*(C`undef\*(C'\fR is returned on
failure. In addition, the original input text has the returned substring
(and any prefix) removed from it.
.Sp
In a void context, the input text just has the matched substring (and
any specified prefix) removed.
.RE
.ie n .IP """extract_tagged""" 4
.el .IP \f(CWextract_tagged\fR 4
.IX Item "extract_tagged"
\&\f(CW\*(C`extract_tagged\*(C'\fR extracts and segments text between (balanced)
specified tags.
.Sp
The subroutine takes up to five optional arguments:
.RS 4
.IP 1. 4
A string to be processed (\f(CW$_\fR if the string is omitted or \f(CW\*(C`undef\*(C'\fR)
.IP 2. 4
A string specifying a pattern (i.e. regex) to be matched as the opening tag.
If the pattern string is omitted (or \f(CW\*(C`undef\*(C'\fR) then a pattern
that matches any standard XML tag is used.
.IP 3. 4
A string specifying a pattern to be matched at the closing tag.
If the pattern string is omitted (or \f(CW\*(C`undef\*(C'\fR) then the closing
tag is constructed by inserting a \f(CW\*(C`/\*(C'\fR after any leading bracket
characters in the actual opening tag that was matched (\fInot\fR the pattern
that matched the tag). For example, if the opening tag pattern
is specified as \f(CW\*(Aq{{\ew+}}\*(Aq\fR and actually matched the opening tag
\&\f(CW"{{DATA}}"\fR, then the constructed closing tag would be \f(CW"{{/DATA}}"\fR.
.IP 4. 4
A string specifying a pattern to be matched as a prefix (which is to be
skipped). If omitted, optional whitespace is skipped.
.IP 5. 4
A hash reference containing various parsing options (see below)
.RE
.RS 4
.Sp
The various options that can be specified are:
.ie n .IP """reject => $listref""" 4
.el .IP "\f(CWreject => $listref\fR" 4
.IX Item "reject => $listref"
The list reference contains one or more strings specifying patterns
that must \fInot\fR appear within the tagged text.
.Sp
For example, to extract
an HTML link (which should not contain nested links) use:
.Sp
.Vb 1
\&        extract_tagged($text, \*(Aq<A>\*(Aq, \*(Aq</A>\*(Aq, undef, {reject => [\*(Aq<A>\*(Aq]} );
.Ve
.ie n .IP """ignore => $listref""" 4
.el .IP "\f(CWignore => $listref\fR" 4
.IX Item "ignore => $listref"
The list reference contains one or more strings specifying patterns
that are \fInot\fR to be treated as nested tags within the tagged text
(even if they would match the start tag pattern).
.Sp
For example, to extract an arbitrary XML tag, but ignore "empty" elements:
.Sp
.Vb 1
\&        extract_tagged($text, undef, undef, undef, {ignore => [\*(Aq<[^>]*/>\*(Aq]} );
.Ve
.Sp
(also see "gen_delimited_pat" below).
.ie n .IP """fail => $str""" 4
.el .IP "\f(CWfail => $str\fR" 4
.IX Item "fail => $str"
The \f(CW\*(C`fail\*(C'\fR option indicates the action to be taken if a matching end
tag is not encountered (i.e. before the end of the string or some
\&\f(CW\*(C`reject\*(C'\fR pattern matches). By default, a failure to match a closing
tag causes \f(CW\*(C`extract_tagged\*(C'\fR to immediately fail.
.Sp
However, if the string value associated with <reject> is "MAX", then
\&\f(CW\*(C`extract_tagged\*(C'\fR returns the complete text up to the point of failure.
If the string is "PARA", \f(CW\*(C`extract_tagged\*(C'\fR returns only the first paragraph
after the tag (up to the first line that is either empty or contains
only whitespace characters).
If the string is "", the default behaviour (i.e. failure) is reinstated.
.Sp
For example, suppose the start tag "/para" introduces a paragraph, which then
continues until the next "/endpara" tag or until another "/para" tag is
encountered:
.Sp
.Vb 1
\&        $text = "/para line 1\en\enline 3\en/para line 4";
\&
\&        extract_tagged($text, \*(Aq/para\*(Aq, \*(Aq/endpara\*(Aq, undef,
\&                                {reject => \*(Aq/para\*(Aq, fail => MAX );
\&
\&        # EXTRACTED: "/para line 1\en\enline 3\en"
.Ve
.Sp
Suppose instead, that if no matching "/endpara" tag is found, the "/para"
tag refers only to the immediately following paragraph:
.Sp
.Vb 1
\&        $text = "/para line 1\en\enline 3\en/para line 4";
\&
\&        extract_tagged($text, \*(Aq/para\*(Aq, \*(Aq/endpara\*(Aq, undef,
\&                        {reject => \*(Aq/para\*(Aq, fail => MAX );
\&
\&        # EXTRACTED: "/para line 1\en"
.Ve
.Sp
Note that the specified \f(CW\*(C`fail\*(C'\fR behaviour applies to nested tags as well.
.RE
.RS 4
.Sp
On success in a list context, an array of 6 elements is returned. The elements are:
.IP [0] 4
.IX Item "[0]"
the extracted tagged substring (including the outermost tags),
.IP [1] 4
.IX Item "[1]"
the remainder of the input text,
.IP [2] 4
.IX Item "[2]"
the prefix substring (if any),
.IP [3] 4
.IX Item "[3]"
the opening tag
.IP [4] 4
.IX Item "[4]"
the text between the opening and closing tags
.IP [5] 4
.IX Item "[5]"
the closing tag (or "" if no closing tag was found)
.RE
.RS 4
.Sp
On failure, all of these values (except the remaining text) are \f(CW\*(C`undef\*(C'\fR.
.Sp
In a scalar context, \f(CW\*(C`extract_tagged\*(C'\fR returns just the complete
substring that matched a tagged text (including the start and end
tags). \f(CW\*(C`undef\*(C'\fR is returned on failure. In addition, the original input
text has the returned substring (and any prefix) removed from it.
.Sp
In a void context, the input text just has the matched substring (and
any specified prefix) removed.
.RE
.ie n .IP """gen_extract_tagged""" 4
.el .IP \f(CWgen_extract_tagged\fR 4
.IX Item "gen_extract_tagged"
\&\f(CW\*(C`gen_extract_tagged\*(C'\fR generates a new anonymous subroutine which
extracts text between (balanced) specified tags. In other words,
it generates a function identical in function to \f(CW\*(C`extract_tagged\*(C'\fR.
.Sp
The difference between \f(CW\*(C`extract_tagged\*(C'\fR and the anonymous
subroutines generated by
\&\f(CW\*(C`gen_extract_tagged\*(C'\fR, is that those generated subroutines:
.RS 4
.IP \(bu 4
do not have to reparse tag specification or parsing options every time
they are called (whereas \f(CW\*(C`extract_tagged\*(C'\fR has to effectively rebuild
its tag parser on every call);
.IP \(bu 4
make use of the new qr// construct to pre-compile the regexes they use
(whereas \f(CW\*(C`extract_tagged\*(C'\fR uses standard string variable interpolation
to create tag-matching patterns).
.RE
.RS 4
.Sp
The subroutine takes up to four optional arguments (the same set as
\&\f(CW\*(C`extract_tagged\*(C'\fR except for the string to be processed). It returns
a reference to a subroutine which in turn takes a single argument (the text to
be extracted from).
.Sp
In other words, the implementation of \f(CW\*(C`extract_tagged\*(C'\fR is exactly
equivalent to:
.Sp
.Vb 6
\&        sub extract_tagged
\&        {
\&                my $text = shift;
\&                $extractor = gen_extract_tagged(@_);
\&                return $extractor\->($text);
\&        }
.Ve
.Sp
(although \f(CW\*(C`extract_tagged\*(C'\fR is not currently implemented that way).
.Sp
Using \f(CW\*(C`gen_extract_tagged\*(C'\fR to create extraction functions for specific tags
is a good idea if those functions are going to be called more than once, since
their performance is typically twice as good as the more general-purpose
\&\f(CW\*(C`extract_tagged\*(C'\fR.
.RE
.ie n .IP """extract_quotelike""" 4
.el .IP \f(CWextract_quotelike\fR 4
.IX Item "extract_quotelike"
\&\f(CW\*(C`extract_quotelike\*(C'\fR attempts to recognize, extract, and segment any
one of the various Perl quotes and quotelike operators (see
\&\fBperlop\fR\|(3)) Nested backslashed delimiters, embedded balanced bracket
delimiters (for the quotelike operators), and trailing modifiers are
all caught. For example, in:
.Sp
.Vb 1
\&        extract_quotelike \*(Aqq # an octothorpe: \e# (not the end of the q!) #\*(Aq
\&
\&        extract_quotelike \*(Aq  "You said, \e"Use sed\e"."  \*(Aq
\&
\&        extract_quotelike \*(Aq s{([A\-Z]{1,8}\e.[A\-Z]{3})} /\eL$1\eE/; \*(Aq
\&
\&        extract_quotelike \*(Aq tr/\e\e\e/\e\e\e\e/\e\e\e//ds; \*(Aq
.Ve
.Sp
the full Perl quotelike operations are all extracted correctly.
.Sp
Note too that, when using the /x modifier on a regex, any comment
containing the current pattern delimiter will cause the regex to be
immediately terminated. In other words:
.Sp
.Vb 5
\&        \*(Aqm /
\&                (?i)            # CASE INSENSITIVE
\&                [a\-z_]          # LEADING ALPHABETIC/UNDERSCORE
\&                [a\-z0\-9]*       # FOLLOWED BY ANY NUMBER OF ALPHANUMERICS
\&           /x\*(Aq
.Ve
.Sp
will be extracted as if it were:
.Sp
.Vb 3
\&        \*(Aqm /
\&                (?i)            # CASE INSENSITIVE
\&                [a\-z_]          # LEADING ALPHABETIC/\*(Aq
.Ve
.Sp
This behaviour is identical to that of the actual compiler.
.Sp
\&\f(CW\*(C`extract_quotelike\*(C'\fR takes two arguments: the text to be processed and
a prefix to be matched at the very beginning of the text. If no prefix
is specified, optional whitespace is the default. If no text is given,
\&\f(CW$_\fR is used.
.Sp
In a list context, an array of 11 elements is returned. The elements are:
.RS 4
.IP [0] 4
.IX Item "[0]"
the extracted quotelike substring (including trailing modifiers),
.IP [1] 4
.IX Item "[1]"
the remainder of the input text,
.IP [2] 4
.IX Item "[2]"
the prefix substring (if any),
.IP [3] 4
.IX Item "[3]"
the name of the quotelike operator (if any),
.IP [4] 4
.IX Item "[4]"
the left delimiter of the first block of the operation,
.IP [5] 4
.IX Item "[5]"
the text of the first block of the operation
(that is, the contents of
a quote, the regex of a match or substitution or the target list of a
translation),
.IP [6] 4
.IX Item "[6]"
the right delimiter of the first block of the operation,
.IP [7] 4
.IX Item "[7]"
the left delimiter of the second block of the operation
(that is, if it is a \f(CW\*(C`s\*(C'\fR, \f(CW\*(C`tr\*(C'\fR, or \f(CW\*(C`y\*(C'\fR),
.IP [8] 4
.IX Item "[8]"
the text of the second block of the operation
(that is, the replacement of a substitution or the translation list
of a translation),
.IP [9] 4
.IX Item "[9]"
the right delimiter of the second block of the operation (if any),
.IP [10] 4
.IX Item "[10]"
the trailing modifiers on the operation (if any).
.RE
.RS 4
.Sp
For each of the fields marked "(if any)" the default value on success is
an empty string.
On failure, all of these values (except the remaining text) are \f(CW\*(C`undef\*(C'\fR.
.Sp
In a scalar context, \f(CW\*(C`extract_quotelike\*(C'\fR returns just the complete substring
that matched a quotelike operation (or \f(CW\*(C`undef\*(C'\fR on failure). In a scalar or
void context, the input text has the same substring (and any specified
prefix) removed.
.Sp
Examples:
.Sp
.Vb 1
\&        # Remove the first quotelike literal that appears in text
\&
\&                $quotelike = extract_quotelike($text,\*(Aq.*?\*(Aq);
\&
\&        # Replace one or more leading whitespace\-separated quotelike
\&        # literals in $_ with "<QLL>"
\&
\&                do { $_ = join \*(Aq<QLL>\*(Aq, (extract_quotelike)[2,1] } until $@;
\&
\&
\&        # Isolate the search pattern in a quotelike operation from $text
\&
\&                ($op,$pat) = (extract_quotelike $text)[3,5];
\&                if ($op =~ /[ms]/)
\&                {
\&                        print "search pattern: $pat\en";
\&                }
\&                else
\&                {
\&                        print "$op is not a pattern matching operation\en";
\&                }
.Ve
.RE
.ie n .IP """extract_quotelike""" 4
.el .IP \f(CWextract_quotelike\fR 4
.IX Item "extract_quotelike"
\&\f(CW\*(C`extract_quotelike\*(C'\fR can successfully extract "here documents" from an input
string, but with an important caveat in list contexts.
.Sp
Unlike other types of quote-like literals, a here document is rarely
a contiguous substring. For example, a typical piece of code using
here document might look like this:
.Sp
.Vb 4
\&        <<\*(AqEOMSG\*(Aq || die;
\&        This is the message.
\&        EOMSG
\&        exit;
.Ve
.Sp
Given this as an input string in a scalar context, \f(CW\*(C`extract_quotelike\*(C'\fR
would correctly return the string "<<'EOMSG'\enThis is the message.\enEOMSG",
leaving the string " || die;\enexit;" in the original variable. In other words,
the two separate pieces of the here document are successfully extracted and
concatenated.
.Sp
In a list context, \f(CW\*(C`extract_quotelike\*(C'\fR would return the list
.RS 4
.IP [0] 4
.IX Item "[0]"
"<<'EOMSG'\enThis is the message.\enEOMSG\en" (i.e. the full extracted here document,
including fore and aft delimiters),
.IP [1] 4
.IX Item "[1]"
" || die;\enexit;" (i.e. the remainder of the input text, concatenated),
.IP [2] 4
.IX Item "[2]"
"" (i.e. the prefix substring \-\- trivial in this case),
.IP [3] 4
.IX Item "[3]"
"<<" (i.e. the "name" of the quotelike operator)
.IP [4] 4
.IX Item "[4]"
"'EOMSG'" (i.e. the left delimiter of the here document, including any quotes),
.IP [5] 4
.IX Item "[5]"
"This is the message.\en" (i.e. the text of the here document),
.IP [6] 4
.IX Item "[6]"
"EOMSG" (i.e. the right delimiter of the here document),
.IP [7..10] 4
.IX Item "[7..10]"
"" (a here document has no second left delimiter, second text, second right
delimiter, or trailing modifiers).
.RE
.RS 4
.Sp
However, the matching position of the input variable would be set to
"exit;" (i.e. \fIafter\fR the closing delimiter of the here document),
which would cause the earlier " || die;\enexit;" to be skipped in any
sequence of code fragment extractions.
.Sp
To avoid this problem, when it encounters a here document whilst
extracting from a modifiable string, \f(CW\*(C`extract_quotelike\*(C'\fR silently
rearranges the string to an equivalent piece of Perl:
.Sp
.Vb 5
\&        <<\*(AqEOMSG\*(Aq
\&        This is the message.
\&        EOMSG
\&        || die;
\&        exit;
.Ve
.Sp
in which the here document \fIis\fR contiguous. It still leaves the
matching position after the here document, but now the rest of the line
on which the here document starts is not skipped.
.Sp
To prevent <extract_quotelike> from mucking about with the input in this way
(this is the only case where a list-context \f(CW\*(C`extract_quotelike\*(C'\fR does so),
you can pass the input variable as an interpolated literal:
.Sp
.Vb 1
\&        $quotelike = extract_quotelike("$var");
.Ve
.RE
.ie n .IP """extract_codeblock""" 4
.el .IP \f(CWextract_codeblock\fR 4
.IX Item "extract_codeblock"
\&\f(CW\*(C`extract_codeblock\*(C'\fR attempts to recognize and extract a balanced
bracket delimited substring that may contain unbalanced brackets
inside Perl quotes or quotelike operations. That is, \f(CW\*(C`extract_codeblock\*(C'\fR
is like a combination of \f(CW"extract_bracketed"\fR and
\&\f(CW"extract_quotelike"\fR.
.Sp
\&\f(CW\*(C`extract_codeblock\*(C'\fR takes the same initial three parameters as \f(CW\*(C`extract_bracketed\*(C'\fR:
a text to process, a set of delimiter brackets to look for, and a prefix to
match first. It also takes an optional fourth parameter, which allows the
outermost delimiter brackets to be specified separately (see below),
and a fifth parameter used only by Parse::RecDescent.
.Sp
Omitting the first argument (input text) means process \f(CW$_\fR instead.
Omitting the second argument (delimiter brackets) indicates that only \f(CW\*(Aq{\*(Aq\fR is to be used.
Omitting the third argument (prefix argument) implies optional whitespace at the start.
Omitting the fourth argument (outermost delimiter brackets) indicates that the
value of the second argument is to be used for the outermost delimiters.
.Sp
Once the prefix and the outermost opening delimiter bracket have been
recognized, code blocks are extracted by stepping through the input text and
trying the following alternatives in sequence:
.RS 4
.IP 1. 4
Try and match a closing delimiter bracket. If the bracket was the same
species as the last opening bracket, return the substring to that
point. If the bracket was mismatched, return an error.
.IP 2. 4
Try to match a quote or quotelike operator. If found, call
\&\f(CW\*(C`extract_quotelike\*(C'\fR to eat it. If \f(CW\*(C`extract_quotelike\*(C'\fR fails, return
the error it returned. Otherwise go back to step 1.
.IP 3. 4
Try to match an opening delimiter bracket. If found, call
\&\f(CW\*(C`extract_codeblock\*(C'\fR recursively to eat the embedded block. If the
recursive call fails, return an error. Otherwise, go back to step 1.
.IP 4. 4
Unconditionally match a bareword or any other single character, and
then go back to step 1.
.RE
.RS 4
.Sp
Examples:
.Sp
.Vb 1
\&        # Find a while loop in the text
\&
\&                if ($text =~ s/.*?while\es*\e{/{/)
\&                {
\&                        $loop = "while " . extract_codeblock($text);
\&                }
\&
\&        # Remove the first round\-bracketed list (which may include
\&        # round\- or curly\-bracketed code blocks or quotelike operators)
\&
\&                extract_codeblock $text, "(){}", \*(Aq[^(]*\*(Aq;
.Ve
.Sp
The ability to specify a different outermost delimiter bracket is useful
in some circumstances. For example, in the Parse::RecDescent module,
parser actions which are to be performed only on a successful parse
are specified using a \f(CW\*(C`<defer:...>\*(C'\fR directive. For example:
.Sp
.Vb 2
\&        sentence: subject verb object
\&                        <defer: {$::theVerb = $item{verb}} >
.Ve
.Sp
Parse::RecDescent uses \f(CW\*(C`extract_codeblock($text, \*(Aq{}<>\*(Aq)\*(C'\fR to extract the code
within the \f(CW\*(C`<defer:...>\*(C'\fR directive, but there's a problem.
.Sp
A deferred action like this:
.Sp
.Vb 1
\&                        <defer: {if ($count>10) {$count\-\-}} >
.Ve
.Sp
will be incorrectly parsed as:
.Sp
.Vb 1
\&                        <defer: {if ($count>
.Ve
.Sp
because the "less than" operator is interpreted as a closing delimiter.
.Sp
But, by extracting the directive using
\&\f(CW\*(C`extract_codeblock($text,\ \*(Aq{}\*(Aq,\ undef,\ \*(Aq<>\*(Aq)\*(C'\fR
the '>' character is only treated as a delimited at the outermost
level of the code block, so the directive is parsed correctly.
.RE
.ie n .IP """extract_multiple""" 4
.el .IP \f(CWextract_multiple\fR 4
.IX Item "extract_multiple"
The \f(CW\*(C`extract_multiple\*(C'\fR subroutine takes a string to be processed and a
list of extractors (subroutines or regular expressions) to apply to that string.
.Sp
In an array context \f(CW\*(C`extract_multiple\*(C'\fR returns an array of substrings
of the original string, as extracted by the specified extractors.
In a scalar context, \f(CW\*(C`extract_multiple\*(C'\fR returns the first
substring successfully extracted from the original string. In both
scalar and void contexts the original string has the first successfully
extracted substring removed from it. In all contexts
\&\f(CW\*(C`extract_multiple\*(C'\fR starts at the current \f(CW\*(C`pos\*(C'\fR of the string, and
sets that \f(CW\*(C`pos\*(C'\fR appropriately after it matches.
.Sp
Hence, the aim of a call to \f(CW\*(C`extract_multiple\*(C'\fR in a list context
is to split the processed string into as many non-overlapping fields as
possible, by repeatedly applying each of the specified extractors
to the remainder of the string. Thus \f(CW\*(C`extract_multiple\*(C'\fR is
a generalized form of Perl's \f(CW\*(C`split\*(C'\fR subroutine.
.Sp
The subroutine takes up to four optional arguments:
.RS 4
.IP 1. 4
A string to be processed (\f(CW$_\fR if the string is omitted or \f(CW\*(C`undef\*(C'\fR)
.IP 2. 4
A reference to a list of subroutine references and/or qr// objects and/or
literal strings and/or hash references, specifying the extractors
to be used to split the string. If this argument is omitted (or
\&\f(CW\*(C`undef\*(C'\fR) the list:
.Sp
.Vb 5
\&        [
\&                sub { extract_variable($_[0], \*(Aq\*(Aq) },
\&                sub { extract_quotelike($_[0],\*(Aq\*(Aq) },
\&                sub { extract_codeblock($_[0],\*(Aq{}\*(Aq,\*(Aq\*(Aq) },
\&        ]
.Ve
.Sp
is used.
.IP 3. 4
An number specifying the maximum number of fields to return. If this
argument is omitted (or \f(CW\*(C`undef\*(C'\fR), split continues as long as possible.
.Sp
If the third argument is \fIN\fR, then extraction continues until \fIN\fR fields
have been successfully extracted, or until the string has been completely
processed.
.Sp
Note that in scalar and void contexts the value of this argument is
automatically reset to 1 (under \f(CW\*(C`\-w\*(C'\fR, a warning is issued if the argument
has to be reset).
.IP 4. 4
A value indicating whether unmatched substrings (see below) within the
text should be skipped or returned as fields. If the value is true,
such substrings are skipped. Otherwise, they are returned.
.RE
.RS 4
.Sp
The extraction process works by applying each extractor in
sequence to the text string.
.Sp
If the extractor is a subroutine it is called in a list context and is
expected to return a list of a single element, namely the extracted
text. It may optionally also return two further arguments: a string
representing the text left after extraction (like $' for a pattern
match), and a string representing any prefix skipped before the
extraction (like $` in a pattern match). Note that this is designed
to facilitate the use of other Text::Balanced subroutines with
\&\f(CW\*(C`extract_multiple\*(C'\fR. Note too that the value returned by an extractor
subroutine need not bear any relationship to the corresponding substring
of the original text (see examples below).
.Sp
If the extractor is a precompiled regular expression or a string,
it is matched against the text in a scalar context with a leading
\&'\eG' and the gc modifiers enabled. The extracted value is either
\&\f(CW$1\fR if that variable is defined after the match, or else the
complete match (i.e. $&).
.Sp
If the extractor is a hash reference, it must contain exactly one element.
The value of that element is one of the
above extractor types (subroutine reference, regular expression, or string).
The key of that element is the name of a class into which the successful
return value of the extractor will be blessed.
.Sp
If an extractor returns a defined value, that value is immediately
treated as the next extracted field and pushed onto the list of fields.
If the extractor was specified in a hash reference, the field is also
blessed into the appropriate class,
.Sp
If the extractor fails to match (in the case of a regex extractor), or returns an empty list or an undefined value (in the case of a subroutine extractor), it is
assumed to have failed to extract.
If none of the extractor subroutines succeeds, then one
character is extracted from the start of the text and the extraction
subroutines reapplied. Characters which are thus removed are accumulated and
eventually become the next field (unless the fourth argument is true, in which
case they are discarded).
.Sp
For example, the following extracts substrings that are valid Perl variables:
.Sp
.Vb 3
\&        @fields = extract_multiple($text,
\&                                   [ sub { extract_variable($_[0]) } ],
\&                                   undef, 1);
.Ve
.Sp
This example separates a text into fields which are quote delimited,
curly bracketed, and anything else. The delimited and bracketed
parts are also blessed to identify them (the "anything else" is unblessed):
.Sp
.Vb 5
\&        @fields = extract_multiple($text,
\&                   [
\&                        { Delim => sub { extract_delimited($_[0],q{\*(Aq"}) } },
\&                        { Brack => sub { extract_bracketed($_[0],\*(Aq{}\*(Aq) } },
\&                   ]);
.Ve
.Sp
This call extracts the next single substring that is a valid Perl quotelike
operator (and removes it from \f(CW$text\fR):
.Sp
.Vb 4
\&        $quotelike = extract_multiple($text,
\&                                      [
\&                                        sub { extract_quotelike($_[0]) },
\&                                      ], undef, 1);
.Ve
.Sp
Finally, here is yet another way to do comma-separated value parsing:
.Sp
.Vb 8
\&        $csv_text = "a,\*(Aqx b\*(Aq,c";
\&        @fields = extract_multiple($csv_text,
\&                                  [
\&                                        sub { extract_delimited($_[0],q{\*(Aq"}) },
\&                                        qr/([^,]+)/,
\&                                  ],
\&                                  undef,1);
\&        # @fields is now (\*(Aqa\*(Aq, "\*(Aqx b\*(Aq", \*(Aqc\*(Aq)
.Ve
.Sp
The list in the second argument means:
\&\fI"Try and extract a ' or " delimited string, otherwise extract anything up to a comma..."\fR.
The undef third argument means:
\&\fI"...as many times as possible..."\fR,
and the true value in the fourth argument means
\&\fI"...discarding anything else that appears (i.e. the commas)"\fR.
.Sp
If you wanted the commas preserved as separate fields (i.e. like split
does if your split pattern has capturing parentheses), you would
just make the last parameter undefined (or remove it).
.RE
.ie n .IP """gen_delimited_pat""" 4
.el .IP \f(CWgen_delimited_pat\fR 4
.IX Item "gen_delimited_pat"
The \f(CW\*(C`gen_delimited_pat\*(C'\fR subroutine takes a single (string) argument and
builds a Friedl-style optimized regex that matches a string delimited
by any one of the characters in the single argument. For example:
.Sp
.Vb 1
\&        gen_delimited_pat(q{\*(Aq"})
.Ve
.Sp
returns the regex:
.Sp
.Vb 1
\&        (?:\e"(?:\e\e\e"|(?!\e").)*\e"|\e\*(Aq(?:\e\e\e\*(Aq|(?!\e\*(Aq).)*\e\*(Aq)
.Ve
.Sp
Note that the specified delimiters are automatically quotemeta'd.
.Sp
A typical use of \f(CW\*(C`gen_delimited_pat\*(C'\fR would be to build special purpose tags
for \f(CW\*(C`extract_tagged\*(C'\fR. For example, to properly ignore "empty" XML elements
(which might contain quoted strings):
.Sp
.Vb 1
\&        my $empty_tag = \*(Aq<(\*(Aq . gen_delimited_pat(q{\*(Aq"}) . \*(Aq|.)+/>\*(Aq;
\&
\&        extract_tagged($text, undef, undef, undef, {ignore => [$empty_tag]} );
.Ve
.Sp
\&\f(CW\*(C`gen_delimited_pat\*(C'\fR may also be called with an optional second argument,
which specifies the "escape" character(s) to be used for each delimiter.
For example to match a Pascal-style string (where ' is the delimiter
and '' is a literal ' within the string):
.Sp
.Vb 1
\&        gen_delimited_pat(q{\*(Aq},q{\*(Aq});
.Ve
.Sp
Different escape characters can be specified for different delimiters.
For example, to specify that '/' is the escape for single quotes
and '%' is the escape for double quotes:
.Sp
.Vb 1
\&        gen_delimited_pat(q{\*(Aq"},q{/%});
.Ve
.Sp
If more delimiters than escape chars are specified, the last escape char
is used for the remaining delimiters.
If no escape char is specified for a given specified delimiter, '\e' is used.
.ie n .IP """delimited_pat""" 4
.el .IP \f(CWdelimited_pat\fR 4
.IX Item "delimited_pat"
Note that \f(CW\*(C`gen_delimited_pat\*(C'\fR was previously called \f(CW\*(C`delimited_pat\*(C'\fR.
That name may still be used, but is now deprecated.
.SH DIAGNOSTICS
.IX Header "DIAGNOSTICS"
In a list context, all the functions return \f(CW\*(C`(undef,$original_text)\*(C'\fR
on failure. In a scalar context, failure is indicated by returning \f(CW\*(C`undef\*(C'\fR
(in this case the input text is not modified in any way).
.PP
In addition, on failure in \fIany\fR context, the \f(CW$@\fR variable is set.
Accessing \f(CW\*(C`$@\->{error}\*(C'\fR returns one of the error diagnostics listed
below.
Accessing \f(CW\*(C`$@\->{pos}\*(C'\fR returns the offset into the original string at
which the error was detected (although not necessarily where it occurred!)
Printing \f(CW$@\fR directly produces the error message, with the offset appended.
On success, the \f(CW$@\fR variable is guaranteed to be \f(CW\*(C`undef\*(C'\fR.
.PP
The available diagnostics are:
.ie n .IP """Did not find a suitable bracket: ""%s""""" 4
.el .IP "\f(CWDid not find a suitable bracket: ""%s""\fR" 4
.IX Item "Did not find a suitable bracket: ""%s"""
The delimiter provided to \f(CW\*(C`extract_bracketed\*(C'\fR was not one of
\&\f(CW\*(Aq()[]<>{}\*(Aq\fR.
.ie n .IP """Did not find prefix: /%s/""" 4
.el .IP "\f(CWDid not find prefix: /%s/\fR" 4
.IX Item "Did not find prefix: /%s/"
A non-optional prefix was specified but wasn't found at the start of the text.
.ie n .IP """Did not find opening bracket after prefix: ""%s""""" 4
.el .IP "\f(CWDid not find opening bracket after prefix: ""%s""\fR" 4
.IX Item "Did not find opening bracket after prefix: ""%s"""
\&\f(CW\*(C`extract_bracketed\*(C'\fR or \f(CW\*(C`extract_codeblock\*(C'\fR was expecting a
particular kind of bracket at the start of the text, and didn't find it.
.ie n .IP """No quotelike operator found after prefix: ""%s""""" 4
.el .IP "\f(CWNo quotelike operator found after prefix: ""%s""\fR" 4
.IX Item "No quotelike operator found after prefix: ""%s"""
\&\f(CW\*(C`extract_quotelike\*(C'\fR didn't find one of the quotelike operators \f(CW\*(C`q\*(C'\fR,
\&\f(CW\*(C`qq\*(C'\fR, \f(CW\*(C`qw\*(C'\fR, \f(CW\*(C`qx\*(C'\fR, \f(CW\*(C`s\*(C'\fR, \f(CW\*(C`tr\*(C'\fR or \f(CW\*(C`y\*(C'\fR at the start of the substring
it was extracting.
.ie n .IP """Unmatched closing bracket: ""%c""""" 4
.el .IP "\f(CWUnmatched closing bracket: ""%c""\fR" 4
.IX Item "Unmatched closing bracket: ""%c"""
\&\f(CW\*(C`extract_bracketed\*(C'\fR, \f(CW\*(C`extract_quotelike\*(C'\fR or \f(CW\*(C`extract_codeblock\*(C'\fR encountered
a closing bracket where none was expected.
.ie n .IP """Unmatched opening bracket(s): ""%s""""" 4
.el .IP "\f(CWUnmatched opening bracket(s): ""%s""\fR" 4
.IX Item "Unmatched opening bracket(s): ""%s"""
\&\f(CW\*(C`extract_bracketed\*(C'\fR, \f(CW\*(C`extract_quotelike\*(C'\fR or \f(CW\*(C`extract_codeblock\*(C'\fR ran
out of characters in the text before closing one or more levels of nested
brackets.
.ie n .IP """Unmatched embedded quote (%s)""" 4
.el .IP "\f(CWUnmatched embedded quote (%s)\fR" 4
.IX Item "Unmatched embedded quote (%s)"
\&\f(CW\*(C`extract_bracketed\*(C'\fR attempted to match an embedded quoted substring, but
failed to find a closing quote to match it.
.ie n .IP """Did not find closing delimiter to match \*(Aq%s\*(Aq""" 4
.el .IP "\f(CWDid not find closing delimiter to match \*(Aq%s\*(Aq\fR" 4
.IX Item "Did not find closing delimiter to match %s"
\&\f(CW\*(C`extract_quotelike\*(C'\fR was unable to find a closing delimiter to match the
one that opened the quote-like operation.
.ie n .IP """Mismatched closing bracket: expected ""%c"" but found ""%s""""" 4
.el .IP "\f(CWMismatched closing bracket: expected ""%c"" but found ""%s""\fR" 4
.IX Item "Mismatched closing bracket: expected ""%c"" but found ""%s"""
\&\f(CW\*(C`extract_bracketed\*(C'\fR, \f(CW\*(C`extract_quotelike\*(C'\fR or \f(CW\*(C`extract_codeblock\*(C'\fR found
a valid bracket delimiter, but it was the wrong species. This usually
indicates a nesting error, but may indicate incorrect quoting or escaping.
.ie n .IP """No block delimiter found after quotelike ""%s""""" 4
.el .IP "\f(CWNo block delimiter found after quotelike ""%s""\fR" 4
.IX Item "No block delimiter found after quotelike ""%s"""
\&\f(CW\*(C`extract_quotelike\*(C'\fR or \f(CW\*(C`extract_codeblock\*(C'\fR found one of the
quotelike operators \f(CW\*(C`q\*(C'\fR, \f(CW\*(C`qq\*(C'\fR, \f(CW\*(C`qw\*(C'\fR, \f(CW\*(C`qx\*(C'\fR, \f(CW\*(C`s\*(C'\fR, \f(CW\*(C`tr\*(C'\fR or \f(CW\*(C`y\*(C'\fR
without a suitable block after it.
.ie n .IP """Did not find leading dereferencer""" 4
.el .IP "\f(CWDid not find leading dereferencer\fR" 4
.IX Item "Did not find leading dereferencer"
\&\f(CW\*(C`extract_variable\*(C'\fR was expecting one of '$', '@', or '%' at the start of
a variable, but didn't find any of them.
.ie n .IP """Bad identifier after dereferencer""" 4
.el .IP "\f(CWBad identifier after dereferencer\fR" 4
.IX Item "Bad identifier after dereferencer"
\&\f(CW\*(C`extract_variable\*(C'\fR found a '$', '@', or '%' indicating a variable, but that
character was not followed by a legal Perl identifier.
.ie n .IP """Did not find expected opening bracket at %s""" 4
.el .IP "\f(CWDid not find expected opening bracket at %s\fR" 4
.IX Item "Did not find expected opening bracket at %s"
\&\f(CW\*(C`extract_codeblock\*(C'\fR failed to find any of the outermost opening brackets
that were specified.
.ie n .IP """Improperly nested codeblock at %s""" 4
.el .IP "\f(CWImproperly nested codeblock at %s\fR" 4
.IX Item "Improperly nested codeblock at %s"
A nested code block was found that started with a delimiter that was specified
as being only to be used as an outermost bracket.
.ie n .IP """Missing second block for quotelike ""%s""""" 4
.el .IP "\f(CWMissing second block for quotelike ""%s""\fR" 4
.IX Item "Missing second block for quotelike ""%s"""
\&\f(CW\*(C`extract_codeblock\*(C'\fR or \f(CW\*(C`extract_quotelike\*(C'\fR found one of the
quotelike operators \f(CW\*(C`s\*(C'\fR, \f(CW\*(C`tr\*(C'\fR or \f(CW\*(C`y\*(C'\fR followed by only one block.
.ie n .IP """No match found for opening bracket""" 4
.el .IP "\f(CWNo match found for opening bracket\fR" 4
.IX Item "No match found for opening bracket"
\&\f(CW\*(C`extract_codeblock\*(C'\fR failed to find a closing bracket to match the outermost
opening bracket.
.ie n .IP """Did not find opening tag: /%s/""" 4
.el .IP "\f(CWDid not find opening tag: /%s/\fR" 4
.IX Item "Did not find opening tag: /%s/"
\&\f(CW\*(C`extract_tagged\*(C'\fR did not find a suitable opening tag (after any specified
prefix was removed).
.ie n .IP """Unable to construct closing tag to match: /%s/""" 4
.el .IP "\f(CWUnable to construct closing tag to match: /%s/\fR" 4
.IX Item "Unable to construct closing tag to match: /%s/"
\&\f(CW\*(C`extract_tagged\*(C'\fR matched the specified opening tag and tried to
modify the matched text to produce a matching closing tag (because
none was specified). It failed to generate the closing tag, almost
certainly because the opening tag did not start with a
bracket of some kind.
.ie n .IP """Found invalid nested tag: %s""" 4
.el .IP "\f(CWFound invalid nested tag: %s\fR" 4
.IX Item "Found invalid nested tag: %s"
\&\f(CW\*(C`extract_tagged\*(C'\fR found a nested tag that appeared in the "reject" list
(and the failure mode was not "MAX" or "PARA").
.ie n .IP """Found unbalanced nested tag: %s""" 4
.el .IP "\f(CWFound unbalanced nested tag: %s\fR" 4
.IX Item "Found unbalanced nested tag: %s"
\&\f(CW\*(C`extract_tagged\*(C'\fR found a nested opening tag that was not matched by a
corresponding nested closing tag (and the failure mode was not "MAX" or "PARA").
.ie n .IP """Did not find closing tag""" 4
.el .IP "\f(CWDid not find closing tag\fR" 4
.IX Item "Did not find closing tag"
\&\f(CW\*(C`extract_tagged\*(C'\fR reached the end of the text without finding a closing tag
to match the original opening tag (and the failure mode was not
"MAX" or "PARA").
.SH EXPORTS
.IX Header "EXPORTS"
The following symbols are, or can be, exported by this module:
.IP "Default Exports" 4
.IX Item "Default Exports"
\&\fINone\fR.
.IP "Optional Exports" 4
.IX Item "Optional Exports"
\&\f(CW\*(C`extract_delimited\*(C'\fR,
\&\f(CW\*(C`extract_bracketed\*(C'\fR,
\&\f(CW\*(C`extract_quotelike\*(C'\fR,
\&\f(CW\*(C`extract_codeblock\*(C'\fR,
\&\f(CW\*(C`extract_variable\*(C'\fR,
\&\f(CW\*(C`extract_tagged\*(C'\fR,
\&\f(CW\*(C`extract_multiple\*(C'\fR,
\&\f(CW\*(C`gen_delimited_pat\*(C'\fR,
\&\f(CW\*(C`gen_extract_tagged\*(C'\fR,
\&\f(CW\*(C`delimited_pat\*(C'\fR.
.IP "Export Tags" 4
.IX Item "Export Tags"
.RS 4
.PD 0
.ie n .IP """:ALL""" 4
.el .IP \f(CW:ALL\fR 4
.IX Item ":ALL"
.PD
\&\f(CW\*(C`extract_delimited\*(C'\fR,
\&\f(CW\*(C`extract_bracketed\*(C'\fR,
\&\f(CW\*(C`extract_quotelike\*(C'\fR,
\&\f(CW\*(C`extract_codeblock\*(C'\fR,
\&\f(CW\*(C`extract_variable\*(C'\fR,
\&\f(CW\*(C`extract_tagged\*(C'\fR,
\&\f(CW\*(C`extract_multiple\*(C'\fR,
\&\f(CW\*(C`gen_delimited_pat\*(C'\fR,
\&\f(CW\*(C`gen_extract_tagged\*(C'\fR,
\&\f(CW\*(C`delimited_pat\*(C'\fR.
.RE
.RS 4
.RE
.SH "KNOWN BUGS"
.IX Header "KNOWN BUGS"
See <https://rt.cpan.org/Dist/Display.html?Status=Active&Queue=Text\-Balanced>.
.SH FEEDBACK
.IX Header "FEEDBACK"
Patches, bug reports, suggestions or any other feedback is welcome.
.PP
Patches can be sent as GitHub pull requests at
<https://github.com/steve\-m\-hay/Text\-Balanced/pulls>.
.PP
Bug reports and suggestions can be made on the CPAN Request Tracker at
<https://rt.cpan.org/Public/Bug/Report.html?Queue=Text\-Balanced>.
.PP
Currently active requests on the CPAN Request Tracker can be viewed at
<https://rt.cpan.org/Public/Dist/Display.html?Status=Active;Queue=Text\-Balanced>.
.PP
Please test this distribution.  See CPAN Testers Reports at
<https://www.cpantesters.org/> for details of how to get involved.
.PP
Previous test results on CPAN Testers Reports can be viewed at
<https://www.cpantesters.org/distro/T/Text\-Balanced.html>.
.PP
Please rate this distribution on CPAN Ratings at
<https://cpanratings.perl.org/rate/?distribution=Text\-Balanced>.
.SH AVAILABILITY
.IX Header "AVAILABILITY"
The latest version of this module is available from CPAN (see
"CPAN" in perlmodlib for details) at
.PP
<https://metacpan.org/release/Text\-Balanced> or
.PP
<https://www.cpan.org/authors/id/S/SH/SHAY/> or
.PP
<https://www.cpan.org/modules/by\-module/Text/>.
.PP
The latest source code is available from GitHub at
<https://github.com/steve\-m\-hay/Text\-Balanced>.
.SH INSTALLATION
.IX Header "INSTALLATION"
See the \fIINSTALL\fR file.
.SH AUTHOR
.IX Header "AUTHOR"
Damian Conway <damian@conway.org <mailto:damian@conway.org>>.
.PP
Steve Hay <shay@cpan.org <mailto:shay@cpan.org>> is now maintaining
Text::Balanced as of version 2.03.
.SH COPYRIGHT
.IX Header "COPYRIGHT"
Copyright (C) 1997\-2001 Damian Conway.  All rights reserved.
.PP
Copyright (C) 2009 Adam Kennedy.
.PP
Copyright (C) 2015, 2020, 2022 Steve Hay and other contributors.  All rights
reserved.
.SH LICENCE
.IX Header "LICENCE"
This module is free software; you can redistribute it and/or modify it under the
same terms as Perl itself, i.e. under the terms of either the GNU General Public
License or the Artistic License, as specified in the \fILICENCE\fR file.
.SH VERSION
.IX Header "VERSION"
Version 2.06
.SH DATE
.IX Header "DATE"
05 Jun 2022
.SH HISTORY
.IX Header "HISTORY"
See the \fIChanges\fR file.