Skip to content
Related Articles

Related Articles

Improve Article
Perl | AUTOLOAD Function
  • Last Updated : 21 Nov, 2019

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';

Parameters:
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

our $AUTOLOAD;

Example:
Here a look to an example of Perl to call an undefined function




#!/usr/bin/perl
use strict;
use warnings;
  
print "Geeks For Geeks\n";
  
report_protein_function("one", "two");
  
print "A computer portal for every geek\n";

Output:



output 1

As you can see an error has occurred in the program. Now We will use the autoload method in the program




#!/usr/bin/perl
use strict;
use warnings;
use vars '$AUTOLOAD';
  
print "Geeks For Geeks\n";
  
report_protein_function("one", "two");
  
print "A computer portal for every geek\n";
  
# AUTOLOAD() Function
sub AUTOLOAD 
{
    print "AUTOLOAD is set to $AUTOLOAD\n";
    print "with arguments ", "@_\n";
}

Output:
output 2

Redispatching Methods in AUTOLOAD()

The 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.
With NEXT it is possible to implement an AUTOLOAD method that only handles method calls of the form get_ and set_ and is effectively invisible to any other method requests.

The implementation would look like this:




sub AUTOLOAD
{
    $AUTOLOAD =~ s/.*:://;
    if ($AUTOLOAD =~ /^get_\w+$/) 
    {
        # Handle getting...
    }
    elsif ($AUTOLOAD =~ /^set_\w+$/) 
    {
        # Handle setting...
    }
    else 
    {
        # Decline to handle,
        # passing the request on to someone else...
        shift->${\"NEXT::$AUTOLOAD"}(@_);
    }
}

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 AUTOLOAD().
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 AUTOLOAD().

Drawbacks of AUTOLOAD()

Here Listed are a few of the drawbacks of autoload()

  1. 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.
  2. It delays the loading of subroutines.
  3. Autoload Performing speed is very load



My Personal Notes arrow_drop_up
Recommended Articles
Page :