什么是Perl';s相当于Ruby';撬

什么是Perl';s相当于Ruby';撬,ruby,perl,pry,Ruby,Perl,Pry,我想知道Perl与Ruby的pry的等价物是什么 假设我有一块ruby代码 CSV.foreach('teamss.csv', headers: true) do |row| binding.pry team_array << row.to_hash end 1) 您可以使用print()语句: use strict; use warnings; use 5.016; use Data::Dumper; my @data = ( {a => 1, b =&g

我想知道Perl与Ruby的pry的等价物是什么

假设我有一块ruby代码

CSV.foreach('teamss.csv', headers: true) do |row|
  binding.pry
  team_array << row.to_hash
end
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\u程序$perl-d my\u prog.pl
从perl5db.pl版本1.37加载数据库例程
编辑器支持可用。
输入h或“h”以获取帮助,或输入“man perldebug”以获取更多帮助。
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},
主要::(1.pl:10):);
DB v#显示调试程序在代码中停止的“视图”
3:使用5.016;
4:使用数据::转储程序;
5.
6==>my@data=(
7{a=>1,b=>2},
8{c=>3,d=>4},
9{a=>5,b=>6},
10  );
11
12:我的%team\u哈希;
DB v#显示更多视图
10  );
11
12:我的%team\u哈希;
13
14:对于我的$href(@data){
15:%team_hash=%$href;
16  }
17
18
DB s#转到下一行
main:(1.pl:12):我的%team\u散列;
DB s
main:(1.pl:14):对于我的$href(@data){
DB s
main::(1.pl:15):%team_hash=%$href;
DB p%team_hash#没有输出,因为调试器在行首停止
DB s
main::(1.pl:15):%team_hash=%$href;
DB p%team_hash#打印变量
a1b2
分贝

Perl与Pry没有直接的等价物。对于您的特定示例,我将使用调试器。您不需要向源代码中添加任何内容。只需使用
-d
选项调用Perl即可:

perl -d script.pl
从这里开始,就需要使用调试器命令。一些更常用的命令包括:

  • b
    设置断点
  • c
    继续
  • s
    单步(单步)
  • n
    下一步(跳过)
  • r
    步进返回
  • x
    检查变量
  • q
    退出
有关详细信息,请参阅

e、 g.假设
my@example=…
位于脚本的第100行,并且这是您希望每次通过循环检查的变量:

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

有此CPAN模块可用


你能用英语而不是Ruby准确地解释一下你想做什么吗?基本上是在寻找一种方法来逐行跟踪给定数据结构中的内容。受这个问题的启发,我刚刚发布了CPAN。它与同名Ruby工具完全不同,但对于类似的目的应该很有用。谢谢你ch.我刚刚注意到了这一点,非常高兴。为了使这一点更直接,您可以将
$DB::single=1;
放入您的代码中,您希望它放入调试器外壳中。然后在调试器下运行它。哈,刚刚发现Tobynk已经提到他为这个问题创建了它。很好的老Perl周刊。
perl -d script.pl
C:\>perl -d script.pl
...
DB<1> b 101
...
DB<2> c
...
DB<3> x \@example