5 ms·
My main problem with Perl is references and contexts. Most of the experienced programmers I work with find themselves unable to write or debug code that requir
by EliAndrewC 17y ago
My main problem with Perl is references and contexts. Most of the experienced programmers I work 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]\n" # 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.
- berntb 17y agoThe reference stuff is natural to old C programmers, if they think a bit. It is ugly, but will just take some work hours to learn. All that is dwarfed by learning the language specific libraries, no matter which language you use. And by the way, use strict/warnings, so you aren't bitten by a spelling error for variable names. :-) Edit: BTW 2, how do you do one-liners in Python? :-)
- kingkongrevenge 17y ago> push @{$xs->[3]}, 6 # omgwtf?! Maybe the first time you see it, for at most two minutes once in your entire life. I kind of wonder if the people who freak out about this syntax stuff have ever studied a foreign language. Do they scream and run out of the room every time they hit an alien grammar construct?
- Goladus 17y agoIt still slows you down if you don't use it all the time. Unless you're someone who has been programming in perl every day for years at a time, then that line is most assuredly not something that takes 2 seconds to learn and you'll remember forever. Seeing something like that while you are reading code means you have to STOP what you were reading, patiently sort it out the syntax and refer to the documentation to be sure you haven't made any mistaken assumptions, and THEN move on. Even if that only takes a couple of minutes, it adds to the frustration level of reading code. And the point is that it is perl-specific. It doesn't happen with most other code that I have to read.
- kingkongrevenge 17y ago> if you don't use it all the time It's core to the language; very common usage. Context overloading is very core to the language. I don't know what kind of programming you could do that wouldn't involved references used in this way. If you want to talk about the rough edges, I can do that. I actually know and use Perl. But when people who clearly have not invested much time with the language try to criticize it's always idiotic. I mean, let's talk about the 'each' operator or the archaic format stuff. The basic syntax of the language is fine; you just don't know it.
- berntb 17y agoAs I wrote in another place, the main problem with Perl is the trolls, like Godalus seems to be. All that garbage has really lessened any interest in Python and Ruby for me. I really don't want to join communities which have so many members doing so large amounts of hype, language wars and trolling.
- jrockway 17y agoAll that garbage has really lessened any interest in Python and Ruby for me. I really don't want to join communities which have so many members doing so large amounts of hype, language wars and trolling. So true. Perl people are not immune to this, though; I have seen a lot of snipes at Haskell and Common Lisp in various Perl forums. People see a threat to their way of life, and do everything possible to maintain the status quo. (The "complaints" are always about the same sorts of things, too. Perl sucks because it has sigils, Haskell sucks because I don't know what a functor is, and Common Lisp sucks because you can't get into the first element of a list and drive it. These are rarely problems with the languages, and more typically problems with the commenter.) It is funny, because Perl has been around longer than Python and Ruby, and Common Lisp (in some form) has been around longer than Perl. If the designers of Python and Ruby had done their job, there wouldn't even need to be a debate, we would all be using whichever one was newest.
- nkurz 17y agoIn Perl this would be way more complicated Does it need to be more complicated, or are you perhaps trying to make the code look like Python? While there are good reasons for using references to pass lists to functions, it seems clearer (and more Perl-ish) to do the transformations on a list rather than a reference: my @xs = (5, 6); # use a list directly push @xs, 7; print $xs[2]; push @xs, []; push @{$xs[3]}, 6; # treat $xs[3] as array some_func(12, "hello", \@xs, 1); While one could probably argue that pushing to the inner array is clearer in Python, the rest seems just as clear. And if one was writing for an audience not comfortable with references, one could always use a temp variable for clarity: my @inner; push @xs, \@inner; push @inner, 8; For that matter, you could do the same for your 'omgwtf' line: my $inner = $xs->[3]; push @$inner, 9; While there's nothing wrong with the Python, they strike me as pretty comparable in clarity.
- Goladus 17y agoThe point of his example was to show why reference syntax is a pain. "# Use a list directly" is not what happens in a lot of code that I have to read.
- systems 17y agoyou can use any of the Perl OO systems and replace push @$xs, 7; with $xs->append(7);
- bkovitz 17y agoThat's a wonderful thing about Perl, but we're talking about readability here. To read Perl code in practice (written by people other than yourself), you need to know an extraordinary number of Perlisms. This is the curse of TMTOWTDI.
- systems 17y agonot really extraordinary, unless you only know one language! most programmers know half a dozen languages, and another half-dozen technologies and tools Plus the fun is in the details ;) I love languages with depth in them!