mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-23 21:20:42 +08:00
translated
This commit is contained in:
parent
b502222c3f
commit
d38958e230
@ -1,82 +0,0 @@
|
||||
translating---geekpi
|
||||
|
||||
A Perl module for better debugging
|
||||
======
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/annoyingbugs.png?itok=ywFZ99Gs)
|
||||
It's occasionally useful to have a block of Perl code that you use only for debugging or development tweaking. That's fine, but having blocks like this can be expensive to performance, particularly if the decision whether to execute it is made at runtime.
|
||||
|
||||
[Curtis "Ovid" Poe][1] recently wrote a module that can help with this problem: [Keyword::DEVELOPMENT][2]. The module utilizes Keyword::Simple and the pluggable keyword architecture introduced in Perl 5.012 to create a new keyword: DEVELOPMENT. It uses the value of the PERL_KEYWORD_DEVELOPMENT environment variable to determine whether or not a block of code is to be executed.
|
||||
|
||||
Using it couldn't be easier:
|
||||
```
|
||||
use Keyword::DEVELOPMENT;
|
||||
|
||||
|
||||
|
||||
sub doing_my_big_loop {
|
||||
|
||||
my $self = shift;
|
||||
|
||||
DEVELOPMENT {
|
||||
|
||||
# insert expensive debugging code here!
|
||||
|
||||
}
|
||||
|
||||
}Keyworddoing_my_big_loopDEVELOPMENT
|
||||
|
||||
```
|
||||
|
||||
At compile time, the code inside the DEVELOPMENT block is optimized away and simply doesn't exist.
|
||||
|
||||
Do you see the advantage here? Set up the PERL_KEYWORD_DEVELOPMENT environment variable to be true on your sandbox and false on your production environment, and valuable debugging tools can be committed to your code repo, always there when you need them.
|
||||
|
||||
You could also use this module, in the absence of a more evolved configuration management system, to handle variations in settings between production and development or test environments:
|
||||
```
|
||||
sub connect_to_my_database {
|
||||
|
||||
|
||||
|
||||
my $dsn = "dbi:mysql:productiondb";
|
||||
|
||||
my $user = "db_user";
|
||||
|
||||
my $pass = "db_pass";
|
||||
|
||||
|
||||
|
||||
DEVELOPMENT {
|
||||
|
||||
# Override some of that config information
|
||||
|
||||
$dsn = "dbi:mysql:developmentdb";
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
my $db_handle = DBI->connect($dsn, $user, $pass);
|
||||
|
||||
}connect_to_my_databaseDEVELOPMENTDBI
|
||||
|
||||
```
|
||||
|
||||
Later enhancement to this snippet would have you reading in configuration information from somewhere else, perhaps from a YAML or INI file, but I hope you see the utility here.
|
||||
|
||||
I looked at the source code for Keyword::DEVELOPMENT and spent about a half hour wondering, "Gosh, why didn't I think of that?" Once Keyword::Simple is installed, the module that Curtis has given us is surprisingly simple. It's an elegant solution to something I've needed in my own coding practice for a long time.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/18/4/perl-module-debugging-code
|
||||
|
||||
作者:[Ruth Holloway][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://opensource.com/users/druthb
|
||||
[1]:https://metacpan.org/author/OVID
|
||||
[2]:https://metacpan.org/pod/release/OVID/Keyword-DEVELOPMENT-0.04/lib/Keyword/DEVELOPMENT.pm
|
@ -0,0 +1,80 @@
|
||||
一个更好的调试 Perl 模块
|
||||
======
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/annoyingbugs.png?itok=ywFZ99Gs)
|
||||
只有在调试或开发调整时才使用 Perl 代码块有时会很有用。这很好,但是这样的代码块可能会对性能产生很大的影响, 尤其是在运行时决定是否执行它。
|
||||
|
||||
[Curtis“Ovid”Poe][1] 编写了一个可以帮助解决这个问题的模块:[Keyword::DEVELOPMENT][2]。该模块利用 Keyword::Simple 和 Perl 5.012 中引入的可插入关键字架构来创建新的关键字:DEVELOPMENT。它使用 PERL_KEYWORD_DEVELOPMENT 环境变量的值来确定是否要执行一段代码。
|
||||
|
||||
使用它并不容易:
|
||||
```
|
||||
use Keyword::DEVELOPMENT;
|
||||
|
||||
|
||||
|
||||
sub doing_my_big_loop {
|
||||
|
||||
my $self = shift;
|
||||
|
||||
DEVELOPMENT {
|
||||
|
||||
# insert expensive debugging code here!
|
||||
|
||||
}
|
||||
|
||||
}Keyworddoing_my_big_loopDEVELOPMENT
|
||||
|
||||
```
|
||||
|
||||
在编译时,DEVELOPMENT 块内的代码已经被优化掉了,根本就不存在。
|
||||
|
||||
你看到好处了么?在沙盒中将 PERL_KEYWORD_DEVELOPMENT 环境变量设置为 true,在生产环境设为 false,并且可以将有价值的调试工具提交到你的代码库中,在你需要的时候随时可用。
|
||||
|
||||
在缺乏高级配置管理的系统中,你也可以使用此模块来处理生产和开发或测试环境之间的设置差异:
|
||||
```
|
||||
sub connect_to_my_database {
|
||||
|
||||
|
||||
|
||||
my $dsn = "dbi:mysql:productiondb";
|
||||
|
||||
my $user = "db_user";
|
||||
|
||||
my $pass = "db_pass";
|
||||
|
||||
|
||||
|
||||
DEVELOPMENT {
|
||||
|
||||
# Override some of that config information
|
||||
|
||||
$dsn = "dbi:mysql:developmentdb";
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
my $db_handle = DBI->connect($dsn, $user, $pass);
|
||||
|
||||
}connect_to_my_databaseDEVELOPMENTDBI
|
||||
|
||||
```
|
||||
|
||||
稍后对此代码片段的增强使你能在其他地方,比如 YAML 或 INI 中读取配置信息,但我希望您能在此看到该工具。
|
||||
|
||||
我查看了关键字 Keyword::DEVELOPMENT 的源码,花了大约半小时研究,“天哪,我为什么没有想到这个?”安装 Keyword::Simple 后,Curtis 给我们的模块就非常简单了。这是我长期以来在自己的编码实践中需要的一个优雅解决方案。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/18/4/perl-module-debugging-code
|
||||
|
||||
作者:[Ruth Holloway][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://opensource.com/users/druthb
|
||||
[1]:https://metacpan.org/author/OVID
|
||||
[2]:https://metacpan.org/pod/release/OVID/Keyword-DEVELOPMENT-0.04/lib/Keyword/DEVELOPMENT.pm
|
Loading…
Reference in New Issue
Block a user