Proper perl switch formatting in emacs

Change: . After reading the answers, I think the answer is “don't do this,” so I marked the corresponding answer as an official answer.

Is there an easy way to get emacs to display perl switch instructions like perldoc.perl.org go to page ?

Here's the formatting on perldoc.perl.org:

use Switch;

switch ($val) {
    case 1          { print "number 1" }
    case "a"        { print "string a" }
    case [1..10,42] { print "number in list" }
    case (\@array)  { print "number in list" }
    case /\w+/      { print "pattern" }
    case qr/\w+/    { print "pattern" }
    case (\%hash)   { print "entry in hash" }
    case (\&sub)    { print "arg to subroutine" }
    else            { print "previous case not true" }
}

Here in the fragment formatting is performed cperl-modeafter M-x indent-region:

use Switch;

switch ($val) {
  case 1                { print "number 1" }
    case "a"    { print "string a" }
      case [1..10,42]   { print "number in list" }
        case (\@array)  { print "number in list" }
  case /\w+/    { print "pattern" }
        case qr/\w+/    { print "pattern" }
        case (\%hash)   { print "entry in hash" }
        case (\&sub)    { print "arg to subroutine" }
        else            { print "previous case not true" }
    }

I have an inexplicable desire to stick with if-elsif constructs ...

ps. I think this describes the desired process, but it seems like it will take some time for parsing.

+3
3

, emacs.

if ( condition ) {

}
elsif( other_condition ) {

}
else {

}

Switch.pm. . perl5.porters:

Perl.

PerlMonks.

, 5.10. Perl , :

use feature "switch";

given($_) {
   when (/^abc/) { $abc = 1; }
   when (/^def/) { $def = 1; }
   when (/^xyz/) { $xyz = 1; }
   default { $nothing = 1; }
}
+4
0

cperl , perltidy.

Here's a neat feature to run perltidy in Emacs in the current area:

;; Slick functions to run perltidy in place
(defun perltidy-region ()
  "Run perltidy on the current region."
  (interactive)
  (save-excursion
    (shell-command-on-region (point) (mark) "perltidy -q" nil t)))
0
source

Source: https://habr.com/ru/post/1721346/


All Articles