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

#!/usr/bin/perl -w use strict; use lib '.'; use MyTest; my $x = "blah blah blah\n"; chomp($x); print "^$x\$\n";
The First Module
package 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 Second Try
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;
The Output ^blah blah blah$ What The Output Should Be ^blah blah blah_this is a test$

In reply to Overloading Perl's Built-In's by bbfu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.