How about this: (the same but "unrolled")

my @elements;
push @elements, $1 while
   /\G\s*"([^\\"]*(?:\\["\\][^\\"]*)*)"/gc or
   /\G\s*'([^\\']*(?:\\['\\][^\\']*)*)'/gc or
   /\G\s*([^\s'"]\S*)/gc;

is there actually an advantage to doing this?

-----Original Message-----
From: Randal L. Schwartz [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, June 06, 2001 10:43 AM
To: Ondrej Par
Cc: Peter Cornelius; [EMAIL PROTECTED]
Subject: Re: splitting strings with quoted white space


>>>>> "Ondrej" == Ondrej Par <[EMAIL PROTECTED]> writes:

Ondrej> On Wednesday 06 June 2001 18:19, Randal L. Schwartz wrote:
>> That's a good approach, but maybe this one is more straightforward:
>>
>> $_ = q{whatever "this" 'line is'};
>>
>> my @elements;
>> push @elements, $1 while
>> /\G\s*"(.*?)"/gc or
>> /\G\s*'(.*?)'/gc or
>> /\G\s*(\S+)/gc;
>>
>> print map "<<$_>>", @elements;
>>
>> The use of scalar /\G...../gc to inchworm along a string is a powerful
>> technique.

Ondrej> Yes, this is better. With one exception - you're not handling \' and
\" (but
Ondrej> this can be copied from previous example).

my @elements;
push @elements, $1 while
  /\G\s*"((?:[^\\"]|\\"|\\\\)*)"/gc or
  /\G\s*'((?:[^\\']|\\'|\\\\)*)'/gc or
  /\G\s*([^\s'"]\S*)/gc;

Leaving undefined something like \X as malformed. :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl
training!

Reply via email to