-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First draft of the "humane" pl article
- Loading branch information
1 parent
8856860
commit 0d1e0ce
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
Author: perl@dnmfarrell.com | ||
Title: Perl - The Humane Programming Language | ||
Topic: | ||
|
||
state of the onion first three talks | ||
people are different | ||
|
||
=encoding utf8 | ||
|
||
=head2 Perl - The Humane Programming Language | ||
|
||
Human languages follow the L<Law of Brevity|https://en.wikipedia.org/wiki/Brevity_law>, where the most commonly used words are also the shortest. Perl aims for brevity too. Its keywords tend to be shorter than the traditional versions: | ||
|
||
=begin code | ||
|
||
Traditional Perl | ||
----------- ---- | ||
break last | ||
continue next | ||
filter grep | ||
function sub | ||
import use | ||
let my | ||
throw die | ||
|
||
=end code | ||
|
||
Often entire words can be omitted. Subroutine declarations do not require a C<return> statement, and may be called without parens. Built-in functions can operate on default variables like C<$_> instead of an explicit arg. This code loops over each line of input and splits the tab-separated input into an array, without ever referencing the input variable: | ||
|
||
=begin perl | ||
|
||
while (<>) { | ||
chomp; | ||
my @cols = split /\t/; | ||
... | ||
} | ||
|
||
=end perl | ||
|
||
> People like things to be visually distinct from their surroundings. That's also why the various classes of operators and variables in Perl are visually distinct from each other. It's just sound human engineering. | ||
> Larry Wall | ||
> | ||
|
||
By making variables, operators and function names visually distinct, Perl saves you reading time too. | ||
|
||
=head3 Do What I Mean | ||
|
||
Perl might be the most dynamic of the dynamic languages. Take this pseudocode: | ||
|
||
=begin code | ||
|
||
foo = "100" | ||
bar = foo + 1 | ||
|
||
=end code | ||
|
||
Python and Ruby error on code like this. Since they resolve C<foo> to a string type, they refuse to concatenate it with the number 1 later on. | ||
|
||
In human languages, context can change the meaning of the expression. Similarly, Perl uses context to change the value of an expression. The C<+> operator forces a numeric context on its arguments, the concatenation operator C<.>, a string context. Perl uses context to do what you mean. | ||
|
||
=head3 Trust Me | ||
|
||
> Likewise, Perl is designed to let you program naturally. Whatever you think natural means. | ||
> Larry Wall | ||
> | ||
|
||
Perl doesn't enforce a particular paradigm on the programmer. Everything isn't an object, but you're free to use the OO paradigm if you want to. There are no "private" attributes or methods. Any module, object or data structure can be printed, traversed and manipulated. | ||
|
||
Want to inspect an object to see what it's made of? Use L<Data::Printer>: | ||
|
||
=begin perl | ||
|
||
use Data::Printer 'p'; | ||
|
||
my $foo = Foo->new; | ||
p $foo; | ||
|
||
=end perl | ||
|
||
Perl trusts you to solve the problem as you see fit. If you want to fire off a bunch of computation at compile time, you can. If you want to use 90% of a module but monkey patch that one method to behave how you need it, that's fine. You can achieve polymorphism via inheritance, traits, operator overloading and tied values. You might say, "There's More Than One Way To Do It" | ||
|
||
> Of all the programming languages I've used, Perl is the only one where I never feel like I'm fighting with the language. | ||
> Martin, Software Engineer | ||
> | ||
|
||
=head3 Lessons Learned | ||
|
||
Perl is an old language, with some features that seemed like a good idea at the time, but haven't panned out. Implicit variable declaration usually causes more trouble than its worth, so we use the L<strict> pragma to turn it off (Python and Ruby still suffer from it). | ||
|
||
Perl's interpreter threads are best left alone, and its reliance on global state makes implementing threading a tall order. Process concurrency is easy with L<Parallel::ForkManager> though, and L<IO::Async> is good for asynchronous programming. |