by deckard1 · 5y ago · 0 comments
> It combines all the worst aspects of C and Lisp: a billion different sublanguages in one monolithic executable.
Believe it or not, I used to be a Perl hater and a Lisp (or Scheme) weenie. But having worked with Perl, Common Lisp, Ruby, NodeJS, and Scheme, I really have to say that I'd take Perl over all of those. The worst aspect of C would be pointers and unsafe memory access, which Perl doesn't have. And the worst parts of Common Lisp would be the massive, inconsistent, designed-by-committee language, CLOS, and macros. Oh, and LOOP (I shouldn't need to dig out a reference book to remember how to do looping each time I need to. Seriously.) S-expressions have also worn their welcome on me. I'm just tired of brace matching (yes, even with your silly fancy IDE).
Perl is by no means perfect, but I've found it does more things right than wrong. Blaming Perl for regexes being useful is like blaming a hammer for nails being ubiquitous. What? You're going to hand code a lexer each time you need to parse a phone number or a zip code? If there was a better alternative we would be using it.
For a while I was cursing the arcane syntax such as $_, $\ and even $ on a variable to the left of the equal sign...
and then something weird happened. Those things became idioms in my mind and I started achieving a sort of fluency.
And when I started to think in perl, I found it to be the highest-level language I had EVER worked in. I could express myself because there were multiple ways to do something and one always matched what I was thinking.
For example, instead of having to think IF NOT SOMETHING I could UNLESS SOMETHING and then express it as
unless(something) { do_something(); }
or even: do_something() unless something;
Now I like the idea of regular expressions, but I find the implementation leaves a lot to be desired. or should I say implementations plural.To me, the main usability problem with regular expressions boils down this: it is hard to distinguish between a literal and a regex control character.
This is exacerbated because regex control characters are usually the same characters other languages treat specially as well.
So you have regular expressions - which are somewhat straightforward, but you have to escape them for the place you are USING the regular expression - perl, shell script, sed, awk, grep or egrep, etc.
So I've used regular expression for decades, and even now it's always subject to a little trial and error and requires testing.
Anyway... if you’re interested in Perl, especially how to write nice Perl, I encourage you to get yourself a copy of Perl Best Practice by Damian Conway for Christmas
When Python started gaining traction, I was so incredibly turned off by the fans of the language that I swore I would never write code in it. That was 1998. If Python was 10% as good as those people were claiming, it would be humankind's greatest achievement for the next 1000 years, and it isn't.
Please note that I'm not saying anything bad about Python here, only its advocates in the late 1990s. I still have a bad taste in my mouth because of those conversations, over 20 years later.
(1) I have an i18n mapping which provides native support for a lot of helpful characters. Both Mac and Linux use all the keys, so in addition to the half-dozen characters with accents, I have access to a lot more directly. Mac and Linux do remap the remaining keys differently, though.
(2) My code got a lot more readable. Δx is easier to read and more concise than delta_x.
(3) I have a lot more characters to pick from.
A lot of the issues you raise with regexp are due to limits of ASCII.
For more esoteric characters, I do Google and either enter alt codes or cut-and-paste, which might sound like , but remember that you write code once, and read it many times. And editors are increasingly good.
Beyond Unicode, one does have text formats with support for colors, bold, and whatnot, either embedded (escape codes in text) or as syntax highlighting (editor parses).
The CS-level reg exps seem like a good idea. The implementation -- where they're encoded as bizarre ASCII escape sequences -- could use a good revamp.
Oddly enough, I think this is my first HN post where I went beyond ASCII....
Edit: And HN filtered out a bunch of Unicode. It kept only the delta.
STOP PARSING WEB FORM INPUT. Please. For the love of God just stop.
This is why emails of the form "foo+bar@fakeaddress.myemail" rarely work. Somebody's regex didn't allow "+" and everybody copied it. If you really need to check an email address, send a verification email to it. It's the only method that actually works.
Why are you parsing a phone number. The user typed in a phone number, obey it. Are you sure that you know every combination of valid phone number?
Zip code ... maybe ... if you are US only. Otherwise a postcode can't be parsed either.
And this is before we talk about the Cardinal Sin of using regexes as parsers.
Also, don't store zip code as an integer. There are valid zip codes that start with zero that will likely get truncated if you store a zip code as an integer.
In a practical sense, it doesn't. But it's there if you specifically go digging for it:
$ perl -le 'print unpack("p", pack("p", "Hello, world."));'
Hello, world.
$ perl -le 'print unpack("p", reverse(pack("p", "Hello, world.")));'
Segmentation fault (core dumped)
$There's a reason Rakudo adopted them in its quest to out-Perl Perl (5).
Regex is fine as an adequately-powerful shorthand for command-line munging and text editors, where the terseness is useful. It turns on you as soon as you want to use it in a programming language... which is where Perl comes in.
Use a parser combinator library. I've yet to find a case where they weren't a better replacement for regexes.
I've always wondered what would happen if there was a CL standardization where everything was CLOS'ified in the :common-lisp package, so the #'+, #'map(can), etc. were generic functions along with a nice set of collections (dictionary, etc.). And the more specialized/efficient functions were squirreled away in a non-default package. Well that, and a bunch of other things like a different module system. SBCL seems like such a nice compiler that doesn't get the respect it deserves for being able to compile fast binaries for such a dynamic language.
It's hard to get people to switch from something they find good enough for their needs, if the new thing is incompatible. See: Perl 6.