Skip to content

Instantly share code, notes, and snippets.

@EliAndrewC
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EliAndrewC/29188163fa4c6cb69dd3 to your computer and use it in GitHub Desktop.
Save EliAndrewC/29188163fa4c6cb69dd3 to your computer and use it in GitHub Desktop.
What I dislike about Perl
My main problem with Perl is references and contexts. Most of the experienced programmers I've worked with find themselves unable to write or debug code that requires the use of these Perl concepts.
For example, my co-workers and I find it easy and intuitive to work with lists in Python:
xs = [5, 6]
xs.append(7)
print xs[2] # prints 7
xs.append([]) # easy to append lists to other lists
xs[3].append(6) # easy to append to the inner list
some_func(12, "hello", xs, True) # easy to pass to a function
In Perl this would be way more complicated for reasons that are obvious to someone who knows Perl well, but a mystery to people who use Perl casually.
$xs = [5, 6]; # must be a reference
push @$xs, 7 # deference $xs as a list
print "$xs->[2]" # still not too bad
push @$xs, [] # add a nested list reference
push @{$xs->[3]}, 6 # omgwtf?!
some_func(12, "hello", $xs, 1) # not too bad
Understanding "push @{$xs->[3]}, 6" is asking a lot just to deal with a nested list that you need to pass to a function, especially compared to the easily understandable "xs[3].append(6)". For that matter, it's asking a lot to make you understand the difference between a list and a list reference. It's asking a lot to ask to understand the concept of "evaluating in a ___ context", especially when other languages manage to do the same things without these concepts.
Another thing about the above is how errors are handled. Suppose that in the Python code we said
xs[0].append(123)
We'd be trying to call the "append" method on an int, which would be a mistake and we'd get the exception
AttributeError: 'int' object has no attribute 'append'
which is fairly helpful. But suppose that we did the same thing in Perl:
push @{$xs->[0]}, 123
Do you know what happens when we run that line? Nothing. There's no error, but everything is unchanged. Here's what the Perl interpreter does:
First, it evaluates the scalar 5 in a list context. Evaluating a scalar in a list context creates a new list with a single element, which is the value of the scalar.
Second, it appends the integer 123 to the new list, which now contains 5 as its first element and 123 as its second element.
Third, it immediately discards the list since we didn't store a reference to it anywhere, so Perl just garbage collects it and nothing is stored/saved anywhere.
I've seen people lose hours and hours to issues like this. They have a line of code which is completely wrong, but the interpreter just silently does something nonsensical.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment