$str = q{split on whitespace "but leave" quoted "spaces alone"}; @chunks = get_chunks($str); # desired return value: # ('split', 'on', 'whitespace', 'but leave', 'quoted', 'spaces alone') # it's hard to do it with split(), but it CAN be done! # method 1 # note: this leaves the "s in... sub get_chunks { my$s;split/"(?{$s=!$s})(?!)|\s+(?(?{$s})(?!))/,pop } # method 2 # note: this removes the "s (preferred) sub get_chunks { my$s;@_=split/(?(?{$s})"\s*(?{$s=0})|(?:\s*"(?{$s=1})|\s+))/,' '.pop;shift;@_ }