-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtc-bench
executable file
·9557 lines (7209 loc) · 306 KB
/
mtc-bench
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
#!/usr/bin/env perl
# This chunk of stuff was generated by App::FatPacker. To find the original
# file's code, look for the end of this BEGIN block or the string 'FATPACK'
BEGIN {
my %fatpacked;
$fatpacked{"Text/CSV.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'TEXT_CSV';
package Text::CSV;
use strict;
use Exporter;
use Carp ();
use vars qw( $VERSION $DEBUG @ISA @EXPORT_OK %EXPORT_TAGS );
@ISA = qw( Exporter );
BEGIN {
$VERSION = '2.04';
$DEBUG = 0;
}
# if use CSV_XS, requires version
my $Module_XS = 'Text::CSV_XS';
my $Module_PP = 'Text::CSV_PP';
my $XS_Version = '1.53';
my $Is_Dynamic = 0;
my @PublicMethods = qw/
version error_diag error_input
known_attributes
PV IV NV CSV_TYPE_PV CSV_TYPE_IV CSV_TYPE_NV
CSV_FLAGS_IS_QUOTED CSV_FLAGS_IS_BINARY CSV_FLAGS_ERROR_IN_FIELD CSV_FLAGS_IS_MISSING
/;
%EXPORT_TAGS = (
CONSTANTS => [qw(
CSV_FLAGS_IS_QUOTED
CSV_FLAGS_IS_BINARY
CSV_FLAGS_ERROR_IN_FIELD
CSV_FLAGS_IS_MISSING
CSV_TYPE_PV
CSV_TYPE_IV
CSV_TYPE_NV
)],
);
@EXPORT_OK = (qw(csv PV IV NV), @{$EXPORT_TAGS{CONSTANTS}});
#
# Check the environment variable to decide worker module.
unless ($Text::CSV::Worker) {
$Text::CSV::DEBUG and Carp::carp("Check used worker module...");
if ( exists $ENV{PERL_TEXT_CSV} ) {
if ($ENV{PERL_TEXT_CSV} eq '0' or $ENV{PERL_TEXT_CSV} eq 'Text::CSV_PP') {
_load_pp() or Carp::croak $@;
}
elsif ($ENV{PERL_TEXT_CSV} eq '1' or $ENV{PERL_TEXT_CSV} =~ /Text::CSV_XS\s*,\s*Text::CSV_PP/) {
_load_xs() or _load_pp() or Carp::croak $@;
}
elsif ($ENV{PERL_TEXT_CSV} eq '2' or $ENV{PERL_TEXT_CSV} eq 'Text::CSV_XS') {
_load_xs() or Carp::croak $@;
}
else {
Carp::croak "The value of environmental variable 'PERL_TEXT_CSV' is invalid.";
}
}
else {
_load_xs() or _load_pp() or Carp::croak $@;
}
}
sub new { # normal mode
my $proto = shift;
my $class = ref($proto) || $proto;
unless ( $proto ) { # for Text::CSV_XS/PP::new(0);
return eval qq| $Text::CSV::Worker\::new( \$proto ) |;
}
#if (ref $_[0] and $_[0]->{module}) {
# Carp::croak("Can't set 'module' in non dynamic mode.");
#}
if ( my $obj = $Text::CSV::Worker->new(@_) ) {
$obj->{_MODULE} = $Text::CSV::Worker;
bless $obj, $class;
return $obj;
}
else {
return;
}
}
sub csv {
if (@_ && ref $_[0] eq __PACKAGE__ or ref $_[0] eq __PACKAGE__->backend) {
splice @_, 0, 0, "csv";
}
my $backend = __PACKAGE__->backend;
no strict 'refs';
&{"$backend\::csv"}(@_);
}
sub require_xs_version { $XS_Version; }
sub module {
my $proto = shift;
return !ref($proto) ? $Text::CSV::Worker
: ref($proto->{_MODULE}) ? ref($proto->{_MODULE}) : $proto->{_MODULE};
}
*backend = *module;
sub is_xs {
return $_[0]->module eq $Module_XS;
}
sub is_pp {
return $_[0]->module eq $Module_PP;
}
sub is_dynamic { $Is_Dynamic; }
sub _load_xs { _load($Module_XS, $XS_Version) }
sub _load_pp { _load($Module_PP) }
sub _load {
my ($module, $version) = @_;
$version ||= '';
$Text::CSV::DEBUG and Carp::carp "Load $module.";
eval qq| use $module $version |;
return if $@;
push @Text::CSV::ISA, $module;
$Text::CSV::Worker = $module;
local $^W;
no strict qw(refs);
for my $method (@PublicMethods) {
*{"Text::CSV::$method"} = \&{"$module\::$method"};
}
return 1;
}
1;
__END__
=pod
=head1 NAME
Text::CSV - comma-separated values manipulator (using XS or PurePerl)
=head1 SYNOPSIS
This section is taken from Text::CSV_XS.
# Functional interface
use Text::CSV qw( csv );
# Read whole file in memory
my $aoa = csv (in => "data.csv"); # as array of array
my $aoh = csv (in => "data.csv",
headers => "auto"); # as array of hash
# Write array of arrays as csv file
csv (in => $aoa, out => "file.csv", sep_char => ";");
# Only show lines where "code" is odd
csv (in => "data.csv", filter => { code => sub { $_ % 2 }});
# Object interface
use Text::CSV;
my @rows;
# Read/parse CSV
my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1 });
open my $fh, "<:encoding(utf8)", "test.csv" or die "test.csv: $!";
while (my $row = $csv->getline ($fh)) {
$row->[2] =~ m/pattern/ or next; # 3rd field should match
push @rows, $row;
}
close $fh;
# and write as CSV
open $fh, ">:encoding(utf8)", "new.csv" or die "new.csv: $!";
$csv->say ($fh, $_) for @rows;
close $fh or die "new.csv: $!";
=head1 DESCRIPTION
Text::CSV is a thin wrapper for L<Text::CSV_XS>-compatible modules now.
All the backend modules provide facilities for the composition and
decomposition of comma-separated values. Text::CSV uses Text::CSV_XS
by default, and when Text::CSV_XS is not available, falls back on
L<Text::CSV_PP>, which is bundled in the same distribution as this module.
=head1 CHOOSING BACKEND
This module respects an environmental variable called C<PERL_TEXT_CSV>
when it decides a backend module to use. If this environmental variable
is not set, it tries to load Text::CSV_XS, and if Text::CSV_XS is not
available, falls back on Text::CSV_PP;
If you always don't want it to fall back on Text::CSV_PP, set the variable
like this (C<export> may be C<setenv>, C<set> and the likes, depending
on your environment):
> export PERL_TEXT_CSV=Text::CSV_XS
If you prefer Text::CSV_XS to Text::CSV_PP (default), then:
> export PERL_TEXT_CSV=Text::CSV_XS,Text::CSV_PP
You may also want to set this variable at the top of your test files, in order
not to be bothered with incompatibilities between backends (you need to wrap
this in C<BEGIN>, and set before actually C<use>-ing Text::CSV module, as it
decides its backend as soon as it's loaded):
BEGIN { $ENV{PERL_TEXT_CSV}='Text::CSV_PP'; }
use Text::CSV;
=head1 NOTES
This section is also taken from Text::CSV_XS.
=head2 Embedded newlines
B<Important Note>: The default behavior is to accept only ASCII characters
in the range from C<0x20> (space) to C<0x7E> (tilde). This means that the
fields can not contain newlines. If your data contains newlines embedded in
fields, or characters above C<0x7E> (tilde), or binary data, you B<I<must>>
set C<< binary => 1 >> in the call to L</new>. To cover the widest range of
parsing options, you will always want to set binary.
But you still have the problem that you have to pass a correct line to the
L</parse> method, which is more complicated from the usual point of usage:
my $csv = Text::CSV->new ({ binary => 1, eol => $/ });
while (<>) { # WRONG!
$csv->parse ($_);
my @fields = $csv->fields ();
}
this will break, as the C<while> might read broken lines: it does not care
about the quoting. If you need to support embedded newlines, the way to go
is to B<not> pass L<C<eol>|/eol> in the parser (it accepts C<\n>, C<\r>,
B<and> C<\r\n> by default) and then
my $csv = Text::CSV->new ({ binary => 1 });
open my $fh, "<", $file or die "$file: $!";
while (my $row = $csv->getline ($fh)) {
my @fields = @$row;
}
The old(er) way of using global file handles is still supported
while (my $row = $csv->getline (*ARGV)) { ... }
=head2 Unicode
Unicode is only tested to work with perl-5.8.2 and up.
See also L</BOM>.
The simplest way to ensure the correct encoding is used for in- and output
is by either setting layers on the filehandles, or setting the L</encoding>
argument for L</csv>.
open my $fh, "<:encoding(UTF-8)", "in.csv" or die "in.csv: $!";
or
my $aoa = csv (in => "in.csv", encoding => "UTF-8");
open my $fh, ">:encoding(UTF-8)", "out.csv" or die "out.csv: $!";
or
csv (in => $aoa, out => "out.csv", encoding => "UTF-8");
On parsing (both for L</getline> and L</parse>), if the source is marked
being UTF8, then all fields that are marked binary will also be marked UTF8.
On combining (L</print> and L</combine>): if any of the combining fields
was marked UTF8, the resulting string will be marked as UTF8. Note however
that all fields I<before> the first field marked UTF8 and contained 8-bit
characters that were not upgraded to UTF8, these will be C<bytes> in the
resulting string too, possibly causing unexpected errors. If you pass data
of different encoding, or you don't know if there is different encoding,
force it to be upgraded before you pass them on:
$csv->print ($fh, [ map { utf8::upgrade (my $x = $_); $x } @data ]);
For complete control over encoding, please use L<Text::CSV::Encoded>:
use Text::CSV::Encoded;
my $csv = Text::CSV::Encoded->new ({
encoding_in => "iso-8859-1", # the encoding comes into Perl
encoding_out => "cp1252", # the encoding comes out of Perl
});
$csv = Text::CSV::Encoded->new ({ encoding => "utf8" });
# combine () and print () accept *literally* utf8 encoded data
# parse () and getline () return *literally* utf8 encoded data
$csv = Text::CSV::Encoded->new ({ encoding => undef }); # default
# combine () and print () accept UTF8 marked data
# parse () and getline () return UTF8 marked data
=head2 BOM
BOM (or Byte Order Mark) handling is available only inside the L</header>
method. This method supports the following encodings: C<utf-8>, C<utf-1>,
C<utf-32be>, C<utf-32le>, C<utf-16be>, C<utf-16le>, C<utf-ebcdic>, C<scsu>,
C<bocu-1>, and C<gb-18030>. See L<Wikipedia|https://en.wikipedia.org/wiki/Byte_order_mark>.
If a file has a BOM, the easiest way to deal with that is
my $aoh = csv (in => $file, detect_bom => 1);
All records will be encoded based on the detected BOM.
This implies a call to the L</header> method, which defaults to also set
the L</column_names>. So this is B<not> the same as
my $aoh = csv (in => $file, headers => "auto");
which only reads the first record to set L</column_names> but ignores any
meaning of possible present BOM.
=head1 METHODS
This section is also taken from Text::CSV_XS.
=head2 version
(Class method) Returns the current module version.
=head2 new
(Class method) Returns a new instance of class Text::CSV. The attributes
are described by the (optional) hash ref C<\%attr>.
my $csv = Text::CSV->new ({ attributes ... });
The following attributes are available:
=head3 eol
my $csv = Text::CSV->new ({ eol => $/ });
$csv->eol (undef);
my $eol = $csv->eol;
The end-of-line string to add to rows for L</print> or the record separator
for L</getline>.
When not passed in a B<parser> instance, the default behavior is to accept
C<\n>, C<\r>, and C<\r\n>, so it is probably safer to not specify C<eol> at
all. Passing C<undef> or the empty string behave the same.
When not passed in a B<generating> instance, records are not terminated at
all, so it is probably wise to pass something you expect. A safe choice for
C<eol> on output is either C<$/> or C<\r\n>.
Common values for C<eol> are C<"\012"> (C<\n> or Line Feed), C<"\015\012">
(C<\r\n> or Carriage Return, Line Feed), and C<"\015"> (C<\r> or Carriage
Return). The L<C<eol>|/eol> attribute cannot exceed 7 (ASCII) characters.
If both C<$/> and L<C<eol>|/eol> equal C<"\015">, parsing lines that end on
only a Carriage Return without Line Feed, will be L</parse>d correct.
=head3 sep_char
my $csv = Text::CSV->new ({ sep_char => ";" });
$csv->sep_char (";");
my $c = $csv->sep_char;
The char used to separate fields, by default a comma. (C<,>). Limited to a
single-byte character, usually in the range from C<0x20> (space) to C<0x7E>
(tilde). When longer sequences are required, use L<C<sep>|/sep>.
The separation character can not be equal to the quote character or to the
escape character.
=head3 sep
my $csv = Text::CSV->new ({ sep => "\N{FULLWIDTH COMMA}" });
$csv->sep (";");
my $sep = $csv->sep;
The chars used to separate fields, by default undefined. Limited to 8 bytes.
When set, overrules L<C<sep_char>|/sep_char>. If its length is one byte it
acts as an alias to L<C<sep_char>|/sep_char>.
=head3 quote_char
my $csv = Text::CSV->new ({ quote_char => "'" });
$csv->quote_char (undef);
my $c = $csv->quote_char;
The character to quote fields containing blanks or binary data, by default
the double quote character (C<">). A value of undef suppresses quote chars
(for simple cases only). Limited to a single-byte character, usually in the
range from C<0x20> (space) to C<0x7E> (tilde). When longer sequences are
required, use L<C<quote>|/quote>.
C<quote_char> can not be equal to L<C<sep_char>|/sep_char>.
=head3 quote
my $csv = Text::CSV->new ({ quote => "\N{FULLWIDTH QUOTATION MARK}" });
$csv->quote ("'");
my $quote = $csv->quote;
The chars used to quote fields, by default undefined. Limited to 8 bytes.
When set, overrules L<C<quote_char>|/quote_char>. If its length is one byte
it acts as an alias to L<C<quote_char>|/quote_char>.
This method does not support C<undef>. Use L<C<quote_char>|/quote_char> to
disable quotation.
=head3 escape_char
my $csv = Text::CSV->new ({ escape_char => "\\" });
$csv->escape_char (":");
my $c = $csv->escape_char;
The character to escape certain characters inside quoted fields. This is
limited to a single-byte character, usually in the range from C<0x20>
(space) to C<0x7E> (tilde).
The C<escape_char> defaults to being the double-quote mark (C<">). In other
words the same as the default L<C<quote_char>|/quote_char>. This means that
doubling the quote mark in a field escapes it:
"foo","bar","Escape ""quote mark"" with two ""quote marks""","baz"
If you change the L<C<quote_char>|/quote_char> without changing the
C<escape_char>, the C<escape_char> will still be the double-quote (C<">).
If instead you want to escape the L<C<quote_char>|/quote_char> by doubling
it you will need to also change the C<escape_char> to be the same as what
you have changed the L<C<quote_char>|/quote_char> to.
Setting C<escape_char> to C<undef> or C<""> will completely disable escapes
and is greatly discouraged. This will also disable C<escape_null>.
The escape character can not be equal to the separation character.
=head3 binary
my $csv = Text::CSV->new ({ binary => 1 });
$csv->binary (0);
my $f = $csv->binary;
If this attribute is C<1>, you may use binary characters in quoted fields,
including line feeds, carriage returns and C<NULL> bytes. (The latter could
be escaped as C<"0>.) By default this feature is off.
If a string is marked UTF8, C<binary> will be turned on automatically when
binary characters other than C<CR> and C<NL> are encountered. Note that a
simple string like C<"\x{00a0}"> might still be binary, but not marked UTF8,
so setting C<< { binary => 1 } >> is still a wise option.
=head3 strict
my $csv = Text::CSV->new ({ strict => 1 });
$csv->strict (0);
my $f = $csv->strict;
If this attribute is set to C<1>, any row that parses to a different number
of fields than the previous row will cause the parser to throw error 2014.
=head3 skip_empty_rows
my $csv = Text::CSV->new ({ skip_empty_rows => 1 });
$csv->skip_empty_rows ("eof");
my $f = $csv->skip_empty_rows;
This attribute defines the behavior for empty rows: an L</eol> immediately
following the start of line. Default behavior is to return one single empty
field.
This attribute is only used in parsing. This attribute is ineffective when
using L</parse> and L</fields>.
Possible values for this attribute are
=over 2
=item 0 | undef
my $csv = Text::CSV->new ({ skip_empty_rows => 0 });
$csv->skip_empty_rows (undef);
No special action is taken. The result will be one single empty field.
=item 1 | "skip"
my $csv = Text::CSV->new ({ skip_empty_rows => 1 });
$csv->skip_empty_rows ("skip");
The row will be skipped.
=item 2 | "eof" | "stop"
my $csv = Text::CSV->new ({ skip_empty_rows => 2 });
$csv->skip_empty_rows ("eof");
The parsing will stop as if an L</eof> was detected.
=item 3 | "die"
my $csv = Text::CSV->new ({ skip_empty_rows => 3 });
$csv->skip_empty_rows ("die");
The parsing will stop. The internal error code will be set to 2015 and the
parser will C<die>.
=item 4 | "croak"
my $csv = Text::CSV->new ({ skip_empty_rows => 4 });
$csv->skip_empty_rows ("croak");
The parsing will stop. The internal error code will be set to 2015 and the
parser will C<croak>.
=item 5 | "error"
my $csv = Text::CSV->new ({ skip_empty_rows => 5 });
$csv->skip_empty_rows ("error");
The parsing will fail. The internal error code will be set to 2015.
=item callback
my $csv = Text::CSV->new ({ skip_empty_rows => sub { [] } });
$csv->skip_empty_rows (sub { [ 42, $., undef, "empty" ] });
The callback is invoked and its result used instead. If you want the parse
to stop after the callback, make sure to return a false value.
The returned value from the callback should be an array-ref. Any other type
will cause the parse to stop, so these are equivalent in behavior:
csv (in => $fh, skip_empty_rows => "stop");
csv (in => $fh. skip_empty_rows => sub { 0; });
=back
Without arguments, the current value is returned: C<0>, C<1>, C<eof>, C<die>,
C<croak> or the callback.
=head3 formula_handling
Alias for L</formula>
=head3 formula
my $csv = Text::CSV->new ({ formula => "none" });
$csv->formula ("none");
my $f = $csv->formula;
This defines the behavior of fields containing I<formulas>. As formulas are
considered dangerous in spreadsheets, this attribute can define an optional
action to be taken if a field starts with an equal sign (C<=>).
For purpose of code-readability, this can also be written as
my $csv = Text::CSV->new ({ formula_handling => "none" });
$csv->formula_handling ("none");
my $f = $csv->formula_handling;
Possible values for this attribute are
=over 2
=item none
Take no specific action. This is the default.
$csv->formula ("none");
=item die
Cause the process to C<die> whenever a leading C<=> is encountered.
$csv->formula ("die");
=item croak
Cause the process to C<croak> whenever a leading C<=> is encountered. (See
L<Carp>)
$csv->formula ("croak");
=item diag
Report position and content of the field whenever a leading C<=> is found.
The value of the field is unchanged.
$csv->formula ("diag");
=item empty
Replace the content of fields that start with a C<=> with the empty string.
$csv->formula ("empty");
$csv->formula ("");
=item undef
Replace the content of fields that start with a C<=> with C<undef>.
$csv->formula ("undef");
$csv->formula (undef);
=item a callback
Modify the content of fields that start with a C<=> with the return-value
of the callback. The original content of the field is available inside the
callback as C<$_>;
# Replace all formula's with 42
$csv->formula (sub { 42; });
# same as $csv->formula ("empty") but slower
$csv->formula (sub { "" });
# Allow =4+12
$csv->formula (sub { s/^=(\d+\+\d+)$/$1/eer });
# Allow more complex calculations
$csv->formula (sub { eval { s{^=([-+*/0-9()]+)$}{$1}ee }; $_ });
=back
All other values will give a warning and then fallback to C<diag>.
=head3 decode_utf8
my $csv = Text::CSV->new ({ decode_utf8 => 1 });
$csv->decode_utf8 (0);
my $f = $csv->decode_utf8;
This attributes defaults to TRUE.
While I<parsing>, fields that are valid UTF-8, are automatically set to be
UTF-8, so that
$csv->parse ("\xC4\xA8\n");
results in
PV("\304\250"\0) [UTF8 "\x{128}"]
Sometimes it might not be a desired action. To prevent those upgrades, set
this attribute to false, and the result will be
PV("\304\250"\0)
=head3 auto_diag
my $csv = Text::CSV->new ({ auto_diag => 1 });
$csv->auto_diag (2);
my $l = $csv->auto_diag;
Set this attribute to a number between C<1> and C<9> causes L</error_diag>
to be automatically called in void context upon errors.
In case of error C<2012 - EOF>, this call will be void.
If C<auto_diag> is set to a numeric value greater than C<1>, it will C<die>
on errors instead of C<warn>. If set to anything unrecognized, it will be
silently ignored.
Future extensions to this feature will include more reliable auto-detection
of C<autodie> being active in the scope of which the error occurred which
will increment the value of C<auto_diag> with C<1> the moment the error is
detected.
=head3 diag_verbose
my $csv = Text::CSV->new ({ diag_verbose => 1 });
$csv->diag_verbose (2);
my $l = $csv->diag_verbose;
Set the verbosity of the output triggered by C<auto_diag>. Currently only
adds the current input-record-number (if known) to the diagnostic output
with an indication of the position of the error.
=head3 blank_is_undef
my $csv = Text::CSV->new ({ blank_is_undef => 1 });
$csv->blank_is_undef (0);
my $f = $csv->blank_is_undef;
Under normal circumstances, C<CSV> data makes no distinction between quoted-
and unquoted empty fields. These both end up in an empty string field once
read, thus
1,"",," ",2
is read as
("1", "", "", " ", "2")
When I<writing> C<CSV> files with either L<C<always_quote>|/always_quote>
or L<C<quote_empty>|/quote_empty> set, the unquoted I<empty> field is the
result of an undefined value. To enable this distinction when I<reading>
C<CSV> data, the C<blank_is_undef> attribute will cause unquoted empty
fields to be set to C<undef>, causing the above to be parsed as
("1", "", undef, " ", "2")
Note that this is specifically important when loading C<CSV> fields into a
database that allows C<NULL> values, as the perl equivalent for C<NULL> is
C<undef> in L<DBI> land.
=head3 empty_is_undef
my $csv = Text::CSV->new ({ empty_is_undef => 1 });
$csv->empty_is_undef (0);
my $f = $csv->empty_is_undef;
Going one step further than L<C<blank_is_undef>|/blank_is_undef>, this
attribute converts all empty fields to C<undef>, so
1,"",," ",2
is read as
(1, undef, undef, " ", 2)
Note that this affects only fields that are originally empty, not fields
that are empty after stripping allowed whitespace. YMMV.
=head3 allow_whitespace
my $csv = Text::CSV->new ({ allow_whitespace => 1 });
$csv->allow_whitespace (0);
my $f = $csv->allow_whitespace;
When this option is set to true, the whitespace (C<TAB>'s and C<SPACE>'s)
surrounding the separation character is removed when parsing. If either
C<TAB> or C<SPACE> is one of the three characters L<C<sep_char>|/sep_char>,
L<C<quote_char>|/quote_char>, or L<C<escape_char>|/escape_char> it will not
be considered whitespace.
Now lines like:
1 , "foo" , bar , 3 , zapp
are parsed as valid C<CSV>, even though it violates the C<CSV> specs.
Note that B<all> whitespace is stripped from both start and end of each
field. That would make it I<more> than a I<feature> to enable parsing bad
C<CSV> lines, as
1, 2.0, 3, ape , monkey
will now be parsed as
("1", "2.0", "3", "ape", "monkey")
even if the original line was perfectly acceptable C<CSV>.
=head3 allow_loose_quotes
my $csv = Text::CSV->new ({ allow_loose_quotes => 1 });
$csv->allow_loose_quotes (0);
my $f = $csv->allow_loose_quotes;
By default, parsing unquoted fields containing L<C<quote_char>|/quote_char>
characters like
1,foo "bar" baz,42
would result in parse error 2034. Though it is still bad practice to allow
this format, we cannot help the fact that some vendors make their
applications spit out lines styled this way.
If there is B<really> bad C<CSV> data, like
1,"foo "bar" baz",42
or
1,""foo bar baz"",42
there is a way to get this data-line parsed and leave the quotes inside the
quoted field as-is. This can be achieved by setting C<allow_loose_quotes>
B<AND> making sure that the L<C<escape_char>|/escape_char> is I<not> equal
to L<C<quote_char>|/quote_char>.
=head3 allow_loose_escapes
my $csv = Text::CSV->new ({ allow_loose_escapes => 1 });
$csv->allow_loose_escapes (0);
my $f = $csv->allow_loose_escapes;
Parsing fields that have L<C<escape_char>|/escape_char> characters that
escape characters that do not need to be escaped, like:
my $csv = Text::CSV->new ({ escape_char => "\\" });
$csv->parse (qq{1,"my bar\'s",baz,42});
would result in parse error 2025. Though it is bad practice to allow this
format, this attribute enables you to treat all escape character sequences
equal.
=head3 allow_unquoted_escape
my $csv = Text::CSV->new ({ allow_unquoted_escape => 1 });
$csv->allow_unquoted_escape (0);
my $f = $csv->allow_unquoted_escape;
A backward compatibility issue where L<C<escape_char>|/escape_char> differs
from L<C<quote_char>|/quote_char> prevents L<C<escape_char>|/escape_char>
to be in the first position of a field. If L<C<quote_char>|/quote_char> is
equal to the default C<"> and L<C<escape_char>|/escape_char> is set to C<\>,
this would be illegal:
1,\0,2
Setting this attribute to C<1> might help to overcome issues with backward
compatibility and allow this style.
=head3 always_quote
my $csv = Text::CSV->new ({ always_quote => 1 });
$csv->always_quote (0);
my $f = $csv->always_quote;
By default the generated fields are quoted only if they I<need> to be. For
example, if they contain the separator character. If you set this attribute
to C<1> then I<all> defined fields will be quoted. (C<undef> fields are not
quoted, see L</blank_is_undef>). This makes it quite often easier to handle
exported data in external applications.
=head3 quote_space
my $csv = Text::CSV->new ({ quote_space => 1 });
$csv->quote_space (0);
my $f = $csv->quote_space;
By default, a space in a field would trigger quotation. As no rule exists
this to be forced in C<CSV>, nor any for the opposite, the default is true
for safety. You can exclude the space from this trigger by setting this
attribute to 0.
=head3 quote_empty
my $csv = Text::CSV->new ({ quote_empty => 1 });
$csv->quote_empty (0);
my $f = $csv->quote_empty;
By default the generated fields are quoted only if they I<need> to be. An
empty (defined) field does not need quotation. If you set this attribute to
C<1> then I<empty> defined fields will be quoted. (C<undef> fields are not
quoted, see L</blank_is_undef>). See also L<C<always_quote>|/always_quote>.
=head3 quote_binary
my $csv = Text::CSV->new ({ quote_binary => 1 });
$csv->quote_binary (0);
my $f = $csv->quote_binary;
By default, all "unsafe" bytes inside a string cause the combined field to
be quoted. By setting this attribute to C<0>, you can disable that trigger
for bytes C<< >= 0x7F >>.
=head3 escape_null
my $csv = Text::CSV->new ({ escape_null => 1 });
$csv->escape_null (0);
my $f = $csv->escape_null;
By default, a C<NULL> byte in a field would be escaped. This option enables
you to treat the C<NULL> byte as a simple binary character in binary mode
(the C<< { binary => 1 } >> is set). The default is true. You can prevent
C<NULL> escapes by setting this attribute to C<0>.
When the C<escape_char> attribute is set to undefined, this attribute will
be set to false.
The default setting will encode "=\x00=" as
"="0="
With C<escape_null> set, this will result in
"=\x00="
The default when using the C<csv> function is C<false>.
For backward compatibility reasons, the deprecated old name C<quote_null>
is still recognized.
=head3 keep_meta_info
my $csv = Text::CSV->new ({ keep_meta_info => 1 });
$csv->keep_meta_info (0);
my $f = $csv->keep_meta_info;
By default, the parsing of input records is as simple and fast as possible.
However, some parsing information - like quotation of the original field -
is lost in that process. Setting this flag to true enables retrieving that
information after parsing with the methods L</meta_info>, L</is_quoted>,
and L</is_binary> described below. Default is false for performance.
If you set this attribute to a value greater than 9, then you can control
output quotation style like it was used in the input of the the last parsed
record (unless quotation was added because of other reasons).
my $csv = Text::CSV->new ({
binary => 1,
keep_meta_info => 1,
quote_space => 0,
});
my $row = $csv->parse (q{1,,"", ," ",f,"g","h""h",help,"help"});
$csv->print (*STDOUT, \@row);
# 1,,, , ,f,g,"h""h",help,help
$csv->keep_meta_info (11);
$csv->print (*STDOUT, \@row);
# 1,,"", ," ",f,"g","h""h",help,"help"
=head3 undef_str
my $csv = Text::CSV->new ({ undef_str => "\\N" });
$csv->undef_str (undef);
my $s = $csv->undef_str;
This attribute optionally defines the output of undefined fields. The value
passed is not changed at all, so if it needs quotation, the quotation needs
to be included in the value of the attribute. Use with caution, as passing
a value like C<",",,,,"""> will for sure mess up your output. The default
for this attribute is C<undef>, meaning no special treatment.
This attribute is useful when exporting CSV data to be imported in custom
loaders, like for MySQL, that recognize special sequences for C<NULL> data.
This attribute has no meaning when parsing CSV data.
=head3 comment_str
my $csv = Text::CSV->new ({ comment_str => "#" });
$csv->comment_str (undef);
my $s = $csv->comment_str;
This attribute optionally defines a string to be recognized as comment. If
this attribute is defined, all lines starting with this sequence will not
be parsed as CSV but skipped as comment.
This attribute has no meaning when generating CSV.
Comment strings that start with any of the special characters/sequences are
not supported (so it cannot start with any of L</sep_char>, L</quote_char>,
L</escape_char>, L</sep>, L</quote>, or L</eol>).
For convenience, C<comment> is an alias for C<comment_str>.
=head3 verbatim
my $csv = Text::CSV->new ({ verbatim => 1 });
$csv->verbatim (0);
my $f = $csv->verbatim;
This is a quite controversial attribute to set, but makes some hard things
possible.
The rationale behind this attribute is to tell the parser that the normally
special characters newline (C<NL>) and Carriage Return (C<CR>) will not be
special when this flag is set, and be dealt with as being ordinary binary
characters. This will ease working with data with embedded newlines.
When C<verbatim> is used with L</getline>, L</getline> auto-C<chomp>'s
every line.
Imagine a file format like
M^^Hans^Janssen^Klas 2\n2A^Ja^11-06-2007#\r\n