Perl/TkFAQ-12.5有什么办法可以了解Perl/Tk的路径中有哪些模块吗?

王朝perl·作者佚名  2008-05-18
窄屏简体版  字體: |||超大  

原文:

12.5. Is there a way to find out what is in my perl/Tk "PATH"?

Presuming this question is asking for a little more than the answer you get when you type: ls perl5/lib/Tk/*.pm

there are ways to find out what gets EXPORTED by Tk.pm. Use a script like: #!/usr/bin/perl use Tk; require 'dumpvar.pl'; dumpvar('Tk');

or more succintly at the shell command prompt: perl -e 'use Tk; require "dumpvar.pl"; dumpvar("Tk");'

The advantage of using dumpvar over ls is that it gives you a brief summary of all the arguments your widgets want. Note that the output is many lines and you may wish to pipe through more or less.

If you wish to determine the Configuration options a given widget accepts (and what the values are at a given point in a script) you may use the ->configure method with no arguments to retrieve the list of lists, as in this example: #!/usr/bin/perl use Tk; my $main = MainWindow -> new; my $scrl = $main -> Scrollbar('-orient' => 'horizontal'); @scrollconfig = $scrl -> configure; for (@scrollconfig) { print "@$_\n"; } etc.

Such code is useful for development but is probably best left out, commented out, or switched out of "production line" code.

译文:

12.5 有什么办法可以了解Perl/Tk的路径中有哪些模块吗?

其实答案很简单,只要在命令行里输入:

ls perl5/lib/Tk/*.pm

但是,我们不妨考虑的更深入一些,可以用下面的简单脚本来了解Tk.pm输出(Export)了哪些:

#!/usr/bin/perl

use Tk;

require 'dumpvar.pl';

dumpvar('Tk');

或者可以更简便的,就在命令行中使用如下命令:

perl -e 'use Tk; require "dumpvar.pl"; dumpvar("Tk");'

(译者注:在Windows系统中需要这样使用:

perl -e "use Tk; require 'dumpvar.pl';dumpvar('Tk');")

使用dumpvar比ls的好处就在于,它可以简单的给出你的组件可能涉及的所有参数的值。请注意,它的输出有很多行,所以你可能需要使用more或者less来进行分屏显示。

如果你想知道一个给定的组件的所有配置选项(以及在脚本中的某处的其所对应的值),那么你可以不带任何参数的使用->configure方法,从而可以获得一个选项和当前值的列表。如下例:

#!/usr/bin/perl

use Tk;

my $main = MainWindow -> new;

my $scrl = $main -> Scrollbar('-orient' => 'horizontal');

@scrollconfig = $scrl -> configure;

for (@scrollconfig) {

print "@$_\n";

}

……

当然,这样的代码对于我们开发时非常有用,但是在实际的脚本中可能必须删掉或注释掉相应的代码了。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航