原文:
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";
}
……
当然,这样的代码对于我们开发时非常有用,但是在实际的脚本中可能必须删掉或注释掉相应的代码了。