smackdab has asked for the wisdom of the Perl Monks concerning the following question:

Trying to use autoload for the first time ;-)

I can create a "sub AUTOLOAD" in main:: to dynamically call a function and it works cool!

Now, I want to move the stuff into a package called foo::

But, the docs say (I think) that there can only be one sub AUTOLOAD defined in each package...I was thinking I could use BEGIN in my package to create the main::AUTOLOAD to do what I want...but this assumes that the user hasn't already created a main::AUTOLOAD....I am stuck

(pseudo code)
package main; myfunc(1,2,3); # I want to redirect to foo::myfunc() sub AUTOLOAD { if $caller eq "foo" foo::myfunc(@_); } # Any way to get rid of the main::autoload and somehow # handle it in the foo package ??? Or is there a way # to override AUTOLOAD in my package using BEGIN or ???

thanks!!!

uc(autoload) in title - dvergin 2002-05-11

Replies are listed 'Best First'.
Re: autoload and packages
by svad (Pilgrim) on May 11, 2002 at 09:26 UTC
    you could either use Exporter module, or, even simpler, following approach:
    package mypackage; sub my_lovely_autoload_for_main { # bla bla bla print STDERR "[@_]\n"; } *::main::AUTOLOAD = \&::mypackage::my_lovely_autoload_for_main; package main; fjkre('wdw');
    Vadim

    PS. Checking for existing main::AUTOLOAD and calling at appropriate time is left as exercise for reader :)

      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?
        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!
Re: autoload and packages
by TheHobbit (Pilgrim) on May 11, 2002 at 09:54 UTC

    Hi,
    No need to use AUTOLOAD here, simply use Exporter as in

    package foo; use Exporter; use vars qw(@ISA @EXPORT); @ISA=qw(Exporter); @EXPORT=qw(myfunc); sub myfunc { #something clever; } package main; use foo; #imports myfunc, so that now # main::myfunc is an alias for # foo::myfunc myfunc(1,2,3); #calls foo:myfunc(1,2,3)

    Autoload is usefull for other things, like automatic generation of functions.

    Cheers


    Leo TheHobbit

    Update minor typos corrected, thanks to gmax.
      Thanks, but my function name is dynamic...I guess the "example" was too simplistic...Vadim's approach sounds like a bit of work, but would work...
Re: AUTOLOAD and packages
by strat (Canon) on May 13, 2002 at 10:14 UTC
Re: AUTOLOAD and packages
by svad (Pilgrim) on May 15, 2002 at 16:47 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 { print "do first\n"; #now 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.

A reply falls below the community's threshold of quality. You may see it by logging in.