4 ms·
>Perl shepherds you into using regexps...Note that there is nothing about Perl that forces you to do this. It provides all the tools needed to do the right thin
by tyingq 5y ago
>Perl shepherds you into using regexps...Note that there is nothing about Perl that forces you to do this. It provides all the tools needed to do the right thing.
It does have index() and rindex(), but doesn't have a begins_with(), ends_with(), and other string functions that Python, Ruby, PHP, java, etc, have. You can roll your own, but there are arguably things missing.
- wazoox 5y agoIt actually has this in a more powerful form: you'd use /^./ or /.$/ of course, and if you want more explicit writing, you can use Regexp::English that provides the kind of functions you like (see https://metacpan.org/pod/Regexp::English https://metacpan.org/pod/Regexp::English ). The thing is that all of these functions are completely superfluous, because a Perlist will use regexp which are much more compact and allow writing in a nice, functional way (map and grep FTW). In fact, in most languages, manipulation of strings is so cumbersome that I go back to Perl almost every time, because a regexp is almost always the most compact, fast and readable solution. Also frankly I never heard of anyone trying to parse XML or HTML with regexps this century for anything but a quick throwaway one-liner, so the example is actually quite poor.
- tyingq 5y agoYeah, I was answering this from the article: "Note that there is nothing about Perl that forces you to [use regexes]...It provides all the tools needed" I was saying that Perl does sort of force you down the regex path, because things like starts_with() aren't there. You're basically confirming that. Also, it varies by search string size, version and machine type, but at least for me, index() is significantly faster in many situations...2 to 3x faster.
- wazoox 5y agoBut I don't want functions such as start_with() or startWith()! I'd much rather use regexes, which are more powerful and more expressive, than a mish-mash of tests like if ( (myvar.startsWith('hello') and myvar.endsWith('world')) or ((myvar.startsWith('Hello') and myvar.endsWith('World'))) {...} I much prefer if ( $myvar =~ /^[H|h]ello.*[W|w]orld$/ ) { } Perl doesn't have these functions because it has better, more powerful tools. It's akin to saying that Python lacks line numbers as GW-BASIC did...
- tyingq 5y agoThey are more powerful and expressive, but are also less efficient and significantly slower for simple things like starts/ends_with(). Benchmark index("abc","ab")==0 vs "abc" =~ /^ab/
- deleted 5y ago[deleted]
- wazoox 5y agoThis is a canonical case of premature optimisation. I have performance problems with the database backend, the network, disk access, but too slow a regex is a completely non-existing problem in real-life for me. Or else I wouldn't be using Perl/PHP/Python/Ruby but C/C++/Rust/Java etc.
- tyingq 5y agoMaybe. Batch processing huge text files is pretty common in Perl.
- AnimalMuppet 5y agoWhat's the cause, and what's the effect? I reach for Perl when I have that kind of a problem, because Perl makes it easier than anything else I've ever seen. Does Perl push me to program that way? Or is it the kind of problem that I program in Perl that pushes me to program that way?
- tyingq 5y agoThat's true, though Perl was around when there weren't a whole lot of choices in languages too.