Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-26 00:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found