by nine_k · 7y ago · 0 comments
Perl is a great experiment in language design, or, rather, a battery of experiments. It's the opposite of a cleanly designed language.
One of the places this shows up to me is being able to say "it" (in essence). That is, we humans, when talking to each other, we say things like "Read in a line of input. If it looks like an XML tag, pass it to the XML handler function". But you can't talk to a computer that way. The computer says, in effect, "Read in a line of input from where? And put it where? And what do you mean 'if it ends in a newline'? If what ends in a newline?"
But in Perl, you can write it just like the humans say it, and Perl says, "Well, you didn't say where to read the input from, so I assume I should read it from the standard place (which is perfectly well defined in Perl). You didn't say where to put the line of input, so I'll put it in the default variable. You didn't say what to check to see if it looks like an XML tag, so I will check the default variable." The default variable, essentially, plays the role of "it" in human language.
And the whole language is like that. There are shortcuts that work most of the time, and precise ways of saying things that work when the shortcut isn't what you want. And what the shortcut does is well defined (in the docs; it can be very opaque in code).
None of this makes Perl an ideal language. But it has a perspective on language that is (as far as I can tell) unique among programming languages. It is only fair to judge the coherence of the design within the framework of what Perl is trying to be.
The attempt to mirror human language is in the flexibility of the constructs. This is the much maligned There Is More Than One Way To Do It (TMTOWTDI) principle.
The language is designed to allow the programmer to express themselves in they way that makes the point best for their brains.
Now, this is almost a classical debate in programming at this point but it was really in high gear during the late 90s as Python's One Way approach quickly proved more useful for larger codebases, in contrast to the personalized anarchy of styles often found in a large Perl project.
Perl 6 changes the landscape of "personalizable programming language" considerably, making it less complex in some ways and more complex in others.
Whether or not TMTOWTDI is a boon or a bane to you is your choice. I find that having a language that is designed for personal expression is precisely the thing I want when I am reaching for scripting glue.
Actually, I find it maps rather directly to how a lot of people explain things. For example, "Examine each number, and as long as it's greater than 10 and odd, then pass it to the the work function."
for my $number ( @numbers ) {
next unless $number > 10;
next unless $number % 2;
work($number);
}
Perl generally allows you to structure your code as you would explain how to accomplish something to another person. This can be beneficial or detrimental depending on how deep you go with this concept. A lot of becoming a good Perl programmer is learning where a sane line is for this, but each language usually has a similar feature which the user needs to decide where the sane point to stop is before taking it to an extreme that is detrimental.There are definitely aspects of Perl that I think are better left untouched, and thankfully the community has pretty much come to consensus about the most problematic missteps the language allows.
for number in numbers:
if number <= 10: continue
work(number)
Do you have a better example where the Perl syntax would shine? for @numbers -> $number {
next unless $number > 10;
next unless $number % 2;
work($number);
}
Although personally, I wouldn't write it like that: I would probably write it as: for @numbers.grep( { $_ > 10 && $_ % 2 } ) -> $number {
work($number)
}
or use the postfix notation: work($_) for @numbers.grep( { $_ > 10 && $_ % 2 } );
If you want to spread this out over multiple CPUs and you don't care about the order in which the work is done, you only need to add the `.race` method: for @numbers.grep( { $_ > 10 && $_ % 2 } ).race -> $number {
work($number)
}
More info: https://docs.perl6.org/routine/race numbers.into_iter().filter(|n| n>10 && n%2!=0).for_each(work);
map work . filter ((/=0) . (`mod` 2)) . filter (>10) $ numbers
Insert tirade about how for-loops are free to do so much that they are slower-to-comprehend than specific-purpose iterator/list functions, here. for @numbers -> $number {
next unless $number > 10;
next if $number %% 2;
work($number);
}Also, "cryptic charade" could be a good name for what all non-Perl 6 languages call "regular expressions". It would make an even better name for a prog rock band.
Source code is instructions for two audiences: A human, and a computer. The computer doesn't care what the syntax is, but the human does. It is an ongoing challenge to write code in a manner which effectively and concisely communicates to other humans, including your future self. This is a thing at which Perl5 and Perl6 both excel, Perl6 more so IMO (although there is not yet enough history to prove it).
It is true that one can easily write unmaintainable spaghetti in Perl5. One can write unmaintainable spaghetti in any language, but Perl5 makes it somewhat easier to do it by mistake--this is an aspect of the same versatility which gives it its expressive power, which in turn is what makes it possible to communicate things using Perl more effectively (in the same space) than with many other computer languages. You have to employ some discipline not to sound like a gibbering lunatic. This is not much different from human languages: if you have ever read English 101 papers you will find some of them sadly similar to Perl5 scripts written by similarly undisciplined authors.
In contrast to Perl5, Perl6 makes it far easier to avoid accidental spaghetti and does so without sacrificing any of the linguistic expressiveness that makes Perl generally so useful.
Here's 'it' in lisp: https://en.wikipedia.org/wiki/Anaphoric_macro
https://docs.perl6.org/language/variables#index-entry-topic_...
my @menu = <hamburger fries milkshake>;
say @menu.contains('hamburger'); # True
say @menu.contains('hot dog'); # False
say @menu.contains('milk'); # True!
say @menu.contains('er fr'); # True!
say @menu.contains(<es mi>); # True! # Let's drop `<>` (quote-words constructor) and
# use a more familiar array of strings:
my @menu = 'hamburger', 'fries', 'milkshake';
# Let's look at the definition of the `Str` method in
# the `List` class from which the `Array` class inherits:
#`{
method Str(List:D: --> Str:D)
Stringifies the elements of the list and joins
them with spaces (same as .join(' ')).
}
# Thus, after being stringfied, @menu is treated as
# the string 'hamburger fries milkshake'
# With this is in hand, we can gauge the possible
# result of the following statements:
say @menu.contains('hamburger'); # True
say @menu.contains('hot dog'); # False
say @menu.contains('milk'); # True
say @menu.contains('er fr'); # True
say @menu.contains(<es mi>); # True
If you'd like instead to search the array for
words as elements, we could use Perl 6's set
functions. However, I don't know if they're as
performant as regular string searches: say 'hamburger' (<=) @menu; # True
say 'fries' (<=) @menu; # True
say 'hot dog' ∈ @menu; # False
say 'milk' ∈ @menu; # False
say 'er fr' ∈ @menu; # False
`∈` is the unicode equivalent of `(<=)`.---
I'm not sure if I'm missing something in the "odd design choices" in this specific case so it'd be great if you could elaborate a little bit further here.
my @menu = <hamburger fries milkshake>;
say so @menu.grep('hamburger'); # True
say so @menu.grep('hot dog'); # False
say so @menu.grep('milk'); # False
say so @menu.grep('er fr'); # False
say so @menu.grep(<es mi>); # FalseIt should have used a different word.
A similar example would be the word `length`.
There is no `length` in Perl 6 because it is ambiguous. Instead there is `.elems` and `.chars`.
It is perhaps even a worse because there are other languages which use `contains` to see if the container has a given element.