perl - emacs 中正确的 perl 开关格式

标签 perl emacs indentation switch-statement

编辑:阅读回复后,我认为答案是“不要这样做”,因此我将适当的回复标记为官方回复。

有没有简单的方法让 emacs 显示 perl switch 语句,比如 perldoc.perl.org 的 switch page

这是 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" }
}

在代码段上运行 M-x indent-region 后,这是 cperl-mode 中的格式:

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" }
    }

我有一种莫名的冲动要坚持使用 if-elsif 结构...

附言。我想this描述了所需的过程,但看起来需要一段时间才能解析。

最佳答案

抱歉,我无法在 emacs 方面为您提供帮助。但是,我会建议您坚持使用

if ( condition ) {

}
elsif( other_condition ) {

}
else {

}

而不是使用可怕的 Switch.pm。参见 Nicholas Clark's message to perl5.porters :

Switch will be removed from the Perl core distribution in the next major release.

关于 PerlMonks 的更多讨论.

正如 Randal Schwartz 在下面的评论中指出的那样,从版本 5.10 开始,Perl 具有强大的 replacement不依赖于源过滤器:

use feature "switch";

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

关于perl - emacs 中正确的 perl 开关格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1640446/

相关文章:

javascript - 如何基于JavaScript整理HTML代码

multithreading - 如何在 Perl 中处理任何事件、RabbitMQ(心跳)和长时间运行的作业?

emacs - 您如何确保关联列表在 Emacs 中维护唯一键

PHPstorm 代码粘贴缩进

html - 将电影添加到组织模式 html 输出

emacs - 如何在 OSX 的 GNU Emacs 中 pbpaste UTF-8 字符?

string - 如何让 bash "eat"字符串中所有行共有的缩进字符?

perl - "inappropriate ioctl for device"

perl - Mojolicious url_for : absolute path

perl - 模板工具包,测试嵌套循环中的最后一次迭代