An advantage to introspection and an interactive interpreter
I spent part of today writing a very simple network server in Perl. While most of my problems were due to my ignorance, the experience did give me a new appreciation for introspection and interactive interpreters.
The problem is that without introspection, things are opaque when something goes wrong. What do you have? Certainly not what you expected, but it's hard to tell much more than that. With introspection, you can find out type information, maybe a printable representation of the thing (this gives you some idea if you're on the right track), and perhaps even lets you find out some of what you can do with it.
Perl has some introspection support, but it also has a tendency to make a lot of the socket things I was interested in into typeless, opaque strings. It was startling how different working in such a 'bare' environment felt.
An interactive interpreter taps introspection's power, because you can experiment without having to edit and restart programs. You grab the thing you're having trouble with, then fiddle around with it and see what happens; this gives you rapid feedback, so you can quickly refine and focus your fiddling and work out how to do what you want.
(Exceptions are also an advantage in this sort of exploration because they let you know immediately when you've done something wrong, and often give you specific details about what you screwed up.)
To answer the question 'why Perl (instead of, say, Python)': Solaris 9 has Perl has part of a relatively standard install, but not Python et al. And I would rather write simple networking programs in Perl than C.
Sidebar: introspection et al in Perl
Perl does have some addons for introspective and interactive interpreter features. The ones I've found and want to save for later reference:
- Perl's debugger ('
perl -d') can be used the introspect a number of things in a program, although not with straightforward syntax. You can sort of use it as an interactive 'interpreter' with something like '
perl -de 42', which starts Perl on a simple do-nothing expression. (Mentioned here.)
- psh, a simple interactive interpreter.
- This rather more complicated Perl shell thing, which seems to be at least partly an attempt at a Unix style shell as well as an interactive perl environment.
I'm honestly surprised that
psh or something like it isn't shipped
with Perl by now. (It's not as if
psh is very big or complicated.)
(Additional suggestions welcome.)