Utility methods (factorys) are often used to create objects which may be of any of one of a series of different classes, depending on the data passed in. Have a look at this Perl program which reads in data from a log file and creates a list of web site visit objects:
use visit;
open (FH,"ac_20080527");
while ($line = <FH>) {
$current = make visit($line);
push @samples,$current;
}
make visit is actually creating objects of type
robot or
human - which happen to be subclasses of
visit though they don't have to be. Here is the code:
package visit;
sub make {
package visit;
sub make {
$stuff = $_[1];
if ($stuff =~ /slurp|crawl|spider|bot/i) {
return (new robot($stuff));
} else {
return (new human($stuff));
}
}
The result is that the list in the main code ends up containing a list of objects of various types, and using polymorphic methods on those objects, very need code can be used to call in different features of each.
If you want to have a look at the complete source code of both the main application and the visit.pm module, you can open each of them in a separate window
here and
here. If you want to learn all about OO in Perl (it's great for larger scripting tasks) look at our
Perl for Larger Projects Course ... you've just missed one, I'm afraid ... but there will be another one along later this year!
(written 2008-06-04, updated 2008-06-05)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P218 - Perl - More Objects [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
[246] When to bless a Perl variable - (2005-03-15)
[531] Packages in packages in Perl - (2005-12-16)
[588] Changing @INC - where Perl loads its modules - (2006-02-02)
[592] NOT Gone phishing - (2006-02-05)
[656] Think about your design even if you don't use full UML - (2006-03-24)
[831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
[930] -> , >= and => in Perl - (2006-11-18)
[1217] What are factory and singleton classes? - (2007-06-04)
[1320] Perl for Larger Projects - Object Oriented Perl - (2007-08-25)
[1435] Object Oriented Programming in Perl - Course - (2007-11-18)
[1664] Example of OO in Perl - (2008-06-03)
[1819] Calling base class constructors - (2008-10-03)
[1949] Nuclear Physics comes to our web site - (2008-12-17)
[2427] Operator overloading - redefining addition and other Perl tricks - (2009-09-27)
[2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
[2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
[2811] Igloos melt in the summer, but houses do not - (2010-06-15)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
[2972] Some more advanced Perl examples from a recent course - (2010-09-27)
[3097] Making Perl class definitions more conventional and shorter - (2010-12-20)
[3098] Learning Object Orientation in Perl through bananas and perhaps Moose - (2010-12-21)
[3377] What do I mean when I add things in Perl? - (2011-08-02)
[3581] Perl - calls to methods that use => - what do they mean? - (2012-01-16)
[3941] Building an object based on another object in Perl - (2012-12-03)
[4096] Perl design patterns example - (2013-05-20)
[4098] Using object orientation for non-physical objects - (2013-05-22)
[4356] Object factories in C++, Python, PHP and Perl - (2014-12-19)
[4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)
G996 - Well House Consultants - Newsletter Scripts [1001] .pdf files - upload via PHP, store in MySQL, retrieve - (2006-12-19)
[1066] Final, Finally and Finalize - three special words in Java - (2007-02-05)
[1123] mysqldump and mysqlrestore - (2007-03-30)
[1321] Resetting session based tests in PHP - (2007-08-26)
[1387] Error logging to file not browser in PHP - (2007-10-11)
[1487] Efficient PHP applications - framework and example - (2007-12-28)
[1505] Script to present commonly used images - PHP - (2008-01-13)
[1601] Replacing the last comma with an and - (2008-04-04)
[1743] First class functions in Lua lead to powerful OO facilities - (2008-08-07)
[1813] Ajax - going Asyncronous and what it means - (2008-09-28)
[1954] mod_rewrite for newcomers - (2008-12-20)
[2046] Finding variations on a surname - (2009-02-17)
[2145] Using the internet to remotely check for power failure at home (PHP) - (2009-04-29)
[2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27)
[2360] Error Handling in Lua with assert and pcall - (2009-08-13)
[2433] Controlling, supressing, enabling PHP error messages - (2009-10-02)
[2539] Changing Images - (2009-12-17)
[3179] Oops - I typed ci not vi, and have lost my file ... - (2011-02-21)
Some other Articles
What are Unions (C programming)Talk on TransWilts train service to Green PartyChecking server performance for PHP generated pagesSlow boot and terminal start on Linux boxesFactory method example - PerlPython in an afternoon - a lecture for experienced programmersWestonbirt Arboretum PostcodeEquality, sameness and identity - PythonKorn shell - some nuggets