in reply to Re: Re: Re: autoload and packages
in thread AUTOLOAD and packages

Thanks Svad ---remember, friends don't let friends code drunk :*)

I'll post the entire module later, as I guess I can't explain what I am trying to do...unfortunately this is normal as I am learning perl by trial and error...

I know that I can check to see if main::AUTOLOAD is defined and I guess I could die() if it is...I just was wondering if I could save the function pointer and insert mine in its place. Then I would call the original one and no one would be the wiser ;-)

I want to create a module that you can just "use" and it would take care of "fixing" any AUTOLOAD requirements.

Of course if I wanted to not have a dynamic function name none of this would be an issue...but what fun is that !

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: autoload and packages
by svad (Pilgrim) on May 14, 2002 at 17:26 UTC
    if we'll see into perlref.pod and search for substring {CODE} and read around a bit, then we'll calmly understand that we're able to do following trick:
    use strict; package main; sub AUTOLOAD { return 'old-main-autoload'; } package mypackage; # let's capture that ::AUTOLOAD my $aasub = *{$::{AUTOLOAD}}{CODE}; # now let's, as we decided earlier, have it overloaded *::AUTOLOAD = sub { # here how we overload it: print "do first\n"; #call old one and print that returned value inside '[]' print '[', $aasub->(), "]\n"; print "do last\n"; }; package main; # again flurfish_sub();
    in my case output is
    do first [old-main-autoload] do last
    But my personal advice - you're moving to C-like area or even assembler code catching area when you will really soon will complicate logic.
    I think when you will see that code in your program then it is a good signal to redesign your code.
    :)

    Best wishes,
    Vadim.