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

I get the function name in the autoload function by using the $AUTOLOAD variable,but how can I get the arguments which i passed for the undefined functions ?

Replies are listed 'Best First'.
Re: autoload usage
by moritz (Cardinal) on Feb 02, 2009 at 11:04 UTC
    They come in via @_, as documented in perlsub.
Re: autoload usage
by perlthirst (Scribe) on Feb 02, 2009 at 11:28 UTC

    For your Reference

    use strict; use warnings; testing("ARG1"); sub AUTOLOAD { my $pg = our $AUTOLOAD; print "Function Name ->$pg<-\n"; print "Arguments ". join "\n", @_; }

    Output will be:
    Function Name ->main::testing<-
    Arguments ARG1

Re: autoload usage
by lakshmananindia (Chaplain) on Feb 02, 2009 at 13:02 UTC

    You can get the arguments by using the @_ similar to other user defined functions.

Re: autoload usage
by targetsmart (Curate) on Feb 06, 2009 at 17:20 UTC
    I admire the autoload feature of perl, I generally use this autoload for method making(accessors for object/instance specific get/set variables).

    for eg,

    sub AUTOLOAD { my $Self = $_[0]; my $CalledAs = $AUTOLOAD; $CalledAs =~ s/^.*:://; return if $CalledAs =~ /DESTROY/; $code = qq { sub $CalledAs { my \$Self = \$_[0]; \@_ > 1 ? \$Self->{\$CalledAs} = \$_[1]: \$Self->{\$Called +As} ; } }; eval $code; if($@){ die "Error defining sub routine, $CalledAs,$code, $@\n"; } goto &{$AUTOLOAD}; }
    If you defined your autoload subroutine like above inside any of your package, you are reducing lot of coding, because with the help of above routine you achieve some thing like this.

    $SomeObject->name("john");
    # will set hash $SomeObject's hash like $SomeObject{'name'} = "john";
    # later you can retrieve the value using
    $SomeObject->name(); # will return "john";
    easy isn't it.

    but defining a sub-routine for every attribute like name, age, address, etc., is quite faster in terms of access than this way of creating subroutine in autoload. But the above method reduces lot of code.


    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.