Skip to content
A parser for CSV (comma-separated values) files for Perl 6
Find file
Latest commit f7e877e @masak changing `X ~~ m//` to `X ~~ //`
There were some (weird) test failures, and this looked like the right fix.
Indeed, the change makes the test failures go away.


Simple CSV (comma-separated values) format parser for Perl 6.

  use v6;
  use Text::CSV;

  say Text::CSV.parse-file('somefile.csv').perl;
  say Text::CSV.parse("foo,bar\nbaz,boo").perl;

Oh, and the C<parse> methods take the following named parameters:
(Parameters without the postcircumfix parenthesis are booleans. Just include
them to set them to True. Parameters with postcircumfix parenthesis expect a
parameter value.)

    :trim          Removes whitespace on both ends of each value.

    :skip-header   Causes the first line not to be included in the output.

    :strict        Throws an error if a row has a different number of columns
                   than the previous ones.

    :output( )     Determines the shape of the returned data structure.
                   Allowed values are 'arrays' (the default), 'hashes',
                   and any type object (i.e. ':output(MyType)').
                   When the value is 'hashes' or a type object, the first line
                   is assumed to be a special header line, the values on that
                   line are used as hash keys, and :skip-header is suppressed.

    :quote( )      Use a character for quoting other than the default of
                   double quote '"'. Must be a single, non-space character.
                   Must be different from separator character.

    :separator( )  Use a separator character other than the default of comma
                   ','. Must be a single non new-line character. (Tabs are
                   fine. Spaces probably aren't a good idea, but... if you
                   want to use them, knock yourself out. Just be prepared for
                   fragile behavior. You have been warned.) Must be different
                   from quote character.

If you see yourself regularly contravening the defaults of one or more of
these parameters, it might be a good idea to instantiate the Text::CSV
class, giving it the default values you want:

  my $parser = :output<hashes>, :!strict );
  my Hash[Str] @hashes = $parser.parse-file('somefile.csv');

Text::CSV also exports a subroutine: C<csv-write>, to convert CSV data
structures back to a string. C<csv-write> supports all of the internal CSV
representations provided by Text::CSV.parse.

C<csv-write> expects a 2 dimensional array of CSV data in one of the
configurations provided by the .parse method (Array of Array, Array of Hash or
Array of Object) but also will handle a flat 1 dimension array of scalar values.

  use v6;
  use Text::CSV;

  my @csv = Text::CSV.parse("some,string,of,CSV,data");
  my $csv-string = csv-write( @csv );

There are a few other parameters which may be useful or necessary depending on
your needs and data.

    :header( )     An array of strings. Optional for Array mode. If provided,
                   written as the first (header) line in the file.
                   Mandatory for hash or object modes. For hashes, the header
                   array is the keys to the hash. In object mode, the
                   accessors for the object attributes.

    :always-quote  Always quote every field in the CSV, even if it would not
                   normally be necessary.

    :quote( )      Same rules as for the parser.

    :separator( )  Same rules as for the parser.

Unless you use a mutating parameter (:trim, :skip-header, :always-quote),
csv-write( Text::CSV.parse( "some,csv,string", %parameters ), %parameters )
should always return the original CSV string.

Like Text::CVS.parse-file, there is a C<csv-write-file> which has all the same
options and requirements as C<csv-write> but writes the CSV out to a given file
name. C<csv-write-file> will overwrite an existing file without warning and has
no option to append. If you need finer control, open a file handle and use
C<csv-write> which returns a string.

  use v6;
  use Text::CSV;

  my @csv = Text::CSV.parse-file('/path/to/in.csv');
  csv-write( @csv, :file('/path/to/out.csv') );

== License

This module is released under Artistic 2.0. See LICENSE.
Something went wrong with that request. Please try again.