I am using cperl-mode to edit Perl scripts. According to the Emacs wiki ( link ) one could use defalias to use cperl-mode instead of perl-mode. Here is an example:

$ emacs -Q -l init.el test.pl &

where init.el is:

(defalias 'perl-mode 'cperl-mode)
(defun my-test-hook ()
  (message-box (symbol-name major-mode)))
(add-hook 'cperl-mode-hook #'my-test-hook)

This gives two message boxes, indicating that the hook my-test-hook is being run twice at startup.. (I also tested this with emacs-lisp-mode (without using defalias of course) and then the hook was run only a single time, as expected.. So that was what lead me to suspect defalias was the culprit).

Why is the hook being run twice here? (I am asking because I would like to have it run only a single time if possible)

  • I really doubt the defalias plays any role here. cperl-mode-hook variable is just a variable (possibly autoloaded), adding an alias to a function cperl-mode shouldn't affect it at all. I'd try to debug the cperl-mode function instead--maybe it runs the hooks twice in some cases?
    – wvxvw
    Mar 19, 2015 at 14:22

1 Answer 1


It looks like a bug in cperl-mode. It is defined using define-derived-mode, a special form that defines a mode. When using it, it generates code to call the mode hook. However, the body of also contain an explicit call to the hooks, so it will be called twice.

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.