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

Thanks, I kinda get it...but if there is a main::AUTOLOAD, do I have to "rename" it? As it seems "*::main::AUTOLOAD = \&::mypackage::my_lovely_autoload_for_main;" will override it and then I can't call the original main::AUTOLOAD... Am I missing something?

Replies are listed 'Best First'.
Re: Re: Re: autoload and packages
by svad (Pilgrim) on May 12, 2002 at 00:12 UTC
    okay, you decided not to do exercise for a reader :)
    Let me give you my code for example for a reader:
    BEGIN{ if (exists $::{AUTOLOAD}) { print "exisa\n"; } else { print "no-exisa\n"; } } sub AUTOLOAD{} BEGIN{ if (exists $::{AUTOLOAD}) { print "exisa\n"; } else { print "no-exisa\n"; } }
    Sorry for not clearing it for cleverness, I'm drunk now :)
    but I checked it, it works!
      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 !

        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.