Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Object-Oriented Programming In Perl (part 2)
Object methods and properties, inheritance, overrides and destructors - they're all here!

| Building Your Very Own Automobile |

Before we get into the nitty-gritty of object methods and properties, I want to take a minute to make sure that we're all clear on the basics. And I'm going to use an example I've used before to communicate the concept to those of you who may still not be too comfortable with it.

Take your car, the vehicle that transports you from place to place, and consider this, within the framework of OOP, as an "object". This object has certain basic properties - shape, size, colour - and certain basic functions - start, stop, accelerate and decelerate.

Now take it one step further. Every car can, in fact, be considered a subset of the class Automobile, which defines the basic characteristics and functions of every automobile on the planet. Once the class Automobile spawns a new car, or "object", its individual characteristics (color, shape) can be changed without affecting either the parent class or other spawned objects.

In Perl, a class is equivalent to a package - and the package might look something like this:


# this is Automobile.pm
# a Perl module to create new automobiles

package Automobile;

# constructor
sub new
{
my $this = {};
bless $this;
return $this;
}

# subroutine (aka "object method")
sub init
{
# code to initialize variables (aka "object properties")
# this won't stay empty for long - keep reading!
}

# a few other object methods
sub start
{
# some code
}

# a few other object methods
sub stop
{
# some code
}

sub accelerate
{
# some code
}

sub decelerate
{
# some code
}

# remember to end the module with this
1;


In order to use this, you simply need to instantiate a new object in your Perl script by calling the constructor, like this:


#!/usr/bin/perl

use Automobile;

# create objects
$his = new Automobile;
$hers = new Automobile;

# run a few object methods

# let's take this baby out for a spin
$his->start();

# well, hello there, pretty lady!
# care to join me?
$hers->start();

# this is fun!
$his->accelerate();
$hers->accelerate();

# was that a speed trap?
$his->decelerate();
$hers->decelerate();

# busted!
$his->stop();
$hers->stop();


You're already familiar with the method of creating a new object - the example above simply demonstrates the manner in which other methods can be applied to the object. These methods are nothing more than subroutines within the package definition.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2018 Melonfire. All rights reserved
Terms and Conditions | Feedback