编辑:阅读回复后,我认为答案是“不要这样做”,因此我将适当的回复标记为官方回复。
有没有简单的方法让 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/