MathCurvesSurfacesWallpaper GroupsGallerySoftwarePOV-Ray
ProgramingLinuxPerl PythonHTMLCSSJavaScriptPHPJavaEmacsUnicode ♥
Web Hosting by 1&1

Python & Perl: Classes and Objects

Xah Lee,

Python

In Python, you can define a boxed set of data and functions, which is traditionally known as “class”. Here's a example.

# -*- coding: utf-8 -*-
# python

# in the following, we define a set of data and functions as a class,
# and name it xxx

class xxx:
     "A class extempore! =(^_^)="
     i=1 # i'm a piece of data
     def okaydokey(self): return "okaydokey"
     def square(self,a): return a*a

# in the following, we create a object of the class xxx.  This is
# called “instantiating a class”.
x = xxx()

# Data or functions defined in a class are called the class's
# attributes or methods. To use them, append a dot and their name
# after the object's name.
print 'value of attribute i is:', x.i
print "3 squared is:", x.square(3)
print "okaydokey called:", x.okaydokey()

# In the definition of function inside a class, the first parameter
# “self” is necessary. It is just side-effect of the language design.

# The first line in the class definition is the class's
# documentation. It can be accessed thru the __doc__ attribute.
print "xxx's doc string is:", x.__doc__

# var inside the class can be change like this
x.i = 400

# new data can be added to the class
x.j=4
print x.j

# A class's method can also be over-rided
x.square = 333
# (the following line will no longer work)
## print "3 squared is:", x.square(3)

In Python, one must be careful not to overwrite data or methods defined in a class.

Perl

Perl the language is not really Object Oriented. However, programing in OOP style can be done in Perl to some extent. (Note: perl supports functional programing much better than Python, and Python does not really support functional programing.)

To be able to program in OOP style in Perl, you need to understand well Perl's refrences.

Here is a quote from perldoc perlobj:

First you need to understand what references are in Perl. See perlref
for that. Second, if you still find the following reference work too
complicated, a tutorial on object-oriented programming in Perl can be
found in perltoot and perltooc.

If you're still with us, then here are three very simple definitions
that you should find reassuring.

1.  An object is simply a reference that happens to know which class it
    belongs to.

2.  A class is simply a package that happens to provide methods to deal
    with object references.

3.  A method is simply a subroutine that expects an object reference (or
    a package name, for class methods) as the first argument.

Programing OOP in Perl is rather complex, and not recommended. However, it is relatively easy to use packages that comes with a OOP interface. Here's some excerpt from the CGI package's documentation, which illustrates how to use packages with a OOP interface.

…
  CREATING A NEW QUERY OBJECT (OBJECT-ORIENTED STYLE):
         $query = new CGI;
…
  FETCHING THE NAMES OF ALL THE PARAMETERS PASSED TO YOUR SCRIPT:
         @names = $query->param
…
  FETCHING THE VALUE OR VALUES OF A SINGLE NAMED PARAMETER:
        @values = $query->param('foo');

                  -or-

        $value = $query->param('foo');
…

  SETTING THE VALUE(S) OF A NAMED PARAMETER:
        $query->param('foo','an','array','of','values');
…
blog comments powered by Disqus