154 points · by ColinWright · 5y ago · 117 comments
It's all oddly satisfying, in a way...
> 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
(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.
Pththththth.ps! PostScript is too readable. (But he's right I'm a monster raving loony.)
https://www.donhopkins.com/home/archive/NeWS/pizzatool.txt
Here's a PostScript Pretty Plotter (among other things):
https://medium.com/@donhopkins/the-shape-of-psiber-space-oct...
https://www.donhopkins.com/home/archive/psiber/cyber/litecyb...
Hunt the Wumpus I'm most proud of, but you need to run it on a PS-enabled printer, or in GhostScript. The BrainFuck interpreter, prime number calculator, and the larger quine all produce paper output on modern systems (just tested in Preview on macOS).
Of course others like elaborate closures and syntax that seems simple but ends up hiding tons of complexity about the data structures. To Each his own, I say.
https://news.ycombinator.com/item?id=13524588
Leigh Klotz's comment on the regex article:
>OK, I think I’ve written more PostScript by hand than Jamie, so I assume he thinks I’m not reading this. Back in the old days, I designed a system that used incredible amounts of PostScript. One thing that made it easier for us was a C-like syntax to PS compiler, done by a fellow at the Turning Institute. We licensed it and used it heavily, and I extended it a bit to be able to handle uneven stack-armed IF, and added varieties of inheritance. The project was called PdB and eventually it folded, and the author left and went to First Person Software, where he wrote a very similar language syntax for something called Oak, and it compiled to bytecodes instead of PostScript. Oak got renamed Java.
>So there.
>And yes, we did have two problems…
>— comment by Leigh L. Klotz, Jr. on June 7th, 2008 at 3:22am JST (12 years, 6 months ago) — comment permalink
Arthur van Hoff (the author of PdB and the original Java compiler written in Java) has also written more PostScript than Jamie, especially if you count the PostScript written by programs he wrote, like PdB and GoodNeWS/HyperNeWS/HyperLook.
Here's the README file (and distribution) of PdB, Arthur van Hoff's object oriented C to PostScript compiler:
https://github.com/IanDarwin/OpenLookCDROM/blob/master/NeWS/...
Also a paper by Arthur van Hoff about "Syntactic Extensions to PdB to Support TNT Classing Mechanisms":
https://www.donhopkins.com/home/archive/NeWS/PdB.txt
Some before and after examples, like menu.h menu.pdb menu.PS:
HyperNeWS 3.0 prototype source: https://www.donhopkins.com/home/archive/HyperLook/Turing/hn3...
menu.h: https://www.donhopkins.com/home/archive/HyperLook/Turing/hn3... menu.pdb: https://www.donhopkins.com/home/archive/HyperLook/Turing/hn3... menu.PS: https://www.donhopkins.com/home/archive/HyperLook/Turing/hn3...
GoodNeWS/HyperNeWS/HyperLook:
https://medium.com/@donhopkins/hyperlook-nee-hypernews-nee-g...
I'd love to see the turtle JMP.
And the original AWT, if this is to be a full Airing of Sins.
% WARNING WARNING! DANGER! DANGER WILL ROBINSON! DANGER!
% This is *gross* code. I mean UUUUUGLY! (And it used to be
% even more contorted, if you can believe that.)
...
% Brute force debugging hacks
% Ignore this stuff. It was written when I was very frustrated.
(% ifdef PISSEDOFF
...
% For use when mildly irritated:
...
% This is nasty evil vile implementation dependant hackery.
...
% Good god this has gotten bigger than dictbegin can handle!
XNeWS? not { 300 currentdict extend pop } if
...
% This code was designed to be rewritten!
...
% XXXX: Here be the start of the trouble.
...
MyProcess type /processtype eq {
pause pause pause % maybe it will kill itsself
...
% BOO HISS
Also it has a definition of quicksort.The suggestion of a WW2 soldiers origin (a la foo bar) in the article is more believable, though really I don't see why it should have not predated that as well.
Wasn't the quote from comp.religion.emacs?
But seriously, regular expressions are often ill-advised, as solutions based on them are frequently:
- over-matching: they match things in cases where they shouldn't, because a situation wasn't anticipated by the developer
- under-matching: not catching some input they should because a regular pattern was formulated to narrowly by the developer
- matching the wrong thing altogether because they are hard to stay on top of, even when it's one you wrote
- matching something, but you don't understand what and how it works, because regexes are "write only".
There is even another case:
- They work but they are the wrong instrument, because you try to model something that requires context-free generative capacity (e.g. syntax coloring)
Interestingly, at Xerox Research Centre Europe (now Naver Labs) in Grenoble a much more elegant (more powerful yet more readable, and symmetric between inputs and outputs) has been developed as an alternative, mostly to write linguistic rules with it.
For more info:
* http://users.itk.ppke.hu/~sikbo/nytech/gyak/05_morfo/xfst/bo...
* https://press.uchicago.edu/ucp/books/book/distributed/F/bo36...
* There is an open-source implementation called FOMA: https://fomafst.github.io/
http://wiki.call-cc.org/eggref/4/email-address
https://bitbucket.org/knodium/email-address/src/master/email...
It's a fair few lines of code but most of it comes directly from the RFC. It's also longer than it could be because it covers the entire RFC822 syntax, including groups, lists, routes, comments and To: header styles.
But in practice, no one does that. Because imagine grandma on the phone with her bank not remembering if her email address was Mary123@gmail.com, MARY123@gmail.com, or mary123@gmail.com. The horror.
For example, our system needs to handle container numbers[1] as part of an official form, and we check them for correctness to avoid rejection of the form.
As far as we're concerned the container number consist of four letters and seven digits. However the user is allowed to submit a container number with a hyphen or a space between the letters and the digits.
Writing a regex that checks for a properly formatted container number is quick, easy to understand and a lot less convoluted than most alternatives.
[WARNING: DO NOT CLICK THAT LINK OR YOU WILL BREAK THE INTERNET!]
With an email address, you almost always want a valid email address, and don't much care why it's valid.
So, sure, check for an @ in there, but then just... try and email it with a link, if the page gets visited, congrats, you have a valid email address.
It's true that those bits of eMail infrastructure are probably more robust but it's still strictly bad practice.
Even if you're "just" storing it in your database, you should sanitise it on the way in so that when someone does something "unexpected" with it, such as display it in a web browser UI, you're not going to suffer from injection attacks there either.
Apropos to OP, this article also quotes JWZ.
Normal regular expressions have been around since Kleene, Chomsky and Russell started framing them in the 40s/50s. They are a powerful tool for lexical analysis, PERL showed us that in the early 90's. Dismissing them is just gatekeepy, IMHO.
My estimated time to completion with regex is literally undefined
Or we might end up with https://xkcd.com/927/
The quote is fun, and I've sometimes used variants of it, but I don't get why the target is regular expressions.
The way that FSM is inherently a forward/backward process seems super elegant and it's more readable (at first read) than regex's.
Anyone have insight why they aren't more common?
Fascinating.
One of my favorite quotes from JWZ: "“In 1999 I took my leave of that whole sick, navel-gazing mess we called the software industry. Now I'm in a more honest line of work: now I sell beer.”
Some people, when confronted with a problem, think "I will create a branch." Now they have two problems.
> The nun Satou approached Java master Banzen and said: “There is a processing bottleneck which I believe I can eliminate by means of multiple worker threads. Yet I have heard this proverb: If you are confronted with a problem and say, ‘I shall use threads’, you now have two problems.”
... and it goes from there to explore it a bit.
https://www.oasis-open.org/committees/relax-ng/compact-20021...
RELAX NG compact syntax cheat sheet
https://gist.github.com/miikka/b137a8705e88b0b6c164
https://en.wikipedia.org/wiki/RELAX_NG#Compact_syntax
>RELAX NG compact syntax is a non-XML format inspired by extended Backus-Naur form and regular expressions, designed so that it can be unambiguously translated to its XML counterpart, and back again, with one-to-one correspondence in structure and meaning, in much the same way that Simple Outline XML (SOX) relates to XML. It shares many features with the syntax of DTDs. Here is the compact form of the above schema:
element book {
element page { text }+
}
>With named patterns, this can be flattened to: start = element book { page+ }
page = element page { text }
>A compact RELAX NG parser will treat these two as the same pattern.There's a wonderful DDJ interview with James Clark called "A Triumph of Simplicity: James Clark on Markup Languages and XML" where he explains how a standard has failed if everyone just uses the reference implementation, because the point of a standard is to be crisp and simple enough that many different implementations can interoperate perfectly.
A Triumph of Simplicity: James Clark on Markup Languages and XML:
https://web.archive.org/web/20130721072712/https://www.drdob...
"The standard has to be sufficiently simple that it makes sense to have multiple implementations." -James Clark
The first half is about using them, and the second half is focused on optimizing. You can skip the second half and still get a lot out of it, but it really is some fantastic stuff.
But by all means use them for a quick hack or script.
You don't have to go as far as pattern extraction and backreferencing and all that to make some good use of it.
SQL isn’t hard, but trying to remember all the use cases for the various ways to query something always trips me up. “Wait, do I do a LEFT JOIN here? Ok, now I gotta to look up the different kinds of joins so I can be sure I’m writing the most efficient query.”
Again, it’s just a matter of practice. I’d take SQL over a regex any day.
At the higher levels, it's definitely it's own programing language with a radically different herritage from the JS/C# I'm used to.
Personally, I've never liked SQL, though. As with regexes, a little does no harm. But building anything substantial with it always turns into what feel like absurd bank shots to me. I don't just have to cast everything into the relational paradigm, I have to try to guess how a couple million lines of DB server code is going to map that back into operations I actually understand.
That said, as with most technologies that I don't personally like, people seem to build sufficiently effective systems and profitable businesses on top of them. So I think it's down to personal cognitive differences and matters of taste. Let a thousand flowers bloom, I say.
To me, "lol" means "laugh out loud". So I ask, and it's a genuine question, did "alt.religion.emacs" really, honestly, make you "laugh out loud"? It may have done, but I'm interested to know.
The follow-up is this.
If it did make you "laugh out loud", what is it that you found funny?
If you didn't "laugh out loud", what do you actually mean by "lol"?
I'm interested in language, its uses, and how it's changing, and this is one case where I'm definitely in doubt as to the intended meaning of the comment.
Thanks.
Web scrapers aren’t too bad with perl regexps.
The https site returns 404 but the http site has the content. This breaks Firefox's preferences for https, as I keep get redirected from the http to the https site since the https site responds (albeit with a 404).