bbfu has asked for the wisdom of the Perl Monks concerning the following question:
I was looking at the Time::localtime module and noticed that it overloaded perl's built-in, localtime(). I was wondering how it it worked.
Looking to the internals, it seemed to me that it just exported (via Exporter) it's localtime() function. Nothing special. When I tried that, however, it didn't work.
Try as I might, I could not get my module to overload the built-in in the use'ing program. I thought I understood exporting fairly well. I even tried exporting by hand (via typeglob assignment, see The Second Try below) to no avail. (I know I'm doing this correctly because it works for variables and non-built-in functions.)
As far as I can tell, Time::localtime is not doing anything special besides exporting the function. There must be something I'm missing!
I've included a simple test program and two modules (the Exporter version and the typeglob version) below, as well as the output I get from the program and what the output should be.
Can anyone please help me to understand what's going on here
and what I'm doing wrong?
Thanks!
The Program
The First Module#!/usr/bin/perl -w use strict; use lib '.'; use MyTest; my $x = "blah blah blah\n"; chomp($x); print "^$x\$\n";
The Second Trypackage MyTest; use strict; BEGIN { use Exporter (); use vars qw(@ISA @EXPORT); @ISA = qw(Exporter); @EXPORT = qw(chomp); } sub chomp(@) { my $str = shift; CORE::chomp($str); return $str . "_this is a test"; } 1;
The Output ^blah blah blah$ What The Output Should Be ^blah blah blah_this is a test$package MyTest; use strict; sub import { *main::chomp = \&MyTest::chomp; # Doesn't work # *::chomp = \&MyTest::chomp; # Doesn't work either # *CORE::chomp = \&MyTest::chomp; # Nope # *main::chomp = \*MyTest::chomp; # Sorry, no go # *::chomp = \*MyTest::chomp; # I wish # *CORE::chomp = \*MyTest::chomp; # Please, give it up } sub chomp($) { my $str = shift; CORE::chomp($str); return $str . "_this is a test"; } 1;