Just the initial block indicates that he has missed the fundamentals about Perl.
while ($line ne "") {
# Skip spaces and start with empty field.
if (substr ($line,0,1) eq " ") {
$line = substr ($line,1);
next;
}
That should at least be written using a regular expression to remove leading white space. I like the answer from jrockway best, modules rock. Though I would have used regular expressions to do it, something like.
#!/usr/bin/perl -w
#
# $Id$
#
use strict;
open(FD, "< qq.in") || die "Failed to open file.";
while (my $line = ) {
# Don't like chomp.
$line =~ s/(\r|\n)//g;
# ".*?[^\\\\]" = Match everything between quotations that doesn't end with
# an escaped quotation, match lazy so we will match the shortest possible.
# [^",]*? = Match strings that doesn't have any quotations.
# If we combine the two above we can match strings that contains quotations
# anywhere in the string (or doesn't contain quotations at all).
# Put them together and match lazy again so we can match white-spaces
# and don't include them in the result.
my $match_field = '\s*((".*?[^\\\\]"|[^",]*?)*)\s*';
if (not $line =~ /^$match_field,$match_field,$match_field,$match_field$/) {
die "Invalid line: $line";
}
# Put values in nice variables so we don't have to deal with cryptic $N
# (and can use $1 in replace).
my ($user_id, $name, $level, $numeric_id) = ($1, $3, $5, $7);
print "$line\n";
for my $field ($user_id, $name, $level, $numeric_id) {
# If the field starts with a quotation,
# strip everything after the first unescaped quotation.
$field =~ s/^"(.*?[^\\\\])".*/$1/g;
# Now fix all escaped variables (not only quotations).
$field =~ s/\\(.)/$1/g;
print " [$field]\n";
}
}
close FD;