Python for Perl programmers?

M

Markus Dehmann

I am using perl for everything, even bigger programs, with objects,
uh, modules and stuff. I know pretty much every trick in perl and have
a lot of experience.

But I'd like to try a cleaner language, where you don't have to type
so much crap to create a class etc. So, I wanna give python a try.

Is there a tutorial that takes all the standard perl things and then
explains how to do them in python? That would be perfect. Open a file,
take all the words, put them in a hash, do something with them, print
the result in a formatted way, write it to a new file etc. Create a
class that downloads newsgroups, etc. Things like that.

I don't need long explanations, but just the perl code and the
corresponding python code. Maybe that even helps seeing: ah, python is
much cleaner, or shorter, or whatever.

I mean, if I want to learn French and I already know a lot about
languages, it's probably good not to learn all the grammar, but just
to take an English book and the French translation, and learn from it.
I want to do the same to learn python.

Thanks for every hint!
Markus
 
A

Andrew Dalke

Markus said:
Is there a tutorial that takes all the standard perl things and then
explains how to do them in python? That would be perfect. Open a file,
take all the words, put them in a hash, do something with them, print
the result in a formatted way, write it to a new file etc. Create a
class that downloads newsgroups, etc. Things like that.

Years ago I helped a bit with the PLEAC project, at
http://pleac.sourceforge.net/

] Following the great Perl Cookbook (by Tom Christiansen &
] Nathan Torkington, published by O'Reilly) which presents
] a suite of common programming problems solved in the Perl
] language, this project aims to gather fans of programming,
] in order to implement the solutions in other programming
] languages.

There are a lot of examples there between Perl and Python,
and somewhat fewer comparisons with other languages.

Andrew
(e-mail address removed)
 
J

John Zhuang

Perl To Python Migration
By Martin C. Brown

Publisher : Addison Wesley
Pub Date : November 01, 2001
ISBN : 0-201-73488-5
Pages : 320
 
M

Markus Dehmann

John said:
Perl To Python Migration
By Martin C. Brown

Publisher : Addison Wesley
Pub Date : November 01, 2001
ISBN : 0-201-73488-5
Pages : 320

Is a 2001 book still okay? Or would I have to learn a new python version
afterwards?

Thanks
Markus
 
S

Sam Holden

I am using perl for everything, even bigger programs, with objects,
uh, modules and stuff. I know pretty much every trick in perl and have
a lot of experience.

But I'd like to try a cleaner language, where you don't have to type
so much crap to create a class etc. So, I wanna give python a try.

Reduced typing is a strange reason to go from perl->python :)

package Foo;
sub new {
my ($package, $start) = @_;
bless {count => $start}, $package;
}
sub inc {
$_[0]->{count}++;
}
sub dec {
$_[0]->{count}--;
}
sub val {
$_[0]->{count};
}


class Foo:
def __init__(self, start=0):
self.count = start
def inc(self):
self.count += 1
def dec(self):
self.count -= 1
def val(self):
return self.count

Both seem about the same amount of typing. The perl version actually
returns the previous counter value in inc() and dec(), but that's
not that useful anyway.

I guess you could argue all those {}s are "crap" :)
Is there a tutorial that takes all the standard perl things and then
explains how to do them in python? That would be perfect. Open a file,
take all the words, put them in a hash, do something with them, print
the result in a formatted way, write it to a new file etc. Create a
class that downloads newsgroups, etc. Things like that.

For a smaller grain size, there is:

http://www.python.org/moin/PerlPhrasebook

Though the perl code has parenthesis around function calls which
I don't think anyone would actually use in practice, eg.

print (join (" ", @$l));
print ("\n");

which would usually be (in perl):

print join " ", @$l;
print "\n";

or even (for the brave and foolish):

print "@$l\n";

But other than making the perl code harder to grok for perl programmers
not much harm is done, and it's still understandable and a reasonable
comparison.
 
C

Chris S.

Markus said:
I am using perl for everything, even bigger programs, with objects,
uh, modules and stuff. I know pretty much every trick in perl and have
a lot of experience.

But I'd like to try a cleaner language, where you don't have to type
so much crap to create a class etc. So, I wanna give python a try.

Is there a tutorial that takes all the standard perl things and then
explains how to do them in python? That would be perfect. Open a file,
take all the words, put them in a hash, do something with them, print
the result in a formatted way, write it to a new file etc. Create a
class that downloads newsgroups, etc. Things like that.

I don't need long explanations, but just the perl code and the
corresponding python code. Maybe that even helps seeing: ah, python is
much cleaner, or shorter, or whatever.

I mean, if I want to learn French and I already know a lot about
languages, it's probably good not to learn all the grammar, but just
to take an English book and the French translation, and learn from it.
I want to do the same to learn python.

Thanks for every hint!
Markus

I've read that Ruby is actually more akin to Perl than Python. From a
Ruby wiki, "[Matsumoto] chose the name to reflect the language's Perl
heritage". Granted, Python definitely deserves your attention, but if
you're a die-hard Perl user, you may also want to take a look at Ruby.
 
P

Peter Hickman

Chris said:
I've read that Ruby is actually more akin to Perl than Python. From a
Ruby wiki, "[Matsumoto] chose the name to reflect the language's Perl
heritage". Granted, Python definitely deserves your attention, but if
you're a die-hard Perl user, you may also want to take a look at Ruby.

I actually found Python to be too close to Perl when I first looked into it.
Ruby was sufficiently different to both for me to learn Ruby first.
 
N

Nick Craig-Wood

Markus Dehmann said:
I am using perl for everything, even bigger programs, with objects,
uh, modules and stuff. I know pretty much every trick in perl and have
a lot of experience.

But I'd like to try a cleaner language, where you don't have to type
so much crap to create a class etc. So, I wanna give python a try.

I've recently made that journey myself! For that reason and the
horrible experience of trying to maintain 25,000 lines of perl code.
Is there a tutorial that takes all the standard perl things and then
explains how to do them in python? That would be perfect. Open a file,
take all the words, put them in a hash,

I recently had a revelation to do with Perl hashes. Whenever I would
have written a hash in Perl, I now create a class in Python. The
class can then have documentation and methods. This is a major
difference for me betwen the two languages - its just so easy to
create a class in Python that whenever you are thinking of a data
structure you create a class (rather than a hash) and then immediately
you get a place to put functions which operate on that data etc.

I always found OO in Perl so clunky that I hardly ever bothered with
it, except if I was making a module. Python really encourages me to
do OO, and all my python programs are modules anyway. (I need that
encouragement to use OO since I'm of an age that my first computer
language was BASIC and my second assembler ;-)
do something with them, print the result in a formatted way, write
it to a new file etc. Create a class that downloads newsgroups,
etc. Things like that.

I don't need long explanations, but just the perl code and the
corresponding python code. Maybe that even helps seeing: ah, python is
much cleaner, or shorter, or whatever.

I've found that Python without all those { } @ % $ _ to be quite a bit
cleaner looking than Perl, especially for math heavy code. The
indentation thing really works. Docstrings are great. And the
standard library is very good and very comprehensive - you don't need
to download some module from CPAN of dubious quality - its all in the
standard library, properly documented etc.

Downsides of Python? __init__ __this__ __that__ __etc__ - I found
that a real turnoff initially! Regexpes a little more clunky. Same
functionality though. No command line shortcuts -l -p -i etc. (Not a
bad thing probably!).

Things which trip my up regularly? I still forget to put () on method
calls which take no parameters. Luckily pychecker catches this!
I mean, if I want to learn French and I already know a lot about
languages, it's probably good not to learn all the grammar, but just
to take an English book and the French translation, and learn from it.
I want to do the same to learn python.

"Programming Python" by Mark Lutz I found to be helpful as it has lots
of examples.

"Python Essential Reference" by David Beazley was useful for its
Python quickstart and library reference.

Write a python program is my advice though!
 
J

John Hunter

Markus> I am using perl for everything, even bigger programs, with
Markus> objects, uh, modules and stuff. I know pretty much every
Markus> trick in perl and have a lot of experience.

I was the same way 3 years ago. I read "Object Oriented Perl" by
Damian Conway and thought "God, this guy is a genius" (which he is), the
way he can get perl to do all this amazing stuff. In 2001, mainly for
thesis procrastination, I decided to learn python. I can honestly
say, I haven't touched a line of perl code since then, and hopefully
never will again. In python you don't have to be a genius to do
inheritance or to pass an array of dictionaries; it just works.

Others have answered your quest for perl specific migration docs. If
you are a perl guru, or a guru in any programming language, I doubt
you need documentation specific to your language of choice. If you
know what a for loop is, what a class is, and so on, learning python
is easy. I think the 1st 10 chapters in David Beazley's Python
Essential Reference are great -- they give a short but thorough
introduction to the core features. For a more comprehensive and
recent treatment, take a look at Python in a Nutshell and The Python
Cookbook.

JDH
 
T

Trent Mick

[Markus Dehmann wrote]
Is a 2001 book still okay? Or would I have to learn a new python version
afterwards?

It _is_ a little old, so you'd probably still have a few new things to
learn:

http://mboedick.org/miscellany/python_release_history.php

But the "What's New in Python?" series of documents provide a clear and
concise mechanism for reading up on the new stuff.

What's New in Python 2.0, 2.1, 2.2, 2.3:
http://aspn.activestate.com/ASPN/docs/ActivePython/2.3/whatsnew/overview.html

What's New in Python 2.4 (still in development):
http://www.python.org/dev/doc/devel/whatsnew/


Cheers,
Trent
 
A

Alex Martelli

Chris S. said:
I've read that Ruby is actually more akin to Perl than Python. From a

Knowing a lot of Python and a little Ruby, I concur with this
assessment.
Ruby wiki, "[Matsumoto] chose the name to reflect the language's Perl
heritage". Granted, Python definitely deserves your attention, but if
you're a die-hard Perl user, you may also want to take a look at Ruby.

You might want to take a look at Ruby anyway, particularly after a look
at Python, because it's quite instructive to see what they do in similar
ways, and what in very different ways. In the end, they reach
essentially equivalent power by pretty different routes. Yes, Ruby does
show some weird aspects of Perl heritage (the ability to use control
characters as variable names, the fact that REs are mixed into the
language rather than elegantly placed into their own module, etc, etc),
but I think they are, mostly, rather minor things. I don't think any
Perl programmer will choose a language based on whether or not they can
use control-T as a variable name, or whether they have to 'import re' in
modules that will use regular expressions, and the like.

I have already mentioned this in the past, but, let me repeat, since I
think the point is important...: my main reason for preferring Python
_for production code_ is essentially that Python has a strong cultural
bias towards uniformity, while Ruby shares Perl's cultural appreciation
of diversity. I shudder at the thought of consulting for a project
reveling in diversity, where each programmer does his or her own thing:
that's quite the opposite of how I think a multi-person project should
proceed, with egoless programming, no code ownership, and other such
Extreme Programming values. (One technical aspect where Ruby shows its
appreciation for "doing your own thing": you can alter the behavior of
builtin objects, for example to ensure that strings will compare equal
in a case-insensitive way; this may of course wreck havoc with libraries
you may be using that do NOT aspect such alteration of a built-in type's
behavior, but meanwhile you DO get to do your own thing to a much larger
extent than in Python, where built-in types can't be changed this way).

If you're just fooling around, or for somebody programming by himself
without much need to communicate with others nor share code, I can well
see that these pro-diversity biases and choices might well be a plus. I
am more interested in large systems which do require teams of
programmers, good communication, etc, so I stick with Python. But I
also keep looking for a serious motivation to use Ruby "in production"
in order to get better acquainted with it.

Most important, to me, is that having both languages around afford
maximal opportunity for each to follow a crucial precept: *to thine own
self be true*. Misguided types who come to this group to whine and
harangue against Python's _design principles_ (most often, ones
connected to the cultural bias towards uniformity) should just move to
Ruby and stop whining here -- they'd be happier, and so would we. If
there are similar whiners on Ruby lists complaining about Ruby's embrace
of vast diversity, they might, conversely, be happier with Python.

Rather than trying to remove any real choice, by pushing every language
towards being the same as each other, it's MUCH more clever to accept
that each language (if well designed) stems from a certain set of design
principles, and strive to recognize them: if you like the underlying
principles, you may love the language, if it embodies them well; if you
loathe the principles, use another language -- the better the language
embodies those principles you loathe, the worse off you'll be in using
that language!-). Python is quite up front about its design principles;
just fire up any interactive Python interpreter and type at the prompt:


Alex
 
A

Alex Martelli

Markus Dehmann said:
Is there a tutorial that takes all the standard perl things and then
explains how to do them in python? That would be perfect. Open a file,

It's not QUITE what you're asking for, but: try diveintopython.org and I
think you'll like it (perhaps you'll like it enougn to buy the paper
version). Mark Pilgrim is targeting exactly the kind of very
experienced programmer you seem to be, and his text moves fast...!


Alex
 
A

Aahz

Perl To Python Migration
By Martin C. Brown

Bad book. Maybe someday I'll write up all the problems I have with it,
but the short form is that there are lots of typos and plain wrong
information.
 
N

Nelson Minar

Is there a tutorial that takes all the standard perl things and then
explains how to do them in python?

How's this?
http://www.python.org/moin/PerlPhrasebook

I found the Python tutorial to be the single most useful thing for
learning Python.
http://www.python.org/moin/PerlPhrasebook

There's not so much a CPAN-equivalent for Python, partly because most
of the libraries you need are in the core distribution and partly
because there are fewer Python libraries than Perl libraries.


Enjoy learning Python! I switched about a year ago and find I almost
never get a rash from my scripting anymore.
 
M

Markus Dehmann

I am using perl for everything, even bigger programs, with objects,
uh, modules and stuff. I know pretty much every trick in perl and have
a lot of experience.

But I'd like to try a cleaner language, where you don't have to type
so much crap to create a class etc. So, I wanna give python a try.

Is there a tutorial that takes all the standard perl things and then
explains how to do them in python? That would be perfect. Open a file,
take all the words, put them in a hash, do something with them, print
the result in a formatted way, write it to a new file etc. Create a
class that downloads newsgroups, etc. Things like that.

Thanks for all your suggestions!! I found exactly what I need.

Thanks
Markus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top