Whenever you call a subroutine that does not exist in a package, it raises an error. If you want to handle this scenario, you can include AUTOLOAD subrotine in your package. With AUTOLOAD you can call a subroutine of a package that does not exist, by that the code defined in the AUTOLOAD subroutine executes. AUTOLOAD subroutine can be defined as follows:
Student.pm:-
package Student;
sub new{
my $class = shift;
return bless {}, $class;
}
sub AUTOLOAD{
our $AUTOLOAD;
print $AUTOLOAD;
}
1;
script: usestudent.pl:-
use Student;
my $s = Student->new();
print $s->getName();
Explanation:-
getName subroutine is not defined in the Student module, by calling that getName subroutine, AUTOLOAD subroutine is executed and the output is as follows:
Student::getName
by declaring $AUTOLOAD using "our", perl interpreter assigns the namespace Student::getName to $AUTOLOAD
This is helpfull when we writing lot many subroutines in the package and forget to write one of the subroutines. When we execute the script containing calls to not defined subrotines, we will come to know what subroutines we forgot to define. Even while implemening a GUI application, all the button clicks that are not defined will not lead to any error, instead they do nothing or the print message defined in AUTOLOAD is displayed.