UrbanPro
true

Learn Perl Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

AUTOLOAD subroutine

Sudheer S
18/11/2016 0 0

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.

 

 

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

PEP (Python Enhancement Proposals)
Python Enhancement Proposals(PEP) are suggestions for improvements to the language, made by experienced Python developers. PEP 8 is a style guide on the subject of writing readable code. It contains a...

The Zen of Python
Writing programs that actually do what they are supposed to do is just one component of being a good python programmer. It's also important to write clean code that is easily understood, even weeks after...

Use IPython shell for better understanding of python
Hi All, Even though there are many editors like IDLE, PyDev, PyCharm are available for python, it is very advantageous to use ipython as an interactive shell for Pyhon. How to install it? pip install...

What is a generator?
A generator is a function that has one or more yield statements. Example: >>>def gen_demo(a): yield a a = a+10 yield a a = a+30 yield a >>>gen_a...

Debugging Python code
If you are getting an error while executing your python code, the better way of running your python code is by using -i switch. ex: python -i scriptname.py once you run the above command, your execution...
X

Looking for Perl Training Classes?

The best tutors for Perl Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Perl Training with the Best Tutors

The best Tutors for Perl Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more