Language for Unix command line utilities?
6 points| gn | 15 years ago
Some years ago we decided to move from Perl to Python for new projects because Perl programs had a way of always ending up as maintainability nightmares and because Perl seemed on the way out anyway. It largely worked, but we were never really, truly happy with our Python code. I suspect part of the reason is that Python can be (or at least feel) less succinct than even C if you do lots of low-level file system stuff with close error checking. The true reason is probably largely aesthetic. We can't explain what's wrong, we're just vaguely uneasy.
What other alternative to C should we be looking at? Ruby? Haskell? Is Go there yet? We have very open minds and are willing to consider pretty much anything that gives us reasonably easy and unmolested access to syscalls and their return values.
gaius|15 years ago
OCaml would be a good choice too. Both of these languages work very naturally with tree-like structures. Profiling/code coverage in both is very easy. IMHO there's no need to go to C for any but the most performance-critical code (and remember that your I/O etc is already in C in the kernel). The C approach of checking the return value of every syscall (e.g. no exceptions) is very cumbersome.
Case in point today: rather than persuade our Unix guys to roll out Expect across a bunch of new machines, I rewrote a ~200 line Expect script I had in ~60 lines of Haskell and deployed a binary instead of a script.
mblakele|15 years ago
aidenn0|15 years ago
I mostly use Python for the sorts of things you are mentioning. And from what you're saying you don't like a bout Python, I suspect that going to Ruby or Haskell or such is going to be worse. Python can more easily call the underlying C routines then either of those.
It would be nice if you could provide an example of something you think is inelegant and/or awkward in Python so that we could figure out which direction to point you.
I do a lot of coding in common lisp and some programming in haskell, but wouldn't recommend either of those based on what I've heard from you so far. There's a few dataflow style languages I've seen that would probably allow very succinct code, but they were all toys and performed quite poorly.
gn|15 years ago
For me personally the main source of unhappiness is error messages. In C I can say
if (!(f = open(name, "r"))) die(name);
where die is a tiny function that prints name, followed by whatever strerror has to say to the subject, formatted in the usual fashion. One line, done with it. The obvious, conventional Python equivalent is four lines long because both try: and except: insist on a line of their own. Since I cannot tell Python to produce succinct unixy error messages instead of rambling stack traces I have to catch and examine more or less every plausible exception. Some exceptions I can deal with close to the base level of my call stack in a butt-ugly fourty-line catch-all clause but a large proportion of my syscalls end up taking three lines extra each. I know it's a trivial problem, but I agree with pg you tend to get the more productive the more of your actual application logic you can see.
> I do a lot of coding in common lisp
We did experiment with clisp a while back; it turned out not to be a natural fit for problems that involve a lot of pathname, datetime, and stat info manipulation. If there was a reasonably modern Lisp that let me say things like (localtime (nth 9 (stat "/foo"))) I would go looking for it this very afternoon.
ggchappell|15 years ago
That's an interesting statement. Certainly, Python can be less succinct than Perl, particularly for small scripts where a quick "while(<>) {" and a regexp get most of your work done. But C??
> ... if you do lots of low-level file system stuff with close error checking.
Hmmm. In my experience, C's I/O libraries tend to make error checking something we leave by the wayside. Is there any chance that the real reason your Python scripts are longer, is that you actually check for, and properly handle, the errors there, while in C, you often don't?
In any case, I'll echo a comment from aidenn0:
> It would be nice if you could provide an example of something you think is inelegant and/or awkward in Python so that we could figure out which direction to point you.
chromatic|15 years ago
CyberFonic|15 years ago
konad|15 years ago
See the File example http://golang.org/doc/progs/file.go?h=syscall