http://www.perlmonks.org?node_id=1080161

ricardo_sdl has asked for the wisdom of the Perl Monks concerning the following question:

I've read about the Text File Format(https://ronaldduncan.wordpress.com/2009/10/31/text-file-formats-ascii-delimited-text-not-csv-or-tab-delimited-text) and decided to create a perl script to convert a csv file to this format.

Instead of using one of the available modules(like Text::CSV) to parse the csv I decided to do it myself as matter of practice.

I'm starting to learn perl and I ask for your wisdom on how to make this code better.

Updated code after some suggestions.

#!/usr/bin/perl use 5.010; use strict; use warnings; use utf8; use open qw(:std :utf8); #Convert a csv file to text file format #https://ronaldduncan.wordpress.com/2009/10/31/text-file-formats-ascii +-delimited-text-not-csv-or-tab-delimited-text #the csv file is expected to use double quotes as the delimiter charac +ter and utf-8 encoded sub is_complete_line { my ($line) = @_; my $inside_double_quotes; my $opening_double_quotes = 0; my $closing_double_quotes = 0; foreach(split('', $line)) { next if $_ ne '"'; if ($inside_double_quotes) { $inside_double_quotes = ! $inside_double_quotes; $closing_double_quotes++; next; } $opening_double_quotes++; $inside_double_quotes = ! $inside_double_quotes; } $opening_double_quotes == $closing_double_quotes; } sub convert_to_ttf { my ($line) = @_; my $inside_double_quotes; my $converted_line = ''; foreach(split('', $line)) { if ($_ eq '"' && $inside_double_quotes) { $inside_double_quotes = 0; next; } if ($_ eq '"' && ! $inside_double_quotes) { $inside_double_quotes = 1; next; } if ($_ eq "," && ! $inside_double_quotes) { $converted_line .= "\x{1F}";#unit separator next; } if ($_ eq "\n" && ! $inside_double_quotes) { $converted_line .= "\x{1E}";#record separator next; } $converted_line .= $_; } $converted_line; } open my $fh, '>', "output.txt" or die "Couldn't create the output file +.\n"; my $line = ''; while (<>) { $line .= $_; if (is_complete_line($line)) { print $fh convert_to_ttf($line); $line = ''; } } close $fh; print "Conversion done.\n";

Here is a link to a sample csv file that you can convert(https://db.tt/GWrID417).