Search and Find
More of the content
Scripting with Objects - A Comparative Presentation of Object-Oriented Scripting with Perl and Python
During the last several years, scripting languages have become just as important as systems programming languages. Thus it felt natural to follow my Programming with Objects (PWO) with Scripting with Objects. Programming with Objects was a comparative presentation of C++ and Java, two dominant languages of that genre. In the same vein, Scripting with Objects is a comparative presentation of Perl and Python, two dominant languages of the genre that these languages represent.
Scripting with Objects is based on the same overall philosophy as PWO, that, in addition to its syntax, it is essential to depict a programming language through its applications in order to establish fully its beauty and power. Teaching a programming language divorced from its applications would be akin to teaching English through its grammar alone.
This book is not intended for a reader who wants to acquire quickly the rudiments of a scripting language for solving some problem that the he or she has in mind. Rather, this book is designed for a reader who desires to acquire a more comprehensive and expansive perspective on scripting by simultaneous exposure to two languages.
There is an adage that says that you can never understand one language until you understand at least two.1 This adage goes straight to the heart of becoming a good programmer these days and developing a good sense of the range of programming ideas that are out there. While learning a single language will equip you with the syntax, it will not necessarily give you an appreciation for all of the nuances associated with the usage of that syntax. Learning two languages (and, if at all possible, multiple languages) that are at once similar and different is perhaps the most effective way to appreciate those nuances. In the old days, the more serious students of English literature would often learn Latin as well as a number of other Romance languages such as French, Italian, and Spanish. This was certainly true of all of the noted British poets, novelists, and playwrights of the eighteenth and nineteenth centuries. Learning multiple languages gave them the ability to create literary and linguistic effects in their native tongue that otherwise would not have been possible.
However, learning two languages at the same time does create its own challenges, especially when the languages are as large as Perl and Python. A beginner can easily become confused as to which syntactical construct belongs to what language. Additionally, certain habits one quickly acquires in one language (such as terminating every Perl statement with a semicolon and using just indentations for indicating block termination in Python) can reflexively manifest themselves in the other language. With regard to the first difficulty, note that practically all modern programming requires that you keep the documentation page open in one window as you are programming in another window. All of the major modern languages have become so large that it is impossible to commit to memory all of the functionality in all the modules of a language. So, if you really think about it, working with two programming languages in this day and age should be no different than working with one programming language. In either case, you will be looking up the documentation as you are writing your program or script. The second issue — the data entry habits of one language interfering with the writing of programs in the other language — is a more significant problem, but one that can nevertheless be overcome with practice.
Organization of the Book
This book’s treatment of Perl and Python is mostly comparative, all the way from the basic syntax of the two languages to writing application-level scripts. By comparative, I mean that similar scripting concepts in the two languages are explained with identical examples in most cases. In many chapters, I have followed a Perl presentation of a scripting concept with a Python presentation of the same concept. In other chapters, it seemed more appropriate to present first all of the Perl-related notions, followed by the corresponding Python-based notions.
The book begins in Chapter 1 with the presentation of a multilanguage view of modern application development and the importance of scripting languages (and their differences from the mainstream systems programming languages). The basic Perl syntax is reviewed in Chapter 2, and the basic Python syntax in Chapter 3. Chapter 4 then provides a review of regular expressions and how to use them in Perl and Python. Chapter 5 presents the notion of a reference in Perl. These five chapters constitute the basic scripting material in the book.
With Chapter 6, we begin the presentation of object-oriented scripting, the focus of that chapter being on the notion of a class in Perl. Chapter 7 then follows with a similar discussion but in Python; this chapter also introduces the reader to the new-style classes in Python. Chapter 8 discusses inheritance and polymorphism in Perl, with Chapter 9 doing the same for Python. Chapter 10 is devoted to the topic of exceptions in Perl and Python. Throwing and catching exceptions have become central to object-oriented programming and scripting, so much so that now you see them being used even for affecting certain forms of flow of control. Chapter 11 shows how to define abstract classes and methods in Perl and Python. Abstract classes and methods play a very important role in object-oriented programming in general. They can be used as mixin classes to lend specialized behaviors to other classes and, by serving as root classes, they can help to unify the other classes into meaningful hierarchies. Chapter 12 goes into the issues that deal with memory management and garbage collection.
Application-level scripting starts with Chapter 13 where we take up the subject of writing scripts for creating graphical user interfaces. Chapter 14 presents multithreaded scripting. Multithreading plays an important role when scripts are written for graphical user interfaces and network applications. We take up the topic of network programming with Perl and Python in Chapter 15. Chapter 16 shows how to write scripts for interacting with databases. Finally, Chapter 17 presents scripting for processing XML. This chapter is one of the longest chapters in the book because the world of XML has literally exploded during the last few years. In addition to becoming the technology of choice for representing document content in a manner that is independent of how the document is displayed, XML is also emerging as the lingua franca for the interoperability needed for the rapidly emerging business of web services.
The book is designed both for a one-semester undergraduate course on advanced scripting (this would be for students with prior background in the basics of Perl and Python), and a two-semester undergraduate program for students who will be experiencing scripting for the first time. When used in a one-semester advanced scripting course, the recommended starting point in the book would be Chapter 6. For such a course, the first five chapters are intended to serve as basic reference material. When used for a two-semester educational program in scripting, my recommendation would be to cover roughly the first 500 pages in the first semester and the rest in the second semester.
Figure 0.1 is a recommendation for the use of the book for a one-semester undergraduate course on advanced scripting. Note that there are definite advantages to having all of the reference material needed for advanced instruction in the text itself. Chapter 12 is optional and depends on the degree to which the instructor wants to emphasize memory management issues. After Chapter 12, all of the application chapters can be taught in any order. In longer chapters, such as Chapter 17 on XML, the instructors may wish to only address the material they would like to emphasize.
Fig. 0.1 For a One-Semester Undergraduate Course on Advanced Scripting
Figure 0.2 outlines the recommended use of the book for a two-semester program of instruction in scripting. I believe that a two-semester program would result in a quicker rate of learning, thus making it possible to accommodate a sophisticated project in the latter half of the second semester.
I will be glad to share my teaching materials with the prospective instructors planning to use this book as a text. I would also appreciate hearing from the readers about any typos and other errors they may find. All corrections received will be posted at
www.scripting-with-objects.com and the authors of the corrections duly acknowledged. The corrections and other feedback can be sent to me directly at email@example.com. I would also love to hear from the readers if I have slipped up in making proper attributions to other authors. When an example script in this book was inspired by some material I saw elsewhere, I have acknowledged the author of that material in a footnote or in the "Credits and Suggestions for Further Reading" section at the end of the chapter.
Fig. 0.2 For a Two-Semester Program of Courses Devoted to Scripting
Before ending, I would also like to add that this book should be useful for those who are making a...