in reply to splitting on multiple delimiters
It's clunky, but it works, and it preserves the logic for future generations to understsand.$str = 'command1 command2 command3 "command4 --some-arg arg --some-oth +er-arg 2" command5'; @words = split(/\s/, $str); $arg_flag = 0; for $i (0 .. $#words) { if ($words[$i] =~ /\"/) { if ($arg_flag == 0) { #starting a new command with args $arg_flag = 1; $words[$i] =~ s/\"//; $this_command = $words[$i]; } else { #ending a new command with args $words[$i] =~ s/\"//; $this_command .= " ".$words[$i]; push (@commands, $this_command); $arg_flag = 0; } } else { if ($arg_flag == 0) { #any old command push (@commands, $words[$i]); } else { # an arg in a command being built $this_command .= " ".$words[$i]; } } }
|
|---|