in reply to (Ovid) Re: Autoload'ed class method calls
in thread Autoload'ed class method calls

Ovid, not sure that hackdaddy has got it wrong. His code is the AUTOLOAD sub, not the constructor. I presume he will be supplying his own new sub so that AUTOLOAD will not be called on construction.

More importantly, he needs to declare $AUTOLOAD in package namespace, with

use vars qw($AUTOLOAD);
or
our $AUTOLOAD;
The rest seems to go through use strict 'refs'OK.

Update: Ovid I am humbled! It's interesting how staring at a piece of code can lead you to wrong conclusions (in my case exacerbated by lack of sleep and coffee).

It also shows what a snarfy beast AUTOLOAD can be. I've written pseudo-methods in AUTOLOAD myself, but usually solved problems like hackdaddy's by stepping through with the debugger.

Replies are listed 'Best First'.
(Ovid) Re(3): Autoload'ed class method calls
by Ovid (Cardinal) on Apr 05, 2002 at 18:46 UTC

    Read through his code and error message again. hackdaddy already has $AUTOLOAD declared properly, otherwise the first method call would fail, but according to hackdaddy, it's successful. However, read through the text of the actual error message.

    Can't use string ("WM::Install::Table::ArchiveTbl") as a HASH ref whil +e "strict refs" in use at /u08/pkg/wms/wmcmtools/modules/WM/Install/Table/ArchiveTbl.pm line +192.

    Now look at first few lines of the autoload.

    sub AUTOLOAD { my ($self, $newval) = @_; $AUTOLOAD =~ /.*::get(_\w+)/ and $self->_accessible($1) and return $self->{$1};

    As you can see, the return statement (return $self->{$1}) is attempting to use $self as a hashref. Since the error messsage shows that the programmers is trying to use the package name as a hashref, we clearly have the package name and not the object being passed as the first argument to AUTOLOAD. I was using a generic constructor to illustrate how the first argument could be a package name instead of an object, as expected.

    Cheers,
    Ovid

    PS: I'm also a Terry Pratchett fan (I'm assuming that's what your nick refers to).

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.