Wednesday, May 11, 2011

How to Override Built-in Functions in Perl (localtime Example)

# Importing this module overrides the built-in 'localtime' function 
# so that it returns a Time::tm object instead of an array.
use Time::localtime;

# The overridden localtime now returns an object.
# Note: Passing 'time' as a parameter is redundant since it defaults to the current time.
print "Object: ", join(' ', localtime), "\n";

# If you need to access the original, built-in localtime function 
# (which returns an array of time data), you must use the CORE:: namespace.
my @timeData = CORE::localtime(); 
print "Array: ", join(' ', @timeData), "\n";
print "\n\n\n";

exit;

No comments: