- What is an expressive language?
- What does it mean when someone says that Perl is an expressive language?
views:
511answers:
6An "expressive" language is a language that allows you to easily express logical concepts in code.
People often call Perl expressive since it allows you to use many different approaches to express a specific concept, so it's very flexible in this regard.
(There is a lot of debate about whether this is a good thing or not, though...)
They mean TIMTOWTDI.
The language was designed with this idea in mind, in that it "doesn't try to tell the programmer how to program". This makes it easy to write extremely messy programs, but, as proponents of this motto argue, it also makes it easy to write beautiful and concise ones.
In this context I think it means you can do a lot of things without writing too much code.
For example one-liners:
$ echo Foo | perl -pe "s/o/e/g"
Fee
Or Moose:
# A simple Point class with x and y coordinates.
package Point;
use Moose;
has [qw/x y/] => (is => 'ro', isa => 'Num', default => 0);
1;
Or the concept of context:
my @array = qw(foo bar baz);
my $count = @array; # three
Or the Schwartzian transform:
@sorted = map { $_->[0] }
sort { $a->[1] cmp $b->[1] }
map { [$_, foo($_)] }
@unsorted;
This is good, because you can have things done the way you want. (If you feel like shotgunning through the problem, you can.) On the other hand it’s sometimes bad, because you can have things done the way you want :)
Let's see, Merriam-Webster (s.v, 'expressive') offers this:
3: effectively conveying meaning or feeling 'an expressive silence', 'expressive line drawings'
I think that actually fits Perl to a T because of what other answerers are saying (1) there's more than one way to do things (so Perl has lots of ways to allow the programmer to express her ideas) and (2) Perl is often relatively compact (so you can express a lot with a little).
But I would add that Perl's pervasive tendency towards DWIM (do what I mean) is a big part of it as well.
The idea behind Perl's syntax is not only TMTOWTDI but an attempt to mimic natural language (as far as that's possible while keeping the precision of computer language). The goal is that Perl should be a language that you can become fluent in, like a native speaker, not like a tourist using a phrasebook.
One of the available ways to write the code should have a similar form to the way you think about the problem. When the shape of the code in the editor matches the shape of the problem in your head, you can get to the stage of having running and testable code more quickly, because there isn't a conscious step of translating between what you mean and what the computer wants to hear. This is why people will often tell you that Perl is excellent for prototyping.
It's this, not concision or TMTOWTDI, that's really meant by Perl's "expressiveness". Natural languages provide pronouns; Perl provides $_
. Natural languages provide ellipsis; Perl provides builtins with default arguments. Natural language provides lots of different ways to phrase any given statement, with different shades of meaning, and different modes and registers of speech; Perl does just the same with code. A native speaker of English will usually choose the most concise way to get a point across, out of all of the available ones, but he doesn't have to -- he has the option of using a lot more words for clarity, or even to be poetic. In Perl it's much the same. The goal, to speak unscientifically, is that you should be able to "say what you mean".
Some thoughts of Larry Wall's about the influence of natural languages on Perl: Natural Language Principles in Perl.
Read through Larry Wall's "State of the Onion" address (find them with Google). In the first several speeches he talks about the idea and philosophy of Perl and how he thinks about them. They can be quite enlightening for the newcomer to Perl.