[PerlChina] PerlChina Advent 15: Devel::DidYouMean

2 views
Skip to first unread message

chenlin rao

unread,
Dec 15, 2014, 3:12:58 AM12/15/14
to perl...@googlegroups.com
# Devel::DidYouMean

我们总是期望程序越智能越好。在 Perl6 里,有一系列的“友好”的提示信息,帮助从 Perl5 程序员转移自己的语法习惯。其中一个功能,叫 **Did You Mean?**。如下:

    $ perl6 -e 'sya "ok"'
    ===SORRY!=== Error while compiling -e
    Undeclared routine:
        sya used at line 1. Did you mean 'say'?

现在,我们在 Perl5 里也可以做到同样的事情。这就是 [Devel::DidYouMean](https://metacpan.org/pod/Devel::DidYouMean) 模块。示例如下:

    $ perl -MDevel::DidYouMean -e 'printX("test")'
    Undefined subroutine &main::printX called at -e line 1.
    Did you mean print, printf?

当然,除了系统内置指令,对程序自己定义的函数,也可以做到相应的处理。比如下面这样:

    use 5.010;
    use Devel::DidYouMean;
    use Try::Tiny;

    sub testsay {
        say @_;
    }
    try {
        testsayx("catch me"); # boom
    } catch {
        my $error_msg = $_;
        $Devel::DidYouMean::DYM_MATCHING_SUBS->[0]->("I'm here");
    }

没错,最终我们成功得到了 "I'm here" 的输出。

Haiyan Lin

unread,
Dec 15, 2014, 4:19:31 AM12/15/14
to perl...@googlegroups.com
cool
--
您收到此邮件是因为您订阅了Google网上论坛上的“PerlChina Mongers 讨论组”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到perlchina+...@googlegroups.com
要发帖到此群组,请发送电子邮件至perl...@googlegroups.com
访问此群组:http://groups.google.com/group/perlchina
要查看更多选项,请访问https://groups.google.com/d/optout

Reply all
Reply to author
Forward
0 new messages