in reply to Re^2: Using AUTOLOAD to create class methods
in thread Using AUTOLOAD to create class methods

There's no way you can do it perfectly reliably without AUTOLOAD because of Perl's poor introspection. And, as you note, if you miss a method you have a bug. You can either publish which methods you support (thus turning the bug into a "feature") or have your AUTOLOAD handle it. You might want to just check can (which returns a code ref). The following untested bit might help:

if (my $sub = $self->{DBI}->can($op)) { no strict 'refs'; *$op = $sub; }

You'd have to customize that for your needs, but it has the benefit of installing the subroutine directly into your namespace so you only take the AUTOLOAD hit on the first call.

Cheers,
Ovid

New address of my CGI Course.

Replies are listed 'Best First'.
Re^4: Using AUTOLOAD to create class methods
by diotalevi (Canon) on Nov 03, 2005 at 23:40 UTC
    Do you have any information that compares getting repeated hits to AUTOLOAD vs once and losing the method cache?

      I'm not sure what you mean by "once and losing the method cache". Perhaps there's something I didn't read in the thread.

      AUTOLOAD tends to be slow as Perl must walk through the inheritance tree (if any beyond UNIVERSAL) and search for an appropriate method. Then Perl goes back to the top and looks for AUTOLOAD methods. If it finds one (and you hope it's the right one if there's more than one), then Perl has to execute your AUTOLOAD code prior to getting to the correct method call. Whether or not this is a significant hit depends upon the how frequently AUTOLOAD is going to get called in your application.

      Cheers,
      Ovid

      New address of my CGI Course.

        Perl has a cache that it uses to avoid walking the inheritance tree, basically if you looked in this package and found the method over there, then it just returns the same method the next time. This speeds things up, but you need to remove the cache if anything happens that could make it invalid - such as defining a function somewhere. It will then build up again.

        So if you use an AUTOLOAD that autogenerates methods, then every time you hit that AUTOLOAD you throw away the method cache and have to start generating it again. Depending on your program's profile, this can be significant overhead.