6

I am curious. Most of Perl's implicitly called subroutines must be named in all caps. TIESCALAR, DESTROY, etc. In fact perldoc perltoot says

If constructors can have arbitrary names, then why not destructors? Because while a constructor is explicitly called, a destructor is not. Destruction happens automatically via Perl's garbage collection (GC) system, which is a quick but somewhat lazy reference-based GC system. To know what to call, Perl insists that the destructor be named DESTROY. Perl's notion of the right time to call a destructor is not well-defined currently, which is why your destructors should not rely on when they are called.

Why is DESTROY in all caps? Perl on occasion uses purely uppercase function names as a convention to indicate that the function will be automatically called by Perl in some way. Others that are called implicitly include BEGIN, END, AUTOLOAD, plus all methods used by tied objects, described in perltie.

Why then is the import subroutine left to be lower case? Does anyone have a good insight on this?

1
  • 1
    I'm afraid that there's no consistent reason, and that import is lower-case just because of historical reasons, legacy bla bla.
    – Alessandro
    Feb 19, 2011 at 17:15

3 Answers 3

6

I'd say that "import" is not called implicitly. It's an explicit call issued by implementation of use. To quote from perldoc use:

It is exactly equivalent to:

BEGIN { require Module; Module->import( LIST ); }

2
  • 1
    While I agree that this is possibly the reason, I would argue that from the standpoint of both the author and the user, this is implicitly called. Feb 19, 2011 at 16:53
  • Alternatively how is this different from tie calling TIESCALAR? I can see that DESTROY has more magic around it. Feb 19, 2011 at 17:00
3

To expand on DVK's answer a little, there are situations where you'd legitimately want to invoke import explicitly, for example when loading an optional module or auto-populating namespaces:

eval "require $modulename; $modulename->import( LIST ); ";

I can't think of any situation where you would ever want to invoke DESTROY, TIESCALAR, etc. explicitly.

1
  • I have used DESTROY explicitly to break circular references and make sure objects get garbage collected. You can usually do this by using weakref, but it is not always convenient.
    – mirod
    Feb 20, 2011 at 7:50
1

It's simply an oversight in the design. It's too late to change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.