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 20:47:37)
Associated topics are indexed under
G996 - Well House Consultants - Newsletter Scripts [2539] Changing Images - (2009-12-17)
[2433] Controlling, supressing, enabling PHP error messages - (2009-10-02)
[2360] Error Handling in Lua with assert and pcall - (2009-08-13)
[2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27)
[2145] Using the internet to remotely check for power failure at home (PHP) - (2009-04-29)
[2046] Finding variations on a surname - (2009-02-17)
[1954] mod_rewrite for newcomers - (2008-12-20)
[1813] Ajax - going Asyncronous and what it means - (2008-09-28)
[1743] First class functions in Lua lead to powerful OO facilities - (2008-08-07)
[1601] Replacing the last comma with an and - (2008-04-04)
[1505] Script to present commonly used images - PHP - (2008-01-13)
[1487] Efficient PHP applications - framework and example - (2007-12-28)
[1387] Error logging to file not browser in PHP - (2007-10-11)
[1321] Resetting session based tests in PHP - (2007-08-26)
[1217] What are factory and singleton classes? - (2007-06-04)
[1123] mysqldump and mysqlrestore - (2007-03-30)
[1066] Final, Finally and Finalize - three special words in Java - (2007-02-05)
[1001] .pdf files - upload via PHP, store in MySQL, retrieve - (2006-12-19)
P218 - Perl - More Objects [2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
[2427] Operator overloading - redefining addition and other Perl tricks - (2009-09-27)
[1949] Nuclear Physics comes to our web site - (2008-12-17)
[1819] Calling base class constructors - (2008-10-03)
[1664] Example of OO in Perl - (2008-06-03)
[1435] Object Oriented Programming in Perl - Course - (2007-11-18)
[1320] Perl for Larger Projects - Object Oriented Perl - (2007-08-25)
[930] -> , >= and => in Perl - (2006-11-18)
[831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
[656] Think about your design even if you don't use full UML - (2006-03-24)
[592] NOT Gone phishing - (2006-02-05)
[588] Changing @INC - where Perl loads its modules - (2006-02-02)
[531] Packages in packages in Perl - (2005-12-16)
[246] When to bless a Perl variable - (2005-03-15)
[227] Bellringing and Programming and Objects and Perl - (2005-02-25)
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