PDA

View Full Version : perl, and python



israphelr
January 20th, 2007, 10:44 PM
Hi all.

I've been going through a python tutorial. The language seems very easy, especially compared to Java, which is what I tried a while ago

I just wanted someone to tell what perl and python are most commonly used for, as I'm interested in both languages. I'm new to programming.but..I have been told it's not a good idea for a newbie to begin with Perl.

Since I have nothing I really would like to build, I don't think theres any point in me learning a programming language yet(because i'd probably have to learn it all over again when the time comes around that I'd actually need to use it!)

Thanks.

meng
January 20th, 2007, 10:50 PM
Both languages have their advantages, but I happen to like python as a general-purpose language. Even if you don't plan to build a project right now, it's good to learn the CONCEPTS now and refresh the SYNTAX later. Admittedly, both concepts and syntax are best learned if you also work on projects (even small ones).

Mirrorball
January 20th, 2007, 11:07 PM
Basically everything you can do with one language, you can do with the other. They are both used for simple programs that don't do a lot of numerical computation.

pmasiar
January 21st, 2007, 05:11 AM
I have been told it's not a good idea for a newbie to begin with Perl.

That was very sane advice. Perl was popular years ago, when it was simpler than C++/Java, but now Python/Ruby are better choice.


Since I have nothing I really would like to build, I don't think theres any point in me learning a programming language yet(because i'd probably have to learn it all over again when the time comes around that I'd actually need to use it!)

Especially perl is known to have many quirks and irregularities which will boost your productivity if you hack perl 100 hours a week, but will confuse you thoroughfully if you did not looked in your perl code for 6 months.

Start learning Python anyway! Python is more regular, easier to learn and harder to forget. Learning programming language is like learning human language: just reading about it and it's syntax doesn't teach you how to solve simple problems. You need to use it in simple situations first, and build more complex, before you are ready to "talk to the native" - solving real-life problems. And you need to learn how to design data structures for different tasks, and how to build them in your language.

It may take you many weeks or months to became proficient enough in Python language to be able to start working on your own projects - beyond simple homeworks.

I have some tasks, from simple to more advanced, at "learn python wiki": http://learnpydia.pbwiki.com/ , you may want to try some.

You may also consider learning Game Maker (http://en.wikipedia.org/wiki/Game_maker) - GUI to build games with no need to write code in editor: you build game by manipulating objects in GUI. You will still learn concepts of programming (if, loop, events, create/delete object etc).

Game maker certainly is more fun during your first month of programming than Python :-) But I agree when you switch to conventional Python, many things could feel different - they look different, no nice icon to represent IF in python :-)

phossal
January 23rd, 2007, 02:58 PM
Perl is still as useful as it was some years ago, before the Python fad. They're both good languages though for general system tasks. Plus, learning how to program in either one is likely to make your life as an Ubuntu or general Linux user a better one. You'll be surprised how often you'll find yourself using your new language once you get the hang of it.

pmasiar
January 23rd, 2007, 04:09 PM
Perl is still as useful as it was some years ago, before the Python fad.

LOL! Why this uncalled-for drive-by-smear against Python? Do we need more flamewars? With no facts, just plain smear?

Here are some facts (with links):

Perl was OK 5 years ago, before Python had all the libraries. Now, IMHO Perl is good only for legacy projects. Action is moving to Ruby and Python. Perl6 is almost dead (http://en.wikipedia.org/wiki/Perl#Future) (being developed for 6 years, no result, developers leaving), I bet Ruby2 and Python3 will be out sooner that Perl6 - and I don't see reasons to switch to perl6 even when available. The only reason could be to have periodic table of operators (http://www.ozonehouse.com/mark/blog/code/PeriodicTable.html) with unicode. :-)

- Python (executable pseudocode): "There should be one obvious way to do it right"
- Perl (executable line noise): "There is more than one way to do it" - TIMTOWDI



I liked Perl as my first dynamically typed language - but Perl's style does not guide you to clean code, just the opposite: many quirks, many surprises, baffling newbie. Been there, done that, now using Python, thank you.

phossal
January 23rd, 2007, 04:36 PM
LOL! Why this uncalled-for drive-by-smear against Python? With no facts, just plain smear?Drive by smear? Simply because we've run into each other before, I know that you are a strong believer in Python's usefulness. That's great. I'm happy for you that you've found a language that you feel so strongly about. I like Python, too. I'm not sure what you mean by Drive-by smear.

I'm not sure what facts I have to provide in addition to pointing out that /usr/bin/perl still sits next to its brother /usr/bin/python on my Ubuntu Edgy, and it came like that. I use perl sometimes, and it works fine for me. Perl still helps me accomplish some basic system tasks.


Perl was OK 5 years ago, before Python had all the libraries. Now, IMHO Perl is good only for legacy projects.
You're absolutely allowed - in these forums - to express your opinion freely. I acknowledge that you feel Perl is only good for legacy projects. It isn't my desire to change your opinion. I use perl often enough though, and it still has plenty of value for me. I've read Learning Perl, Learning Python, Programming Perl, Programming Python, Mastering Regular Expressions in Perl, etc. I'm not sure I would avoid either language. I like them both. I don't believe it's a complete waste of time to learn either.



Action is moving to Ruby and Python. Perl6 is almost dead (http://en.wikipedia.org/wiki/Perl#Future) (being developed for 6 years, no result, developers leaving), I bet Ruby2 and Python3 will be out sooner that Perl6 - and I don't see reasons to switch to perl6 even when available. The only reason could be to have periodic table of operators (http://www.ozonehouse.com/mark/blog/code/PeriodicTable.html) with unicode. :-)
Interesting thoughts. Hopefully someone will put them to use. They're unrelated to my suggestion that learning perl is still useful, especially for administration tasks.



I liked Perl as my first dynamically typed language - but Perl's style does not guide you to clean code, just the opposite: many quirks, many surprises, baffling newbie. Been there, done that, now using Python, thank you.
Again, I'm glad you've found the language that handles all of the problems you face as a programmer. If nothing else, I'm happy for you that you've found something you feel so strongly about. If Python has eliminated all of your need for Perl, excellent. Python is a good language. I really don't know anyone who is trying to convince you, pmasiar, to stop using Python. We're happy for you that you're happy. Other programmers, like me, enjoy using other languages, too. And, for me, perl is certainly one of them.

phossal
January 23rd, 2007, 04:58 PM
Perl is still as useful as it was some years ago, before the Python fad. They're both good languages though for general system tasks. Plus, learning how to program in either one is likely to make your life as an Ubuntu or general Linux user a better one. You'll be surprised how often you'll find yourself using your new language once you get the hang of it.

A person who is looking for advice on whether or not Perl has any utility might be interested in hearing from a fairly neutral party that it does. I'm pretty neutral. I like both languages, and I'm always in favor of learning more. I am just making the suggestion that it isn't a waste of time to learn perl.

It seems obvious you recommend python. I do too. Unlike you, I recommend perl too. Is it not okay for me to voice my opinion as you have?

pmasiar
January 23rd, 2007, 05:20 PM
I was refering to:

before the Python fad.

without any facts or anything how python is s fad, compared to Perl.

I promised not to respond to your comment once before (and I restrained myself on simmilar comment (http://ubuntuforums.org/showpost.php?p=2048811&postcount=30) (also completely of-topic and uncalled for) in thread What's wrong with Java?? whre you even admitted you are not "politically correct" and you know it.

Of course you have right *NOT* be PC. I need to grow thicker skin. Next time I will try harder to ignore you smearing Python. I promise I will.

Smear (http://en.wikipedia.org/wiki/Smear_campaign): Smears often consist of unverifiable rumors and are often distortions, half-truths, or even outright lies; smear campaigns are often propagated by gossip spreading. Even when the facts behind a smear are shown to lack proper foundation, the tactic is often effective because the target's reputation is tarnished before the truth is known.

phossal
January 23rd, 2007, 05:24 PM
I'm not pc. If I think you're a complete moron, I will tell you. Python and Ruby are in a fad state. Their respective documentation and publishings are growing, their user base is growing, their popularity is growing. And it will pass. It's called a fad. Perl went through it. AJAX, PHP, and now Python and Ruby. Time goes by. And what was originally a fast-burning fire settles into a slow burn. That's all. I have never, ever (seriously) suggested Python was anything but a great language. I use it, and always admit it. I just believe Perl (once touted as the "glue of the web") has utility, too, even though its fad is over.

If it bothers you less, I'll happily use a different word. I'll use popular instead of fad. They mean essentially the same thing. All popular articles fade into the shadows of new ones.

You post a lot of opinions, but they're not open-minded. You exclude many things in your support for Python, but people deserve to know that there are alternatives. I don't smear. I say what I mean and move on. Perl is useful. Python is currently popular. There isn't any smear, pmasiar.

phossal
January 23rd, 2007, 05:31 PM
(and I restrained myself on simmilar comment (http://ubuntuforums.org/showpost.php?p=2048811&postcount=30) (also completely of-topic and uncalled for) in thread What's wrong with Java?? whre you even admitted you are not "politically correct" and you know it.

I don't get why you refer to my post about Python there. Even in that post, I acknowledge using Python, and I trash Perl right along with it (in my support for Java).

You're looking for something that doesn't exist. You're taking things personally, and you shouldn't. Python is a good language (for some junk). Perl is too (for some junk). Java is great (for different junk), and C is too (for other junk). I like languages. I like them all, in fact, which is why I would be bummed if a person recommended only one.


Smear (http://en.wikipedia.org/wiki/Smear_campaign): Smears often consist of unverifiable rumors and are often distortions, half-truths, or even outright lies; smear campaigns are often propagated by gossip spreading. Even when the facts behind a smear are shown to lack proper foundation, the tactic is often effective because the target's reputation is tarnished before the truth is known.
Saying perl has value fits this definition? You really need to think about how seriously you're taking this issue. Contact a mod for advice on how you should respond to posts, and what it means to accept another's opinion. I'm an argumentative person, and I like debate, but not with you. It sure seems like you're one to voice an opinion and then get angry when someone disagrees. Posting such a thing is way off topic, and a severe overreaction to my initial post suggesting that the OP should learn both languages in question. As usual, you've made this a ridiculous conversation about whether or not it's okay for me to say I like Python AND Perl, because it hurts your feelings (since you only like Python). It's weird. This isn't always about you.

Engnome
January 23rd, 2007, 06:06 PM
I'm not pc. If I think you're a complete moron, I will tell you. Python and Ruby are in a fad state. Their respective documentation and publishings are growing, their user base is growing, their popularity is growing. And it will pass. It's called a fad. Perl went through it. AJAX, PHP, and now Python and Ruby. Time goes by. And what was originally a fast-burning fire settles into a slow burn. That's all. I have never, ever (seriously) suggested Python was anything but a great language. I use it, and always admit it. I just believe Perl (once touted as the "glue of the web") has utility, too, even though its fad is over.

If it bothers you less, I'll happily use a different word. I'll use popular instead of fad. They mean essentially the same thing. All popular articles fade into the shadows of new ones.

You post a lot of opinions, but they're not open-minded. You exclude many things in your support for Python, but people deserve to know that there are alternatives. I don't smear. I say what I mean and move on. Perl is useful. Python is currently popular. There isn't any smear, pmasiar.

I agree.

It seems that you can't even suggest another language than python on the forums without getting bashed/ weird looks. Suggesting Perl means asking for it. So don't do that unless you want a flame war.

phossal
January 23rd, 2007, 06:11 PM
Suggesting Perl means asking for it. So don't do that unless you want a flame war.

Someone has to do it. If everyone does the easy thing, and no one does the right thing, the world becomes one in which I would not want to live. lol Besides, it doesn't seem like a guy who is so used to expressing opinions would find it so difficult to allow another one, especially one that left plenty of room for his own. You know?

pmasiar
January 23rd, 2007, 06:13 PM
I admit english is not my first (or even second) language. I looked it up



I'll use popular instead of fad. They mean essentially the same thing.

fad (http://www.m-w.com/cgi-bin/dictionary?sourceid=Mozilla-search&va=fad) - a practice or interest followed for a time with exaggerated zeal : CRAZE
popular (http://www.m-w.com/cgi-bin/dictionary?sourceid=Mozilla-search&va=popular): - suitable to the majority, commonly liked or approved - COMMON

As I understand it, "fad" has negative associations, "popular" does not.

As I said next time I will be more open and careful, and will try to ignore your comments about Python. let's have talk about something else.

Engnome
January 23rd, 2007, 06:20 PM
Someone has to do it. If everyone does the easy thing, and no one does the right thing, the world becomes one in which I would not want to live. lol Besides, it doesn't seem like a guy who is so used to expressing opinions would find it so difficult to allow another one, especially one that left plenty of room for his own. You know?

Have to agree with you again, it just that if someone does the thread derails (like this on has, thread starter you still reading this?) This kind of conversation is better to do in a separete thread.

Tomosaur
January 23rd, 2007, 06:20 PM
Pmasiar - I've said it before and I'll say it again, you like Python too much. You really do need to just chill out and accept that some people don't like Python, or at least think it's not as amazing as you think it is, without getting all hot and bothered over it.

I agree with Phossal - Python is a fad. Although Python itself is not brand spanking new, it's popularity has only really recently exploded. This fits the definition of a fad perfectly, and you do nothing to convince us otherwise when you get upset over it. It remains to be seen whether Python will stand the test of time (compared with C and other 'old' languages), or whether something new will come along and everyone will switch to that. You're trying to convince us that Python is something we don't think it is. I don't think anyone here dislikes Python, but it DOES have flaws, just like any other language, and it's pointless trying to mould python around problems which other languages can solve more efficiently, or otherwise 'better'.

AND NOW: My two cents:
I prefer Python over Perl, but perl is still pretty useful when you need to throw something together. Writing in Python is a more enjoyable experience for me, but I can take or leave either of them. I would reccommend Python over Perl for most things, but certainly don't just ignore Perl. Even though it's not really very active any more, it's still fairly useful if and when you need to write something quickly. You can also use Perl for cgi stuff, but cgi is essentially obsolete now anyway. If you're running a home server, Perl can be pretty effective still.

phossal
January 23rd, 2007, 06:24 PM
I admit english is not my first (or even second) language.


English is my first language. French is my second. Mandarin is my third. Most of my French and Chinese friends prefer to speak English though.

Perhaps fad has a negative connotation. Perhaps popular doesn't. Perhaps, if I tried, looking up either word and posting one of their respective definitions would yield a closer meaning, or one much further apart. Suppose fad does have a negative connotation. Perhaps I do mean to say Perl is in every way superior to Python, which is a useless language. How does that change the core issue that presenting my opinion is okay - even if it's opposed to yours. What you don't seem to understand is that my opinions do not take you into consideration at all. My opinion is simply that learning every language may prove useful, if not always practical. They have nothing to do with your existence.

If I should choose to have a debate with you, which I have not done to date, my only goal will be convincing everyone else that I make sense. Not you. Get it?



As I said next time I will be more open and careful, and will try to ignore your comments about Python. let's have talk about something else.

Just ignore me entirely. It's a better use of our time.

phossal
January 23rd, 2007, 06:27 PM
I prefer Python over Perl, but perl is still pretty useful when you need to throw something together. Writing in Python is a more enjoyable experience for me, but I can take or leave either of them. I would reccommend Python over Perl for most things, but certainly don't just ignore Perl. Even though it's not really very active any more, it's still fairly useful if and when you need to write something quickly. You can also use Perl for cgi stuff, but cgi is essentially obsolete now anyway. If you're running a home server, Perl can be pretty effective still.

lol Even I agree with all of that.

phossal
January 23rd, 2007, 06:31 PM
Have to agree with you again, it just that if someone does the thread derails (like this on has, thread starter you still reading this?) This kind of conversation is better to do in a separete thread.

I know! I know! I'm embarrassed. I think it's useless, bad for the thread starter, and a waste of time. Sorry. :frown:

pmasiar
January 23rd, 2007, 07:15 PM
I agree.

It seems that you can't even suggest another language than python on the forums without getting bashed/ weird looks. Suggesting Perl means asking for it. So don't do that unless you want a flame war.

It is funny it was *me* who suggested to thread starter, who sounds like a newbie, to try different language: not Python, but Game Maker. Then agreement between many posters is that Python is better first language to newbie than Perl (which is what I said, with explanations why). Then I am bashed for what exactly? For promoting Python? For explaining why Perl is *less* usefull than Python, with links? :confused:

Or was it by using phrase "drive-by smear"? So you can use "fad" without any explanation why you think Python is "fad", and I cannot say what I think about you using the word "fad", even if I argument my position with facts? Even more :confused:

Or is it that any popular language is just fad? Is java fad? Is C fad? Was Cobol fad?

No more postings today.

phossal
January 23rd, 2007, 07:17 PM
You've been beaten badly today. And the tides of popular opinion have turned against you. Now, you must retreat, and cover your own tracks. It's humiliating. You started a war, pmasiar. And you've yet to win any battle. Give it up.

lol Just kidding. :D C'mon, we're all friends here. Everyone digs Python, and maybe in the future you'll acknowledge that learning Perl is cool too. Just say what you have to say, and stop wasting so much time trying to convince everyone else they're wrong.

jblebrun
January 24th, 2007, 07:37 PM
Hey, Perl is a VERY important language. Whenever I need to generate a string of random characters, I use a Perl program..... well, actually, I use the Perl source code as an input file to my Python script. ;-)

Ok, sorry. Now, in seriousness.... I started learning Perl a few years ago, for doing data parasing and analysis. It is very useful for that. However, once I learned Python, I couldn't see any reason to spend anymore time learning Perl. As a result, I only have a basic knowledge of Perl. I can't see any reason to learn it instead of Python, unless you have to work with an existing Perl codebase.

The one thing I miss about Perl is its regular expression handling. I desperately hope that Python eventually integrates RE handling into the core language. The re library is very ungainly. It's almost ironic... Python is a much more beautiful language than Perl, EXCEPT when it comes to regular expressions!

phossal
January 24th, 2007, 08:08 PM
The one thing I miss about Perl is its regular expression handling. I desperately hope that Python eventually integrates RE handling into the core language. The re library is very ungainly. It's almost ironic... Python is a much more beautiful language than Perl, EXCEPT when it comes to regular expressions!

Yep. Me too. Reading "Mastering Regular Expressions" (http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/sr=8-1/qid=1169665720/ref=pd_bbs_sr_1/104-8612182-7680702?ie=UTF8&s=books) is reason alone to learn a little Perl. That was one of the most fun experiences of my early programming career. I miss those days, and perhaps I still champion Perl out of nostalgia.

pmasiar
January 25th, 2007, 02:37 PM
The one thing I miss about Perl is its regular expression handling. I desperately hope that Python eventually integrates RE handling into the core language.

IIRC integrating module re is *not* gonna happen. I read somewhere re as example of a functionality which is better to keep out of language as standard library. I was trying to find a link but sorry no luck :-( So you either trust me or not :-)

Instead, core part of language are simple string methods (startswith, endswith, split etc) with easy-to-remember names covering maybe 70% of average programmer's needs, without need to dive to regular expressions swamp, where greedy dot-star lurks to swallow unsuspecting noobs :-)

If you need it, re is one import away, but if you *don't* need it, it does not complicate syntax and confuse beginners :-) Python's approach (as I see it) is to provide universaly usable features in core, stable widely useful libraries as standard, and allow independent development of new external libraries off main trunk, to postpone including them into standard until ready. Including library to standard too soon means freezing it's API (or breaking the code) and slowing it's development.

Just my 0.02, IMHO, YMMV, I am not Python Core developer.

cellfish
January 25th, 2007, 04:47 PM
Drive by smear? Simply because we've run into each other before, I know that you are a strong believer in Python's usefulness. That's great. I'm happy for you that you've found a language that you feel so strongly about. I like Python, too. I'm not sure what you mean by Drive-by smear.

I'm not sure what facts I have to provide in addition to pointing out that /usr/bin/perl still sits next to its brother /usr/bin/python on my Ubuntu Edgy, and it came like that. I use perl sometimes, and it works fine for me. Perl still helps me accomplish some basic system tasks.


You're absolutely allowed - in these forums - to express your opinion freely. I acknowledge that you feel Perl is only good for legacy projects. It isn't my desire to change your opinion. I use perl often enough though, and it still has plenty of value for me. I've read Learning Perl, Learning Python, Programming Perl, Programming Python, Mastering Regular Expressions in Perl, etc. I'm not sure I would avoid either language. I like them both. I don't believe it's a complete waste of time to learn either.


Interesting thoughts. Hopefully someone will put them to use. They're unrelated to my suggestion that learning perl is still useful, especially for administration tasks.


Again, I'm glad you've found the language that handles all of the problems you face as a programmer. If nothing else, I'm happy for you that you've found something you feel so strongly about. If Python has eliminated all of your need for Perl, excellent. Python is a good language. I really don't know anyone who is trying to convince you, pmasiar, to stop using Python. We're happy for you that you're happy. Other programmers, like me, enjoy using other languages, too. And, for me, perl is certainly one of them.

^^ True Story

yaaarrrgg
January 25th, 2007, 08:00 PM
IMO: For Perl, I think the strength is in text processing. Also, it's handy in complex system scripts where bash is painful. One odd use I've found for it is in manipulating sections of code in other languages. For example, with a mySQL database, you might write a perl script to autogenerate Insert/Update/Select/Delete stored procedures that access all the tables. The criticism I usually hear is that the syntax is cryptic.... you can cram a lot of functionality into just a few characters.

Python seems to be better for more general purpose apps (like GUI's)... although I haven't really used it that much. The only major disadvantage I've seen is that some of the libraries I needed (like for SOAP) were not 100% polished or well documented, as compared to Perl libs (not that the Perl libs are that much better). But generally, I'd probably recommend it over Perl.

hitter
January 28th, 2007, 06:38 PM
I agree PERL is the best language for text processing... i mainly use it for parsing files because of its richness in regular expression..

BTW who said perl is dying.. see these links

http://www.eweek.com/article2/0,1895,2016415,00.asp

http://www.rubyinside.com/ruby-enters-top-10-programming-languages-at-tiobe-353.html

No offense to python!!

phossal
January 28th, 2007, 06:41 PM
Just like C, a lot of Ubuntu "components" still use perl: update-alternatives, ndiswrapper, etc.

Note360
January 28th, 2007, 08:14 PM
Ok, look Perl, Python, Ruby, PHP are all good and have their uses.

Perl - Really good for basic CLI stuff. Basically, BASH on steroids. Really if you want to do bash stuff Perl is the next step.

Python - Really good for CLI and GUI. Amazing for drafting and plugins.

Ruby - My favorite. Good for CLI and a growing GUI library. I like ruby classes better though they are very simular to python. Ruby can be elegant like python or ugly like Perl.

(Perl can be written amazingly nice, but with Perl most big projects started off being thrown together and legacy code was never changed over)

PHP - I like it for web dev. It beats the others because it is so widely used and exists on nearly every web server. Looks like Perl and can be uglier because of HTML.

It is useful to know bits of all (and C, C++, Java or C#).

jblebrun
January 28th, 2007, 08:16 PM
I agree PERL is the best language for text processing... i mainly use it for parsing files because of its richness in regular expression..


Hmmm... does Perl have a richness in regular expression syntax that Python doesn't? I've never found a regular expression task in Perl that I couldn't accomplish in Python. Plus, I think Python handles a lot of the "common cases" better. For example, I'd rather read:



if "sought text" in string




if $string =~ m/sought_text/

phossal
January 28th, 2007, 08:18 PM
The one thing I miss about Perl is its regular expression handling. I desperately hope that Python eventually integrates RE handling into the core language. The re library is very ungainly. It's almost ironic... Python is a much more beautiful language than Perl, EXCEPT when it comes to regular expressions!


Hmmm... does Perl have a richness in regular expression syntax that Python doesn't? I've never found a regular expression task in Perl that I couldn't accomplish in Python. Plus, I think Python handles a lot of the "common cases" better.


Are you the same person? :D

Note360
January 28th, 2007, 11:34 PM
lol

jblebrun
January 28th, 2007, 11:38 PM
Are you the same person? :D

It does seem contradictory, but I never said that Python was missing any sort of richness in its expression handling. My gripe was purely syntactical. I will, however, admit to backpedaling somewhat, as I've re-thought my position on python's regexp module. Even though it's more verbose code than Perl, I think it's much more sensible, and leads to much more maintainable code, in the end.

Really, all I want in python is a way to easily define regexps. So I want a hybrid sort of system. Something like...



regexp=/^blah/
regexp.match(string)

instead of


regexp=re.comile("^blah")
regexp.match(string)


Then you could do


/^blah/.match(string)

Of course, I don't ever expect such bizarre, out-of-place synatx to become a part of Python, especially for something that isn't even part of the core language!

hitter
January 29th, 2007, 06:43 AM
If you have patience to read this.. then u'll reach a conclusion(atleast sort of..!!).. if u dont have patience read the first line alone..

which is better: Perl or Python? More importantly, why?
-- Kragen Sitaker
(http://lists.canonical.org/pipermail/kragen-tol/2002-January/000660.html)

Not surprisingly, both are better than the other in some ways. (Be
warned, this is about 2500 words.)

Python has a read-eval-print loop, with history and command-line
editing, which Perl doesn't.

Python has a bigger standard library, which tends to be better
designed. For example:
- it's possible to write signal handlers that work reliably without a
deep knowledge of the implementation (and I'm not convinced that a
deep knowledge of the implementation is sufficient in Perl)
- time.time() and time.sleep() speak floating-point
- open() raises exceptions when it fails. Every day, people post on
comp.lang.perl asking why their programs are failing, and it's because
some file is missing or unreadable, and they can't tell what's
wrong because they've forgotten to check the return code from
open(). This doesn't happen in Python.
- os.listdir() omits '.' and '..'; they're always there, so you can
include them by writing ['.', '..'] + os.listdir(), but you almost
never want them there. Perl's readdir doesn't omit them, which is
a very frequent source of bugs in programs that use readdir.

On the other hand, Perl has a much bigger nonstandard library --- CPAN
--- and some of its standard library is better-designed:
- system() can take a list of arguments to avoid invoking the shell,
meaning all characters are safe
- so can popen()

Python makes it sort of a pain to build types that act like built-in
lists or strings or dictionaries or files, and (as of 2.0, with 'x in
y' now having a meaning when y is a dict) it's impossible to build
something that acts both like a dictionary and a list. It's
relatively easy to build something that acts like a function.

But Perl makes it a pain to *use* types that act like those things,
and it's impossible to build types that act like files or functions.
But you don't need to build types that act like functions, because you
have Scheme-style closures.

Python's syntax is far, far better.

Except that it's indentation-sensitive, which makes it slightly harder
to cut-and-paste code and offends the kind of people who unthinkingly
adhere to stupid traditions for no good reason. (It might offend
other kinds of people, too, but I know it offends this kind of
people.)

Perl has a C-like plethora of ways to refer to data types, which
brings with it lots of confusion and dumb bugs. It also makes things
like arrays of arrays and dicts of dicts slightly more confusing.

Perl has lots of implicit conversions, which hide typing errors and
silently give incorrect results. Python has almost none, which leads
to slightly more verbose code (for the explicit conversions) and
occasional fatal exceptions (when you forgot to convert). (Python
unfortunately has some and is getting more.)

On the other hand, Python overloads + and * to do different, though
vaguely related, things for strings and numbers; Perl makes the
distinction explicit, calling Python's string + and * . and x instead.
Also, there is a certain variety of implicit conversion --- namely,
from fixnums to bignums --- that Python doesn't yet do, but should.
(Python 2.2 does it.)

Python's variables are local by default. Perl's are global by
default. Perl's policy is unbelievably stupid. Worse, in Perl, the
normal ways to make variables local don't apply to filehandles and
dirhandles, so you have to use special tricks for them.

Perl can be (and, for me, always is) configured to require that all
variables be declared and local. Python has no way to require
variable declarations, although reading an uninitialized variable is a
run-time error, not a run-time warning.

Perl implicitly returns the value of the last expression in a routine.
Python doesn't. This is a point in Python's favor most of the time,
although it makes very short routines verbose. (Although Python has
lambda, which lets you write those very short routines in the Perl
style.)

Perl has nested lexical scopes, which means that occasionally your
variables disappear when you don't want them to, but more often, they
aren't around to cause trouble when you don't want them to. They also
make it really easy to write functions that return functions as
Scheme-style lexical closures. In Python, writing functions that
return functions is painful; you must explicitly list all of the
values you want to close the inner function over, and if you want to
keep callers from accidentally blowing your closure data by passing
too many arguments or keyword arguments with the wrong names, you need
to write a class with __init__ and __call__. Also, in Python, if you
want statements in your closure, you can't write it inline --- you
have to write def foo() and then refer to foo later.

Also, Python 2.x has list comprehensions, which reduce the need for
really simple inline functions (for map and filter), and also are a
very nice language feature in their own right. (And they give you
nested scoping, although it's kind of gross.)

The Perl parser gives better error messages for syntax errors.

Perl optimizes better.

Python has an event loop in the standard library. Perl has POE, which
isn't in the standard library.

Perl has while (<>). Python doesn't, although it has
fileinput.input(), which seems to be broken for interactive use. (It
doesn't hand the lines to the loop until it's read 8K, and it requires
you to hit ^D twice to convince it to stop reading and once more to
end the loop.)

Strings in Python are immutable and pass-by-reference, which means
that passing large strings around is fast, but appending to them is
slow, and it's possible to intern so that string compares are
blazingly fast. Strings in Perl are mutable and pass-by-value, which
means that passing large strings around is slow, but appending to them
is fast, and comparing them is slow.

Python lists don't auto-extend when you try to assign to indices off
the end. Perl lists do. This is generally a point in Python's favor.

Perl autovivifies things, so you can say things like $x->{$y}->[$z]++,
which will make a hash for $x if there isn't one already, an array for
$x->{$y} if there isn't one already, and an element for $x->{$y}->[$z]
if there isn't one already, before incrementing it from its default
value of zero. Doing this in Python is painful. However, Python
allows tuples as hash/dict keys, which lessens the need for this;
you can write
if not x.has_key(y, z):
x[y, z] = 0
x[y, z] = x[y, z] + 1

Python treats strings as sequences, so most of the list and tuple
methods work on them, which makes some code much terser. You have to
use substr() in Perl.

Python requires you to use triple-quoted strings to have multiline
strings. Perl has here-docs, but also lets ordinary strings cross
line endings.

Perl indicates the ends of ranges in two ways: the index one past the
end of the range, and the index of the last element in the range.
.. uses the second; @foo in scalar context uses the first; etc.
Python consistently uses the index one past the end of the range,
which is confusing for new users.

Python has this icky (x,) syntax to create a tuple of one item. Perl
doesn't have this problem.

On the other hand, Perl has cryptocontext bugs: expressions evaluate
to different, and possibly unrelated, things in scalar and list
context. This rarely bites me any more, but it used to.

Perl's context-dependency in function evaluation leads to brittleness
problems, in ways that are difficult to explain; it leads to
difficulties in wrapping functions, a la Emacs advice.

Python has reasonable exception handling; you can catch just the
exceptions you expect to have happen. Perl has 'die', and if you
want, you can eval {} and then regex-match $@ to see if it was the
exception you wanted, and if not, die $@. The usual upshot is that
Perl programs that catch some exceptions usually end up catching all
exceptions and continuing in the face of exceptions that should be
fatal.

On the other hand, it's still too easy to write a program that does
that in Python, too, so people do.

Python gives you backtraces when there are exceptions, from which you
are more likely to be able to find the error than from Perl die
messages, because they have more information; but Perl die messages
are likely to tell you where the error is more quickly, for the same
reason. Perl has 'croak', which lets you decide which level of the
call stack to accuse of causing the error, and can give you backtraces
if you want them.

The Python syntax for referring to things in another module is terse
enough that people actually use it. Perl's syntax for the same thing
is uglier ($math::pi instead of math.pi) and Perl module names are
longer, and so people tend to import things from the other modules
into their own namespace. This makes Perl programs harder to
understand.

However, in both Perl and Python, you can specify which names can be
imported from your module into someone else's namespace, but you can't
specify which names can be referred to from another module (e.g. as
math.pi, $math::pi). The consequence is that, in Perl programs, you
can usually tell which names are internal to the module and which ones
are used from other modules, and in Python programs, you usually
can't. (Without looking at the other modules, that is.)

Perl lets you trivially build dicts out of lists, which is good,
because lists are easy to compute. Python doesn't, although you can
write imperative loops to do the same thing.

Perl lets you easily splice lists into other lists (functionally, in
list literals); Python requires you to do that imperatively.

You can't slice dicts in Python, although you can use 2.x listcomps to
get almost the same effect: Perl's @thing{qw(foo bar baz)} becomes
[thing[k] for k in 'foo bar baz'.split()]. I'm not sure whether this
is better or worse; I think they're both pretty unreadable.

Perl has 'last LABEL' and 'next LABEL'. Python doesn't. This is
stupid of Python, although I don't need multi-level break often. I
can get two-level break by moving the nested loops into a new function
and using 'return', and two-level continue by one-level break.

When Perl converts aggregate data types into strings (e.g. for
printing), it turns any references into ugly strings. When Python
does, it recursively prints what is pointed to (which fails if the
structure is cyclic).

In both Perl and Python, the rules for what counts as true and what
counts as false in conditional expressions are needlessly complicated.

In Python, loop conditions that have side effects end up needing to be
hidden in functions, or you have to write an N-and-a-half-times loop.
Which there's no syntactic construct for, so you have to kludge it
with 'while 1:' and 'break'.

In Python, you can iterate over multiple sets of items at once:
for number, name in [(0, 'zero'), (1, 'one'), (2, 'two')]:
pass
You can't do that in Perl, although you can do the equivalent if you
have an iterator function which returns the tuples one at a time:
while (($number, $name) = next_num_name) { }
Python also has the zip/map(None,...) function to make this easier,
and map() can take multiple sequences which it iterates over in
parallel.

Python has built-in arbitrary-precision arithmetic. Perl has it in a
nonstandard library.

Python has built-in named, default, and variadic parameters; Perl lets
you do all those things yourself, which means that every Perl library
that uses named parameters does it differently, and none of them have
syntax as nice as Python's f(x=3, y=5) syntax.

Perl has class methods. Python doesn't, although, unfortunately, it
is adding them in 2.2.

Perl unifies classes with modules; Python doesn't. So in Python, you
can't import a class directly, the way you can in Perl; you can import
it from the module it lives in, or you can import its module and get
it from there. In Perl, the module is the class. On the other hand,
in Python, modules are unified with files, and in Python, they aren't;
this usually results in more verbosity in Perl.

Python lets you create classes at run-time with the same ease, or lack
thereof, that you can create functions. Perl doesn't. This is
arguably excessive flexibility that leads to excessive cleverness and
unmaintainable code.

Perl will destruct any objects left around at program exit, possibly
resulting in destructing objects that hold pointers to
already-destructed objects; Python doesn't destruct them at all. Both
of these approaches suck.

Python's sort() and reverse() are in-place only, which means they
don't work on immutable sequences, and often makes your code more
complicated; this is moronic. Perl did the right thing here.

Perl's split uses a regex. Python's standard split doesn't. Perl is
better here. However, Python's standard split defaults to the right
separator (whitespace) when you just specify a string, and Perl's
split, by default, discards trailing empty fields, which Python's
doesn't.

Python's built-in comparison routines do recursive lexical comparison
of similar data structures, so you can sort a list of lists or tuples
straightforwardly; and you can sort records by some computed key by
forming tuples of the key and the record, then sorting the tuples. If
you try to sort complex data types in Perl, it will sort them by
memory address.

Python's regular expression library is easier to understand than
Perl's, and uses mostly compatible syntax (although Perl keeps adding
features). Perl's regular expressions return subexpressions by
mutating global variables $1, $2, etc., which have their state saved
and restored in hard-to-understand ways, and they don't mutate those
variables if they don't match. Python's regular expression match
operator returns a 'match object', which is null if the regex failed
to match, or has a method to fetch numbered subexpressions of the
match if it didn't fail to match.

-==-THE END-==-

jblebrun
January 29th, 2007, 09:04 AM
I have some questions about this article... some may be due to misunderstandings on my part...


Python makes it sort of a pain to build types that act like built-in
lists or strings or dictionaries or files, and (as of 2.0, with 'x in
y' now having a meaning when y is a dict) it's impossible to build
something that acts both like a dictionary and a list. It's
relatively easy to build something that acts like a function.

What does this mean, exactly? You can subclass the built in types...



Perl has lots of implicit conversions, which hide typing errors and
silently give incorrect results. Python has almost none, which leads
to slightly more verbose code (for the explicit conversions) and
occasional fatal exceptions (when you forgot to convert). (Python
unfortunately has some and is getting more.)

What implicit conversions exist in Python thus far? (That's what the article writer means when saying "Python ... is getting more" right?)



make it really easy to write functions that return functions as
Scheme-style lexical closures. In Python, writing functions that
return functions is painful; you must explicitly list all of the
values you want to close the inner function over, and if you want to

What is meant by "you must explicity list all of the values you want to close the inner function over? A lambda will include all variables in the current scope of the lambda...



keep callers from accidentally blowing your closure data by passing
too many arguments or keyword arguments with the wrong names, you need
to write a class with __init__ and __call__. Also, in Python, if you
want statements in your closure, you can't write it inline --- you
have to write def foo() and then refer to foo later.

I haven't found a case where I need lambda... all it does is obfuscate code. You can just use a def func(), and swallow extraneous arguments with **dummy at the end of the arg list...



Perl has while (<>). Python doesn't, although it has
fileinput.input(), which seems to be broken for interactive use. (It
doesn't hand the lines to the loop until it's read 8K, and it requires
you to hit ^D twice to convince it to stop reading and once more to
end the loop.)

This just forces you to write self-documenting code. Oh darn.



Strings in Python are immutable and pass-by-reference, which means
that passing large strings around is fast, but appending to them is
slow, and it's possible to intern so that string compares are
blazingly fast. Strings in Perl are mutable and pass-by-value, which
means that passing large strings around is slow, but appending to them
is fast, and comparing them is slow.

Can't you get around the slow append using "".join(strlist)?



Perl autovivifies things, so you can say things like $x->{$y}->[$z]++,
which will make a hash for $x if there isn't one already, an array for
$x->{$y} if there isn't one already, and an element for $x->{$y}->[$z]
if there isn't one already, before incrementing it from its default
value of zero. Doing this in Python is painful. However, Python
allows tuples as hash/dict keys, which lessens the need for this;
you can write
if not x.has_key(y, z):
x[y, z] = 0
x[y, z] = x[y, z] + 1

Or you can write


x[key]=x.get(key,default)+1




Perl lets you trivially build dicts out of lists, which is good,
because lists are easy to compute. Python doesn't, although you can
write imperative loops to do the same thing.

c=dict(zip(a,b)) is fine for me!



Python's sort() and reverse() are in-place only, which means they
don't work on immutable sequences, and often makes your code more
complicated; this is moronic. Perl did the right thing here.

I disagree! The common case should always be the simpler, less intensive one!!! It's not that hard to write
a=list(t)
a.sort()
t=tuple(a)

I like how ruby handles this mutable vs. immutable issue by provide sort and sort!, reverse and reverse!, etc, where the banged version is a mutator, and the non-banged version makes a copy.


Perl's split uses a regex. Python's standard split doesn't. Perl is
better here. However, Python's standard split defaults to the right
separator (whitespace) when you just specify a string, and Perl's
split, by default, discards trailing empty fields, which Python's
doesn't.

Again... keep the common use-case simple.