In Perl, Autoload is a routine or a mechanism which is defined into a package. It is simple to use. It is applied whenever there is an undefined routine called within the package.
Without Autoload the undefined routines will produce error while the program is running. To locate a package or packages an Autoload subroutine is called with arguments of the undefined subroutines.
Syntax: To Use Autoload just put this syntax in your Perl program:-
use vars '$AUTOLOAD';
In $Autoload the name of the original subroutine appears in the global variable of the package that calls an undefined function.
In Newer versions of Perl, the syntax is
Here a look to an example of Perl to call an undefined function
As you can see an error has occurred in the program. Now We will use the autoload method in the program
Redispatching Methods in AUTOLOAD()
AUTOLOAD() function helps us to extract the name of an undefined package. This makes it to obtain an address of data from proxied objects from a scalar referenced.
The Proxy class writes the log and then re-dispatches to the target object.
NEXT it is possible to implement an
AUTOLOAD method that only handles method calls of the form
set_ and is effectively invisible to any other method requests.
The implementation would look like this:
Generating Code in AUTOLOAD()
Re-dispatching the methods is easy to do but it is inefficient. Every method call on the proxy end up failing in normal dispatch in
This makes the previous autoload method to closure bound the name of the undefined subroutine. This makes the code to invoke and directly goes to the result.
This method is easier cleaner and handles the behavior in
Drawbacks of AUTOLOAD()
Here Listed are a few of the drawbacks of autoload()
- Although this method is useful, but it is difficult to use it properly as it does not give proper information regarding the capabilities of object and class.
- It delays the loading of subroutines.
- Autoload Performing speed is very load
- Perl | abs() function
- Perl | hex Function
- Perl | exp Function
- Perl | sin() Function
- Perl | tell() Function
- Perl | chr() Function
- Perl | int() function
- Perl | cos() Function
- Perl | log() Function
- Perl | each() Function
- Perl | uc() Function
- Perl | ord() Function
- Perl | oct() Function
- Perl | join() Function
- Perl | rename() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.