【问题标题】:What is Perl's equivalent to Ruby's PryPerl 相当于 Ruby 的 Pry
【发布时间】:2014-08-29 20:17:12
【问题描述】:

我想知道 Perl 相当于 Ruby 的 pry 是什么?

假设我有一段 ruby​​ 代码

CSV.foreach('teamss.csv', headers: true) do |row|
  binding.pry
  team_array << row.to_hash
end

我可以插入该 binding.pry,然后转到命令行并进行撬动。从这里我可以输入 team_array 并通过循环获取该迭代中当前散列中的内容的输出。然后我可以按下一步并通过循环查看下一次迭代。这样我就可以在终端中逐行跟踪代码中发生的事情。

如果我想在 Perl 中做这样的事情,有什么可用的吗?这让我可以看到每次迭代时循环中究竟发生了什么,或者至少让我看到完成的循环实际上吐出什么。这样我就可以看到大型代码库中的哪些地方可能出现故障。

如果我想把这样的东西放在这个 perl 代码块中。我在问一个例子,它会是什么样子。在下面的代码块或您自己选择的代码块中。或者一个简单的是的,你可以使用它,或者没有这样的东西。

foreach my $secert (@secert) {

    my @example = $self->getMissingSecerts({secert=>$secert});
    next if !@example;

    $self->send_page({page=>"checksecerts/secert", param=>{secert=>$secert,      example=>\@example}});
}

【问题讨论】:

  • 你能用英语而不是 Ruby 准确解释你想要做什么吗?
  • 基本上是在寻找一种方法来逐行跟踪给定数据结构中的内容。
  • 受这个问题的启发,我刚刚在 CPAN 上发布了Pry。它与同名的 Ruby 工具绝不相同,但应该可用于类似目的。
  • 非常感谢。我刚刚注意到这一点,再高兴不过了。

标签: ruby perl pry


【解决方案1】:

有这个 CPAN 模块可用。

https://metacpan.org/pod/Pry

【讨论】:

  • 哈,刚刚发现 tobyink 已经提到他为这个问题创建了它。好老的 Perl 周刊。
【解决方案2】:

Perl 没有直接等效于 Pry。对于您的特定示例,我将使用调试器。您无需在源代码中添加任何内容。只需使用 -d 选项调用 perl:

perl -d script.pl

从那里开始,就是使用调试器命令的问题了。比较常用的有:

  • b设置断点
  • c继续
  • s单步(步入)
  • n 下一步(跨步)
  • r一步返回
  • x检查变量
  • q退出

详情请参阅perldebug

例如假设 my @example = ... 位于脚本的第 100 行,并且这是您每次要通过循环检查的变量:

C:\>perl -d script.pl
...
DB<1> b 101
...
DB<2> c
...
DB<3> x \@example

【讨论】:

  • 为了使它更直接的等效,您可以将$DB::single = 1; 放在您希望它放入调试器外壳的代码中。然后在调试器下运行即可。
【解决方案3】:

1) 你可以只使用 print() 语句:

use strict;
use warnings;
use 5.016;
use Data::Dumper;

my @data = (
    {a => 1, b => 2},
    {c => 3, d => 4},
    {a => 5, b => 6},
);

my %team_hash;

for my $href (@data) {
    %team_hash = %$href;

    print "$_ $team_hash{$_}\n" for (keys %team_hash);
    say '-' x 10;
}

--output:--
a 1
b 2
----------
c 3
d 4
----------
a 5
b 6
----------

2) 或者,您可以使用 Data::Dumper:

use strict;
use warnings;
use 5.016;
use Data::Dumper;

my @data = (
    {a => 1, b => 2},
    {c => 3, d => 4},
    {a => 5, b => 6},
);

my %team_hash;

for my $href (@data) {
    say Dumper(\$href);
    %team_hash = %$href;
}

--output:--
$VAR1 = \{
            'a' => 1,
            'b' => 2
          };

$VAR1 = \{
            'c' => 3,
            'd' => 4
          };

$VAR1 = \{
            'a' => 5,
            'b' => 6
          };

3) 或者,您可以使用 perl 的调试器(参见 perl debugger tutorial):

~/perl_programs$ perl -d my_prog.pl 

Loading DB routines from perl5db.pl version 1.37
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

main::(1.pl:6): my @data = (
main::(1.pl:7):     {a => 1, b => 2},
main::(1.pl:8):     {c => 3, d => 4},
main::(1.pl:9):     {a => 5, b => 6},
main::(1.pl:10):    );
  DB<1> v   #Show the `view` where the debugger stopped in the code
3:  use 5.016;
4:  use Data::Dumper;
5   
6==>    my @data = (
7       {a => 1, b => 2},
8       {c => 3, d => 4},
9       {a => 5, b => 6},
10  );
11  
12: my %team_hash;
  DB<1> v   #Show more of the view
10  );
11  
12: my %team_hash;
13  
14: for my $href (@data) {
15:     %team_hash = %$href;
16  }
17  
18  
  DB<1> s    #Step to the next line
main::(1.pl:12):    my %team_hash;
  DB<1> s
main::(1.pl:14):    for my $href (@data) {
  DB<1> s
main::(1.pl:15):        %team_hash = %$href;
  DB<1> p %team_hash   #No output because the debugger halted at the beginning of the line

  DB<2> s
main::(1.pl:15):        %team_hash = %$href;
  DB<2> p %team_hash   #Print the variable
a1b2
  DB<3> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 2011-03-25
    • 2011-04-01
    • 2018-11-16
    • 2011-02-17
    相关资源
    最近更新 更多