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

I use Emacs as my primary editing tool. Yesterday I switched from perl-mode to cperl-mode. It's got lots of fancy little bells & whistles. One of the ones I liked was the ability to mark a function and then indent it properly/consistently. I've noticed that it has a VERY bad habit with variable names that contain underscores:
foreach my $rule_name ( keys %$hash_ref){
gets converted to:
foreach my $rule _name ( keys %$hash_ref){
Notice that it adds a space between "$rule" and "_name" Interestingly, this only seems to occur in "unenclosed" expressions. In other words, this behavior is only exhibited in the foreach statement, not the the enclosed loop. Have any other monks encountered this? Any suggestions for modifying my .emacs to supress this "feature"?

Replies are listed 'Best First'.
Re: EMACS cperl mode misbehaving
by jmcnamara (Monsignor) on May 06, 2005 at 14:56 UTC

    Weird.

    Happens for me as well with Emacs 21.3.1 (but not with 20.4.1). But it doesn't seem to have anything to do with the .emacs file (I temporarily moved it and got the same results).

    I guess that it is either a bug in cperl-mode or in Emacs itself.

    --
    John.

Re: EMACS cperl mode misbehaving
by heathen (Acolyte) on May 06, 2005 at 15:06 UTC
    To get Emacs to exhibit this behavior, select PERL / "Mark Function". Then PERL / "Indent Region". I spent a little time in Google and found this little gem:
    +;;; (`cperl-fix-line-spacing'): Allow "_" in $vars of foreach etc. w +hen +;;; underscore isn't a word char (gdj-contributed).
    So I added this:
    ;; Allow "_" in $vars of foreach '(cperl-fix-line-spacing)
    to my .emacs file, but apparently its not doing what I expected because variable names in a for loop with an underscore still get "split". Feels like I'm on the right track, but sometimes EMACS behavior is a mystery to me.
      I'm using an old version of emacs (20.4) and it has a variable called cperl-under-as-char which I can Customize. It's set to 't' for me (by default I think, since I don't remember ever changing this) and everything seems to be working. Type "M-x apropos cperl" and look around some. FWIW the docs for cperl-fix-line-spacing don't mention anything about fixing spacing for underscore, but they are pretty sparce.
Re: EMACS cperl mode misbehaving
by tlm (Prior) on May 06, 2005 at 14:56 UTC

    I've used cperl mode for years and I've never seen this behavior. What do you do immediately preceding this conversion?

    the lowliest monk

Re: EMACS cperl mode misbehaving
by Fletch (Bishop) on May 06, 2005 at 14:53 UTC

    Hrmm, I can't get this to reproduce (XEmacs 21.4p15, cperl-mode 4.35). Check that you're up-to-date version wise.

      I'm using emacs 21.1.1, bu have no idea what version of cperl-mode shipped with that build. How do I check the version of cperl-mode?

        Make sure cperl-mode's been loaded (i.e. edit a perl file) and do C-h v cperl-version RET.

        Check the value of the cperl-version variable; one way to do this is with C-h v (aka describe-variable). I have version 5.0, which seems to have fixed the problem you report, but I was able to reproduce it with an old version of cperl (4.23).

        the lowliest monk