"Boilerplate" for new Perl files in Emacs

This Emacs lisp function automatically adds "boilerplate" code to new Perl files.
(defun perl-boilerplate ()
  (if (not (file-exists-p (buffer-file-name (current-buffer))))
      (cond 
       ((string-match "\.pl$" buffer-file-name)
        (insert
         "#!/usr/local/bin/perl\nuse warnings;\nuse strict;\n"))
       ((string-match "\\([^/]*\\)\.pm$" buffer-file-name)
        (insert
         (concat "package " (match-string 1 buffer-file-name)
                 ";\n\nuse warnings;\nuse strict;\n\n1;\n"))
        (backward-char 4)))))

(add-hook 'cperl-mode-hook 'perl-boilerplate)
On opening a new file called "boilerplate.pl", the following is automatically added at the top:
#!/usr/local/bin/perl
use warnings;
use strict;
On opening a new file called "boilerplate.pm", the following is automatically added at the top, and the cursor is repositioned to before the 1;.
package boilerplate;

use warnings;
use strict;

1;
The complicated string-match expression extracts the name of the module from the file name.

Extensions to this might include some exporter code for the .pm version:

@ISA = qw(Exporter);
@EXPORT_OK = qw//;
by changing the concat above to
         (concat "package " (match-string 1 buffer-file-name)
                 ";\n\n@ISA = qw(Exporter);\n@EXPORT_OK = qw//;\n"
                 "use warnings;\nuse strict;\n\n1;\n"))
if you write a lot of modules which export functions.
Copyright © Ben Bullock 2009-2017. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com). / Privacy / Disclaimer