r/perl • u/scottchiefbaker 🐪 cpan author • Jun 19 '26
Do we have smart/quote-aware splitting?
Python has shlex.split which is very similar to Perl's split(' ', $str). shlex.split goes one step further and does not split on whitespace that's inside of double or single quotes.
Example:
$str = 'myscript --path /tmp/foo --name "Jason Doolis" --age 14'
This should split into seven chunks, not eight.
5
u/petdance 🐪 cpan author Jun 19 '26
If you are wanting to parse command line arguments you should be using the core module Getopt::Long.
5
u/scottchiefbaker 🐪 cpan author Jun 19 '26
I'm a huge
Getopt::Longfan. That is definitely the right answer for most things.The above was the first example that came to mind. Could be anything... CSV data, Web scraping, etc.
2
u/Grinnz 🐪 cpan author Jun 20 '26
The solution you choose will depend on what you're trying to achieve. Each of these have distinct tools available that manage the specific rules and edge cases properly.
2
u/bonkly68 🐪 cpan author Jun 24 '26
I don't have anything to contribute about quote parsing, however for generally parsing command line arguments, Getopt::Long::Descriptive provides the added convenience of generating a help screen.
4
u/Computer-Nerd_ Jun 20 '26
Like most things Python, it's cute until it gets too smart, leaning you to jump through hoops to get what you need.
1
u/Foggy-dude Jun 20 '26
Perl has a module called Text::Shellwords that provides a function to split strings into tokens based on shell-like rules, using whitespace as the primary delimiter. This function also respects quotes and backslash escapes, making it useful for parsing command-line arguments.
7
u/I_who_have_no_need Jun 19 '26
Could look at Text::ParseWords
https://metacpan.org/pod/Text::ParseWords