What is @ISA in Perl?

Jim picture Jim · Jul 29, 2013 · Viewed 7.9k times · Source

If I have a module with methods a and b and want to export them I do:

use Exporter;  
our @ISA = qw (Exporter);  
our @EXPORT = qw (a b);  

What I don't understand is what does this line:
our @ISA = qw (Exporter); do?

Answer

friedo picture friedo · Jul 29, 2013

The @Foo::ISA array is a global variable that holds the classes from which class Foo inherits.

Generally, you shouldn't manipulate @ISA directly; doing so is a sign that you're probably looking at old Perl code.

If you want to declare an inheritance relationship, a better way to do it is

use parent 'Exporter';

which tweaks @ISA behind the scenes for you and also does some other useful stuff.

In your case, when you do

our @ISA = qw(Exporter)

you're declaring your class as a subclass of Exporter. The Exporter class provides a method called import. Why do you want that? Because when you say

use MyClass;

What actually happens is:

BEGIN { 
    require 'MyClass.pm';
    MyClass->import;
};

The import method is automatically called every time someone uses your class. That method can do anything you want. (You could write your own import if you want to) but usually it's used to import symbols into the caller's namespace. That's what Exporter does for you.

However, Exporter also exports its own import method, so you don't actually have to inherit it any more. (This was fixed a loooong time ago.) So now you can just say

use Exporter qw(import);

and your package will get the import method without having to mess with @ISA at all.