News for hackers. Stuff that matters.
Re:Perl is 25 years old today by 3amOpsGuy on Tuesday December 18, 2012 @05:21AM (#4936886)
Before i switched to Python as my general purpose language, i used to be a bit of a Perl fanatic.

I've still to come across a language that lets you get your ideas down as quickly. It's full of fantastic time savers, e.g. the for(<>) construct and the format output functionality cover a bunch of common use cases in minimal keystrokes (let me pipe in some data, analyse it, then spit it out in a clean report).

It can hurt readability but for small one offs, it often doesn't matter.

Re:Perl is 25 years old today by ajross on Tuesday December 18, 2012 @12:17PM (#4938430)
My experience is almost exactly the opposite. Whenever I'm writing in Python I can't escape the feeling that the code is simply fragile due to missing language features. Obviously this starts with all the regex initializations (often quite distant in source from where they are applied) which make regular expressions in python just a huge mess.

Perl's autovification allows me to build a data structure at parse time with code that is trivially verifiable. Python forces me to sprinkle the code with a thousand tests for whether or not the field is initialized yet, or write extra code to define things like defaultdict instances (with syntax that I always have to look up, and which in my experience most python programmers don't understand).

Not to mention that if I want to use an OO syntax for that data structure I have to write extra code defining a class for it (and inevitably be yelled at by the pythonista security branch for all the __whatnot__ methods I forgot to define).

Python's comprehension/generator syntax is nice, though, and doesn't have a clean analog in perl. One thing it does do well is chaining up "filter" operations on aggregate data structures in a clean way. And that has value and is worth emulating. But honestly most of the rest of the language is junk compared to perl or ruby.

Re:Perl is 25 years old today by asher on Tuesday December 18, 2012 @11:31PM (#4941151)
I agree with you about the feeling of fragility, especially with defaultdicts.

As for regexes - while they are somewhat clumsy in python, they don't have to be defined distantly:

  m = re.match(r'(\d{4})', buf)
  if m:
    print "year: %s" % m.group(1)
The only thing that really bothers me about the above is the need to separate the assignment of m from the test.

Re comprehensions: seems to me they are just Python's version of Perl's map and grep.

grep:

  @good = grep { $_->{width} > 9 } @crates;
  good = [c for c in crates if c['width'] > 9]
map:

  @squares = map { $_ * $_ } @nums;
  squares = [i*i for i in nums]
Which is better? I think Ruby's way is the best, and Perl/Python are a wash here.

Now, on the autovivification note, I wonder if we can make a Python class that has the desired behaviors. Like:

  c = PerlishStruct()
  c['foo'][17]['bar'] = 'baz'
You don't really think it's junk, do you? Perl, Python and Ruby are extremely similar; I'd say Perl's more ergonomic, but messier; Python's cleaner, but sometimes awkward, and Ruby pretty much has the best of both worlds.
Re:Perl is 25 years old today by berntb on Wednesday December 19, 2012 @01:45AM (#4941482)
You probably shouldn't do this in an inner loop: :-)

  m = re.match(r'(\d{4})', buf)
'map' is more general than just the single, convoluted statement of list comprehensions, which really seems as a kludge from unwillingness to accept multi-line lambdas. So something new was invented that had to be learned; then it was argued it is a boon. (Like references in Perl. :-) )

(There is some defaultdict functionality for autovivification in Python iirc.)

Yeah, they are all similar. I don't know enough Ruby to have an opinion.

Re:Perl is 25 years old today by hercynium on Tuesday December 18, 2012 @05:31PM (#4939994)
For list comprehensions and a different style of generator, look at https://metacpan.org/module/List::Gen

And then there are a pile of modules that implement python-style generators, some using https://metacpan.org/module/Coro (co-routines implemented as threads), some using crazy hacks:

https://metacpan.org/module/Compile::Generators

https://metacpan.org/module/Coro::Generator

https://metacpan.org/module/Attribute::Generator

Re:Perl is 25 years old today by ajross on Tuesday December 18, 2012 @08:56PM (#4940787)
Yeah, honestly though those fail the "clean" test for me. What I really meant isn't the computational capabilities of python generators but the way the mesh nicely with the way builtins like "in" or "all()" or "any()" work on plain old data. Reasoning about them, in general, isn't any harder than reasoning about a dict or list in the common case, and that's a feature missing from the perl equivalents which try to do it in a library. I'd feel comfortable throwing a comprehension into code intended to be maintained by a novice. I'd never inflict any of that junk on the poor maintainer.
Re:Perl is 25 years old today by ryporter on Tuesday December 18, 2012 @06:48AM (#4937101)
I'm in a similar boat. I've largely switched to Python as my scripting language (except for one-liners), but I'll still defend Perl against those who dismiss it out of hand. After using it an Amazon, I've seen firsthand how productive one can be with the language; and, if you're careful (following, e.g., the guidelines in "Perl Best Practices" by Conway) it's very possible to write maintainable code as well.
Re:Perl is 25 years old today by gvalkov on Tuesday December 18, 2012 @08:40AM (#4937409)
I hear you on the one-liners issue. Python is unhelpful when it gets to that - there are no -n, -p, -l switches or special variables like there are in Perl and Ruby.

It's a shame really, since all the building blocks needed to implement a useful mode for writing one-liners are there. In fact, I have a work in progress module that tries to address this issue: https://github.com/gvalkov/python-oneliner

Re:Perl is 25 years old today by slurgfest on Tuesday December 18, 2012 @10:38AM (#4937931)
This is an interesting idea, but if you need an extremely terse DSL which doesn't look anything like Python and has to be learned anew, why not just use Perl directly? Maybe for environments where you can't install Perl?
Re:Perl is 25 years old today by gvalkov on Tuesday December 18, 2012 @12:39PM (#4938541)
Thanks. My approach is actually pretty minimal. Just a read-print-loop (-n, -p), a few local variables (equivalents of $_, $~, $. etc) and a shorthand import syntax to take away some of the verbosity. There are other projects, such as pyp[1], that are more ambitious, but put you through a learning process.

By all means, everybody should use what they are comfortable with. Ruby and Perl allow you to write extremely terse and awesome one-liners, the likes of which will never be possible in Python. But if the first solution that pops into your head is a Python one, why not use that?

[1] http://code.google.com/p/pyp/

Re:Perl is 25 years old today by anonymous on Tuesday December 18, 2012 @08:53AM (#4937460)
I'm actually the reverse. I went to perl from python. I usually use it for system scripting and what I like about it is that it replaces bash nicely without removing the power of specialized syntax. I find it much easier to type open("program -args |") than to construct a subprocess object in python and manually extract lines from its output. On the other hand, python is pretty handy for larger programs, more than a few screens of dense perl syntax starts getting hard on the eyes.
Re:Perl is 25 years old today by dalke on Tuesday December 18, 2012 @07:27AM (#4937171)
Have you looked at APL or its derivatives like J and K? For certain types of work, and with experience, it's very terse and expressive.
Re:Perl is 25 years old today by 3amOpsGuy on Tuesday December 18, 2012 @07:42PM (#4940588)
J, now there's a language! The C source is written in a J like style: a wall of ascii punctuation interspersed with single char identifiers.

It looks like a fantastically expressive language, but i fear the learning curve would be too steep for my time invested to pay off - others would have to spend the same time to learn it as well, negating any code sharing / reuse in my org (we don't use J).

Re:Perl is 25 years old today by brcrth on Tuesday December 18, 2012 @08:01AM (#4937251)
So why you moved to Python?
Re:Perl is 25 years old today by 3amOpsGuy on Tuesday December 18, 2012 @07:54PM (#4940627)
Good question.

I found others i had to share code with weren't as interested as me about writing maintainable perl code - i got really fed up with a patch work of "write only" perl scripts performing useful functions in prod. From DB maintenance tasks to general housekeeping, to work-arounds for prod app issues that never got prioritised for strategic fixes.

I found in practice python code produced by most others was easier maintained: it seemed to more or less force people to write more readable and therefore, maintainable code.

I also viewed the fact that Python was one of the premier languages across so many problem domains (Systems, DB, GUI, Web, Large data, GPU, etc.) as being a sure fire payoff for any time invested in it.

In environments this size it's not any one persons actions dictate what becomes the status quo: different groups will have different agendas and therefore priorities and views. I guess i was lucky that everyone else was either willing to give Python a go, or was easily convinced.

FWIW over the past few years it's proven to be a good choice for us. There are still O'Reilly books float about, esp between the newcomers but there's no zealous "where's my camel book!" shouts anymore.

Re:Perl is 25 years old today by kamaal on Tuesday December 18, 2012 @10:19PM (#4940975)
I am somehow surprised that people consider merely forcing indentation on bad programmers makes them good programmers.
Re:Perl is 25 years old today by 3amOpsGuy on Wednesday December 19, 2012 @04:21AM (#4941794)
I think you're confused about what problem indentation solves.
Re:Perl is 25 years old today by chromatic on Tuesday December 18, 2012 @08:18PM (#4940696)
* ... it seemed to more or less force people to write more readable and therefore, maintainable code.*

What do you mean by "readable" and how does Python seem to enforce that?

Re:Perl is 25 years old today by 3amOpsGuy on Wednesday December 19, 2012 @04:09AM (#4941752)
So one of perl's nicest features is it just plain, flat gets out of your way. If you want to write something some way, go ahead, perl will allow you.

This is great - you can cook something up in 5 lines that would take 15+ in Python and 50+ in C++. It also means that it's really tempting for some to abuse this ability, even for code which will be long lived.

Re:Perl is 25 years old today by chromatic on Wednesday December 19, 2012 @04:05PM (#4944825)
I'm not sure I understand how that answers my question. Are you suggesting that Python doesn't get out of your way and that somehow its intransigence provides the benefit of somehow forcing people to write more readable code?

I don't believe Python is that inflexible (it's not), and I don't see how the limited and superficial ways in which Python pushes all code to look the same are really that meaningful.