-->

I know Perl 5. What are the advantages of learning

2019-03-17 12:49发布

问题:

Coming from a Perl 5 background, what are the advantages of moving to Perl 6 or Python?

Edit: If you downvoted this because you think it's just flamebait, read the answers below. They're not raving arguments; they're well-written discussions of the pros and cons of each language. Give the Stack Overflow community some credit.

回答1:

There is no advantage to be gained by switching from Perl to Python. There is also no advantage to be gained by switching from Python to Perl. They are both equally capable. Choose your tools based on what you know and the problem you are trying to solve rather than on some sort of notion that one is somehow inherently better than the other.

The only real advantage is if you are switching from a language you don't know to a language you do know, in which case your productivity will likely go up.



回答2:

Perl 6 has some great features over python. I think these are all features that Python 3 doesn't have:

  • an extensible, but optional type system
    • including multi-method dispatch and type checking
    • even for builtins and "operators"
    • with subset types
  • Roles: Non-instantiable classes for code reuse (aka mixins)
    • with some additional advantages over most mixins:
    • compile-time method name collision checking,
    • the ability to require, in a role definition, that certain methods are present in the host class, and subsequently to call methods of the host class in the implementation of the role.
  • Grammars (compositions of regexes that return structured data)
  • Powerful extended regexes. Fancier than python's built-in regex capabilities.
  • Junctions and type junctions
  • Metaoperators
  • The ability to define circumfix, postcircumfix, infix, prefix, and postfix operators.
  • Theoretically, the ability to work with the AST as structured data. (Aka: lisp macros)
  • Block scoping
  • The * term for generating closures out of any expression
  • Implicit concurrency constructs ([] meta, junctions)
  • Declarative concurrency constructs (hyper for)
  • The MAIN sub turns its function signature into a command line interface and a command line usage guide.
  • Lazy lists (but you can roll your own with generators)

And it shares with Python the following advantages over Perl 5:

  • The concept of a "class", the class keyword, and associated sugar**:
    • The self keyword
    • getter/setter sugar with the concept of private data
  • a well defined, accessible meta-object protocol
  • great unicode support
  • great date/time support built in
  • In general, "batteries included" - lots of builtin functions
  • non-globally scoped variables by default

Perl6's implementation is not 100% complete, or tuned enough to use for certain values of "performance sensitive" applications. But the 80-90% of the spec that is needed for 95-99% of use cases is complete in Rakudo, right now*

*One glaring omission is any provision for asynchronous I/O. That part of the spec is in flux.

** Though the popular Moose project provides this in Perl 5.



回答3:

Python does not have Junctions. In fact I think only Perl has Junctions so far. :-)



回答4:

In my opinion, Python's syntax is much cleaner, simpler, and consistent. You can define nested data structures the same everywhere, whether you plan to pass them to a function (or return them from one) or use them directly. I like Perl a lot, but as soon as I learned enough Python to "get" it, I never turned back.

In my experience, random snippets of Python tend to be more readable than random snippets of Perl. The difference really comes down to the culture around each language, where Perl users often appreciate cleverness while Python users more often prefer clarity. That's not to say you can't have clear Perl or devious Python, but those are much less common.

Both are fine languages and solve many of the same problems. I personally lean toward Python, if for no other reason in that it seems to be gaining momentum while Perl seems to be losing users to Python and Ruby.

Note the abundance of weasel words in the above. Honestly, it's really going to come down to personal preference.



回答5:

Perl is generally better than python for quick one liners, especially involve text/regular expressions
http://novosial.org/perl/one-liner/



回答6:

Python has one huge advantage: it's implemented, there's a rather stable compiler for it.

Perl 6 is a rather visionary language, and not yet anywhere nearly stable enough for production. But it has a set of very cool features, among them: junctions, grammars (yes, you can write full parsers with Perl 6 "regexes"), unicode handling at the grapheme level, lazy lists and powerful macros.

In your particular case when you know Perl 5 you'll get familiar with the Perl 6 syntax very quickly.

For a more comprehensive list of what cool features Perl 6 has, see the FAQ.



回答7:

You have not said why you want to move away from Perl*. If my crystal ball is functioning today then it is because you do not fully know the language and so it frustrates you.

Stick with Perl and study the language well. If you do then one day you will be a guru and know why your question is irrelevant. Enlightment comes to those to seek it.

  • You called it "Perl5" but there is no such language. :P


回答8:

Python has a major advantage of being available in a production-ready format today.

Python has Jython and IronPython, if you need to work closely with Java or the .net clr.

Perl 6 has the advantages of being based on the same principles as Perl (1-5); If you like Perl, you'll like Perl 6 for the same reasons. (There's more than one way to do it, etc.)

Perl 6 also has an advantage of being only partially implemented: If you want to hack on language internals or help to define the standard libraries, this is a great time to get started in Perl 6.

Edit: (2011) It's still a great time to hack on the Perl6 internals, but there is now a much more mature, usable Perl6 distribution, Rakudo Star. If you want to use Perl6 today, that's a great choice.



回答9:

IMO python's regexing, esp. when you try to represent something like perl's /e operator as in s/whatever/somethingelse/e, becomes quite slow. So in doubt, you may need to stay with Perl5 :-)