Perl to Python Migration provides a useful and practical resource for programmers that are currently intermediate or expert in using Perl and who want to migrate to using Python for different programming projects. The book achieves this by first giving the user an overview and refresher of the Perl techniques required before giving the information on using Python to achieve the same result. Examples of both Python and Perl will be available on-line on the author's website.
Components of a Python Program
Modules, Classes, and Object Orientation
Interfacing to the OS
Communicationing over a Network
Using Python for Web Development
Interface Building with Tk
Resource Guide - (in conjunction with material on the web)
As a long time Perl and Python programmer, Im constantly finding myself using Perl constructs in Python or Python constructs in Perl. Worse, I can suddenly find myself trying to use a feature or system in a Python program that just doesnt exist, or does and works differently. What I needed was some sort of cross reference.
After talking to other programmers and developers it became apparent that a number of people were "jumping ship" from Perl to Python. Some with varying levels of success. The reasons for using Python in preference to Perl were different for each person, but there were some common themes:
Python is object-oriented. Although Perl has object orientation facilities they are not integrated into the system. In Python everything is an object or class even the base data types are objects and have their own methods and attributes. This makes Python a great language to use in situations where you are working on applications that use object orientation as a core technology.
Python is easy to use. Python has a very clean and structured layout, and its very easy to follow whats going on. Perl can frequently look like line noise, and particularly for new programmers this becomes a problem as they try to understand why their program works.
Python does what you tell it to. Perl suffers from a severe case of semi-intelligence. Statements and expressions in Perl have all sorts of rules, exceptions to those rules, and other artefacts that can make it difficult to follow what is going on. Just think about what happens when you call a function, was it called in list or scalar context?, what arguments did it have?, how do I get them back? These are regular questions when programming with Perl.
Python is cross-platform compatible. Perl is also cross-platform compatible, for certain values of compatible. Because of Perls Unix roots it is very much a Unix language use Perl under Windows or MacOS and a large proportion of the built-in functions become obsolete. Pythons functionality is supported by external modules, many of which use the correct version according to their platform whilst retaining the same API.
These arent really hard and fast reasons for why you should make the jump to Python, but they are compelling reasons for why Python is at least worth a look.
In this book thats exactly what we try to do show you how you can make the move from Perl to Python. Many of the basics are the same, and despite a change of language, youre unlikely to need to change the algorithms you are already using.
For the more advanced programmer, or, like me, the programmer who has to use both languages most days, the book should also work as a handy cross reference to allow you to look up Perl and Python statements and functions, and find out what you should use in both languages.
Anybody who has used Perl and who wants or needs to learn Python will find this book useful. The book gives a full guide on the basics of programming in Python from the perspective of a Perl programmer. Right from the basics of line and program structure all the way up to object-orientation and using external modules.
You dont need to be an expert Perl programmer , you just need to be able to understand the basics. Ive tried to make the text as easy to read and follow as possible. However, on occasion things get technical, and to get the best out of the book you should at least know how to use and create your own functions, extension modules and classes and objects.
Without trying to give you the plot before youve read the book, if you know how to work with references, classes and objects in Perl, then your migration to Python is already 50% there!
The book is split into three main sections. The first section gives you an introduction to Python, including information on the basic mechanics of the language and information on who and where it is used.
Section 2 concentrates on the fundamentals of the language from the basics of expressions and statements through to comparing Pythons object types with Perl variables. Well also look at creating functions, error handling, and extending Python with modules and classes.
The last section concentrates on applying Python in real situations compared to the equivalent Perl process, function or module. For example, Chapter 8 looks at Pythons built-in functions and their Perl equivalents, whilst Chapter 10 looks at manipulating data and regular expressions.
There are also two appendices at the back of the book. Appendix A provides a handy guide to getting more information about Python, including other books, websites, newsgroups, mailing lists and details on how to get the Python documentation to help you with your programming. Appendix B is a quick reference it lists every Perl token, variable or function and provides you with the Python alternative. Where possible, it will also direct you straight to the page elsewhere in the book where you can find more information on the Python alternative.
If you are completely new to Python but know Perl then read Sections 1 and 2 first. If youve played with Python but never used it in anger, then start with Section 2. Finally if you know Python and just want quick reference information then use Appendix B to look up the Perl fragment you are looking for, or go straight to the suitable chapters in sections 2 and 3.
If you dont know Perl, then this book will not be a lot of help. Try reading Perl: The Complete Reference 2nd Edition (Osborne/McGraw-Hill, 2001) or Learning Perl, 2nd Edition (OReilly, 1997).
There are fourteen chapters and two appendices in the book. Throughout the book examples of a Perl expression or statement are given, along with the equivalent Python version and information on why the changes and modifications are required. The rough content of each chapter is as follows:
Chapter 1, Introduction An overview of Python covering information on its history and who uses the language.
Chapter 2, Executing Python Programs Python is much more of an interactive language, so before we look at the specifics of Perl and Python programs, well look at how to use and execute Python programs.
Chapter 3, Python Overview Our first look at how Perl and Python differ from an overall structure and approach perspective.
Chapter 4, Components of a Python Program Details the statements, data types and operators supported by Python and how they relate to their Perl equivalents.
Chapter 5, Functions How to create and use Python functions and to migrate your Perl functions and argument handling into Python.
Chapter 6, Exceptions and Error Trapping The mechanics of Pythons exception system which is used throughout the language as a method for highlighting errors, right from parsing the source code through to creating and raising your own exceptions to indicate errors.
Chapter 7, Modules, Classes, and Object Orientation Information on how to extend your Python applications with modules and classes, and how these relate to the Perl module and class structure.
Chapter 8, Built-in Functions Quick guide to the functions that Python supports in the native interpreter.
Chapter 9, Interfacing with the Operating System Details on how to communicate with the operating system, including information on getting command-line arguments, determining your environment and communicating with and starting new processes.
Chapter 10, Data Manipulation How to manipulate numbers, strings, and work with regular expressions in Python.
Chapter 11, File Manipulation File processing and management, from reading and writing to files through to managing directories, file permissions and controlling access.
Chapter 12, Communicating Over a Network Details the processes behind communicating with network services, both from the perspective of a client and a server.
Chapter 13, Web Development A quick guide to programming for the web under Python.
Chapter 14, GUI Development with Tk Information on how to migrate your user interfaces from Perl/Tk to Pythons Tkinter system.
Appendix A, Python Resources A guide to the books, mailing lists, websites and online documentation available for information on Python.
Appendix B, Perl to Python Quick Reference A cross reference that lists all the Perl tokens, variables and functions used in Perl and the corresponding Python equivalent.
All Perl keywords are highlighted in bold, but functions are listed without parentheses. This is because the C functions on which the Perl versions may be based are shown like this().
Examples and code are displayed using a fixed-width font.
Function descriptions are formatted using the same fixed-width font.
Note: notes are formatted like this and include additional information about a particular topic. Youll also find similarly formatted "Warnings," which highlight possible dangerous tools or tricks to watch out for when programming.
I always welcome comments and suggestions on my work. The best way to contact me is via e-mail. You can use either firstname.lastname@example.org. Alternatively, visit my website, http://www.mcwords.com, which contains resources and updated information about the scripts and contents of this book. You can find the homepage for this book at http://www.mcwords.com/projects/books/p2py/.