loris has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

Having just shot myself in the foot (again) by doing

sub shootFoot { my $inTheFootShoot = shift; ... }

in a object-oriented context instead of

sub shootFoot { my ($self,$inTheFootShoot) = @_; ... }

and spent a long time trying to figure out what was wrong, I was wondering if anyone knows of any templating stuff for Emacs, whereby one could insert a method template simply by pressing Ctl/Esc and half a dozen other keys simultaneously. That way I would hope to reduce the chance of such mistakes in the future and thus spare my feet.

BTW, you may suggest wonderful things I could do with other editors, but my religious blindness will almost certainly cause me to ignore them.

Thanks,

loris

Replies are listed 'Best First'.
Re: Perl templates for Emacs?
by Fletch (Bishop) on Jun 30, 2005 at 12:26 UTC

      Thanks! I had seen the EmacsWiki page on general templates, but missed the first one you mention.

Re: Perl templates for Emacs?
by tlm (Prior) on Jun 30, 2005 at 12:51 UTC

    It seems to me that an Emacs shortcut to avoid the mistake you describe would be significantly complicated by the fact that the LHS of the assignment is quite variable, but maybe this will be enough:

    (add-hook 'cperl-mode-hook (lambda() (local-set-key "\C-cm" 'perl-insert-oo-me +thod))) (defun perl-insert-oo-method (methodname) "Insert Perl OO method" (interactive "MEnter method's name: ") (insert (format "sub %s {\nmy ( $self ) = @_;" methodname)) (backward-char 8) (cperl-indent-command) )
    Tweak to taste. It assumes that you are using cperl-mode (and if you aren't I highly recommend it). Also, you can probably modify this example to handle other common coding shortcuts.

    Update:For those less familiar with extending Emacs:

    1. Cut and paste the code above to your .emacs file or equivalent (I believe that for xemacs that would be the file .xemacs/custom.el, but I'm not sufficiently familiar with xemacs to know for sure).
    2. Make this inserted text the region, and execute the command eval-region (i.e. M-x eval-region).
    3. Switch to a new buffer and execute he command cperl-mode (i.e. M-x cperl-mode).
    4. Now, in cperl mode, the key sequence C-c m will bring up a prompt for the method name:
      Enter method name: foo
      Once the method name is entered (say foo), the following will be inserted at point:
      sub foo { my ( $self ) = @_;
      and point will be placed right after $self.

    the lowliest monk

      That is also nice, although I like the multiple arguments bit of the version given in PerlTemplates.

      However, I'd quite like to have pod inserted there too, so time to learn some Lisp, I suppose ...

        It's out of print, but if you can find a copy Writing GNU Emacs Extensions (ISBN 1565922611) is a pretty gentle (if somewhat dated) introduction to elisp. You can at least get the example code from ORA at the link above.

        --
        We're looking for people in ATL

        loris writes:

        time to learn some Lisp, I suppose ...

        Bob Chassell's An Introduction to Programming in Emacs Lisp is a good way to get started, if your primary objective is to extend Emacs, because Elisp has a lot of functions and concepts that are specific to text editing and would not be covered at all in a general Lisp book or tutorial. You can find the book online (though personally I like to support the FSF by buying hardcopies of their books).

        One alternative is Writing GNU Emacs Extensions by Bob Glickstein, but this book is out of print, so you'll need to find a used copy.

        the lowliest monk

Re: Perl templates for Emacs?
by anonymized user 468275 (Curate) on Jun 30, 2005 at 12:32 UTC
    Latest update: varying emacs versions seem to have varying macro support for this, it appears to be necessary to code the macro in a .el file (emacs lisp); .

    Original post: (no longer relevant) I don't know the symptoms you had, but what screams at me is that the bare array needs instead to be expressed in list mode by encasing it in round brackets, so that it forms a valid RHS for the list assignment.e.g.:

    sub FeetIntact{ my ($self,$shootSomeoneElse) = (@_); # ... }

    Update: Oops overkill there - this is only necessary for reading parameters in hash form, e.g.:-

    FeetIntact( toe => big, smell => roquefort ); sub FeetIntact{ my %par = (@_); # $par { smell } now has value 'roquefort' }

    One world, one people

      the bare array needs instead to be expressed in list mode by encasing it in round brackets
      Why should it??
      use strict; use warnings; foo ("Darth", "Vader"); sub foo { my ($bar,$baz) = @_; print "$bar $baz"; }
      prints "Darth Vader", as expected.


      holli, /regexed monk/
      First, you have syntax error in that code (the opening curly in the sub call). Second, braces are unneccessary too.

      Do you test your code before posting?
      FeetIntact(toe => big, smell => roquefort ); sub FeetIntact { my %par = @_; print $par{smell}; #roquefort }


      holli, /regexed monk/
        hmmm looks like you're right.

        One world, one people

      Well, actually I always write

      my ($self,$inTheFootShoot) = @_;

      (apart from when I am shooting myself in the foot). It was the missing

      $self

      that was causing me problems. Are the brackets really necessary?

      loris