|
Object Oriented colors in Perl
|
Social links
Class::Prototype
WWW::Spyder Javascript tricks serial() join function Smart quotes Text to Excel Developing Featherweight Web Services with JavaScript
Miscellaneous
Why not OOP?
OO modules are slower than procedural modules.
OO modules typically take longer to develop.
I can do all the same things procedurally. 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. |
|
| Object Oriented colors in... |
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...
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) package OOPminiscule; #===================================================================== sub new { return bless \$empty_scalar, shift } #===================================================================== sub AUTOLOAD { no warnings; my ( $color ) = $AUTOLOAD =~ /^.+::(\w+)$/; $color = $color =~ /^[A-F0-9]{6}$/ ? "#$color" : $color; *{$AUTOLOAD} = sub {qq|<span style="color:$color">| . join($",@_[1..$#_]) . "</span>\n" }; &{$AUTOLOAD}; } 1; 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 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). |
|
|
Perl Books ·
CPAN ·
mod_perl ·
Perl Monks ·
Perl Mongers ·
Perl Journal ·
Use Perl ·
Perl Jobs ·
ActiveState ·
perldoc.perl.org ·
O’Reilly Perl ·
W3Schools tutorials ·
Ovid's CGI Course ·
Catalyst ·
Perl at Wikipedia
Text, original code, fonts, and graphics ©1990-2008 Ashley Pond V. |
||