How do I create and parse CSV files in Perl

Perl, CSV, Text::CSV, file handling
This code demonstrates how to create and parse CSV files in Perl using the Text::CSV module.
use strict; use warnings; use Text::CSV; # Create a CSV file my $filename = 'example.csv'; my $csv = Text::CSV->new({ binary => 1, auto_diag => 1 }); open my $fh, '>', $filename or die "Could not open '$filename' $!"; $csv->print($fh, ["Name", "Age", "City"]); $csv->print($fh, ["Alice", 30, "New York"]); $csv->print($fh, ["Bob", 25, "Los Angeles"]); close $fh; # Parse the CSV file open my $rh, '<', $filename or die "Could not open '$filename' $!"; while (my $row = $csv->getline($rh)) { print join(", ", @$row), "\n"; # Print each row } close $rh;

Perl CSV Text::CSV file handling