Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 97 lines (68 sloc) 4.056 kb
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
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 = Text::CSV.new( :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.