3

I'm using parse_line from Text::ParseWords to parse a line of text. However, when there is an unescaped double quote (") inside a pair of double quotes, parse_line fails.

For example:

use Text::ParseWords; 
...
my $line = q(1000,"test","Hello"StackOverFlow");
...
@arr = &parse_line(",",1,$line);

I don't want to escape the inner double quote (e.g. "Hello \"StackOverFlow").

Is there any other way to parse the line?

ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
voiceboy
  • 39
  • 2
  • 2
    If you have csv data, perhaps `Text::CSV` might allow loose quotes with some option. – TLP Jan 12 '16 at 17:00
  • 3
    @TLP Bingo. [`allow_loose_quotes`](https://metacpan.org/pod/Text::CSV#allow_loose_quotes) plus set `escape_char` to something other than `"`. – ThisSuitIsBlackNot Jan 12 '16 at 17:06
  • Side-note unrelated to your problem: you shouldn't use `&` when you call subroutines unless you know what it does and have a good reason for it. See [When should I use the & to call a Perl subroutine?](http://stackoverflow.com/questions/1347396/when-should-i-use-the-to-call-a-perl-subroutine) – ThisSuitIsBlackNot Jan 12 '16 at 21:31

1 Answers1

0

Using @TLP and @ThisSuitIsBlackNot notes:

use 5.022;
use Text::CSV;
use Data::Dumper;
my $line = q(1000,"test","Hello"StackOverFlow");

my $csv = Text::CSV->new( {allow_loose_quotes => 1, escape_char => '%'});
$csv->parse($line);
my @fields = $csv->fields();
print Dumper \@fields;


__DATA__
$VAR1 = [
          '1000',
          'test',
          'Hello"StackOverFlow'
        ];
SparkeyG
  • 532
  • 7
  • 22
  • Thanks you very much. But I need to use the parse_line function. is there any way the parse the " between the " " into \". Just like "Hello"StackOverFlow" ====> "Hello\"StackOverFlow" – voiceboy Jan 14 '16 at 16:19