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


I have a perl script that converts a tab delimited file
to a comma delimited file - simple stuff. The script
reads STDIN from the input tab delimited file and
outputs to a CSV file.


The simple code is as follows:

############################################################# # # tab2comma.pl # Convert Tab Delimited File to Comma Delimited File (CSV) # ############################################################ while ( <> ) { tr/\t/,/; print; }


I execute this from the command line as follows:


perl tab2comma.pl psa.tab > psa.csv


where psa.tab is the input tab-delimited file and
psa.csv is the output comma-delimited file.


I am attempting to modify this script, so that it can
be included as a snippet in a larger perl script. I am
not quite sure how to do this and I am getting an
error message which I am not familiar with.


My modified code is:

############################################################ # testtab2comma.pl # Convert Tab Delimited File to Comma Delimited File (CSV) # ############################################################ my $file = "sortpsalog.dat"; open (my $fh,"<", $file) or die "Can't open file $file: $!"; open (my $OUT,">","sortpsalog2.csv") or die "Can't open sort output fi +le: $!"; while <my $line = <$fh>) { chomp($line); ## $line =~ tr/\t/,/; $line =~ s/\t/,/g; print $OUT "$line \n"; } close $fh or die "Can't close input file: $!"; close $OUT or die "Can't close output csv file: $!";

When I execute this perl script I am getting the
following error message:


Glob not terminated at testtab2comma.pl line 17.


Line 17 is the while loop statement. I would appreciate
any assistance on how to convert from the 1st technique
to the 2nd technique or if someone could help me with
the 2nd technique I am devising and the error message
I am getting.

Replies are listed 'Best First'.
Re: Using Substitution Operator In Perl Script
by ikegami (Patriarch) on Aug 28, 2007 at 16:21 UTC

    while <my $line = <$fh>) {
    should be
    while (my $line = <$fh>) {

Re: Using Substitution Operator In Perl Script
by duckyd (Hermit) on Aug 28, 2007 at 16:58 UTC
    ikegami has noted the reason that your script will not run, however I think it's worth mentioning that you may want to consider a more robust way to generate your CSV than simply using tr//. For example, if your input file has double quotes or commas the resulting output will not be valid CSV. There are many modules on CPAN that can help with CSV, Text::CSV_XS is an example:
    use Text::CSV_XS; my $csv = Text::CSV_XS->new; while(<DATA>){ chomp; my @columns = split /\t/; $csv->combine( @columns ) or die "could not combine due to ",$csv- +>error_input()," in input\n"; print $csv->string, "\n"; } __DATA__ first second "hello" 100,000 bye He said "bye"
    Outputs:
    first,second """hello""","100,000" bye,"He said ""bye"""
    Whereas using tr/\t/,/ will give you:
    first,second "hello",100,000 bye,He said "bye"
    which is not valid CSV.
Re: Using Substitution Operator In Perl Script
by andyford (Curate) on Aug 28, 2007 at 20:36 UTC

    I think that putting "use diagnostics" at the top of your programs would help you a great deal. The diagnostics will explain in detail the error messages that you encounter. I find it quite convenient.

    non-Perl: Andy Ford