vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

Is the autoload functions better way to write functions in a program which need 50 no. functions instead of writing all the functions ?

Replies are listed 'Best First'.
Re: autoload usage
by Corion (Patriarch) on Feb 02, 2009 at 11:36 UTC

    Also, if you know the function names beforehand, there is no need to use AUTOLOAD. For simple object accessors, there is Class::Accessor and for others stuff you can do it like this:

    #!perl -w use strict; sub gen_func { my ($package,$name,$code) = @_; no strict 'refs'; *{"$package\::$name"} = $code; }; sub gen_printer { my ($package,$name) = @_; gen_func( $package, $name, sub { print "Hello $name\n" }); }; gen_printer(__PACKAGE__, 'ree'); ree();
Re: autoload usage
by moritz (Cardinal) on Feb 02, 2009 at 10:34 UTC
    If they all have the same (or rather similar) structure, yes.

    If they are all completely different: no.

Re: autoload usage
by Herkum (Parson) on Feb 02, 2009 at 15:40 UTC

    The problem with autoload is that it can hide misspelled methods. It can be hard to debug a program when you have calls that work, but the end result is different than what you expect.

    # Example: $self->callab1e( $value ); #vs. $self->callable( $value ); # Problem is ^. # A transcribed 'one' in place of an L. You # might never notice if your editor uses the # wrong font.

    I am not saying you should manually write 50 functions, but you should not blindly use autoload without understanding its short-comings.