NB: These pages were mostly written in 2001 or so. The résumé dates are accurate but the code is aged and unlike whiskey, 8 year-old code doesn't usually taste better. For a look at my current skills and to see my CPAN modules, sample code, and code discussions, please see these pages instead: Perl resources and sample code and PangyreSoft.
Object Oriented colors in Perl
Social links
View Ashley Pond V's profile on LinkedIn
Miscellaneous
Why not OOP?

OO modules are slower than procedural modules.
Often much slower. If you are writing small scripts that run briefly over and over and need every processor clock second to work, OOP may not be the right choice.

OO modules typically take longer to develop.
A large part of the beauty of Perl is being able to have a project timeline of 2 weeks when the C programmers down the hall are doing something similar in 4 months (your mileage may vary, but there is no doubt whatsoever that Perl is faster to develop in than C). Doing it in OO might mean doubling the initial time spent on a program.

I can do all the same things procedurally.
You may be able to but consider this analogy. Procedural programming is like using a block of pig iron and a piece of oak as your primary tools to build a house. The combination can drive nails, plane surfaces, and even be forged into more complex tools as long as you're ready to forge it back when you need something else. You must always keep track of its parts and its purpose (driving, cutting, fixing, raising, leveling).

OOP is more like crafting the tools once and being done with it. You no longer need to give your hammer an iron head, an oak grip, and nails because your hammer is complete and it knows that it's for nails (and thumbs, of course).

OO is a fad.
Well, everything in life but war is a fad. It's an unfortunate situation that many IT managers are not programmers themselves, or that the recruiters and hiring managers who are responsible for staffing IT departments have no working technical knowledge. These individuals will latch onto buzz terms like OOP simply because they recognize them. Clean living is the best revenge, though, and many of these kids are out of work now that the venture capital has gone away.

Okay, then why OOP?


Other pages

Introduction

OOP. Object oriented programming. The reason some say Ruby is better than Perl. The reason some say Perl is better than Ruby. Most job ads require applicants have knowledge of it. It’s simultaneously fad and revolution, silly gimmick and sanity savior.

OOP is often feared. “How much extra programming will I have to do?” “How difficult is it to pick up?” “Won’t the internals be hopelessly obfuscated?” “It’s slower than an equivalent procedural script, isn’t it?”

The task

We are going to develop an object oriented module. It will...

  1. provide methods (OO subroutines) to easily print colored HTML text
  2. support any named color (eg, “blue” or “green”) recognized by browsers, so these will work:
    1. print $color->magenta("Wow, what a color!");
    2. print $color->vermilion("I can't believe this works.");
  3. also support any hexadecimal colors (e.g.: “#DF303C”) with a method for each, so these will work:
    1. print $color->DF303C("Colored text here");
    2. print $color->336699("This is a websafe color");
  4. also support any user defined colors with a method for each, so something like these will work:
    1. print $color->virgin_green("Underage ETs!");
    2. print $color->HausBraun("Not to be confused with Eva.");
    3. print $color->pantone_process_blue("Yawn...!");
  5. and because of 2-4, the modules will contain an unlimited number of methods

Sound difficult? Like most everything in Perl it’s not, once you know how it can be done. We can do this in about 10 lines of code. No lie. Take a look.

OOP color module (take 1, miniscule)
Usage (output is for use in HTML)
my $cm = OOPminiscule->new();  # "cm" for color maker

print 
    $cm->red("HEY! That's incorrect."),
    "<br />\n",
    $cm->D71986("Wha! That's not a websafe color!"), 
    "<br />\n",
    $cm->blue("* please see the instructions");
HEY! that's incorrect.
Wha! That's not a websafe color!
* please see the instructions

…what?! First you should know, in case you don’t already, the code won’t work for some cases in Task item 3 (3.b. would break the module, for example) and won’t work at all for case 4 yet. Let’s come back to this ungodly idiom in minute.

Wait, what’s OOP again?

In the perldoc perltoot Perl demi-god, Tom Christiansen, describes object oriented programming this way (quote is abbreviated):

An object is nothing but a way of tucking away complex behaviors into a neat little easy-to-use bundle. This is [called] abstraction… …The heart of objects is the class, a private namespace full of data and functions. Objects “live” in a class, meaning that they belong to some package.

It’s simple once you get it. An object is just a reference, a pointer, to some data which is aware of its own namespace (methods/subroutines that belong to it). It can contain any number or style of datum and have easy access to functions from its class or its relatives (parents, super classes, etc).

Page 2, OOP

Search these pages via Google
Text, original code, fonts, and graphics ©1990-2009 Ashley Pond V.