2

I was wondering what Perl's equivalent to Ruby's pry is?

Lets say I have a block of ruby code

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

I can insert that binding.pry to then go to the commandlind and go to pry. From here I can type out team_array and get an output of whats currently in the hash at that iteration through the loop. I then can press next and see the next iteration through the loop. This way I can follow what's going on in the code line by line right in the terminal.

If I wanted to do something like this in Perl is there something available? That lets me see what exactly is happening in a loop at each iteration or something that at least lets me see what the finished loop is actually spitting out. That way I can see where in a large code base something might be breaking down.

If I wanted to put such a thing in this block of perl code. I was asking for an example of what it would look like. Within the block of code below or one of your own choosing. Or a simple yeah you can use this, or no there is no such thing.

foreach my $secert (@secert) {

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

    $self->send_page({page=>"checksecerts/secert", param=>{secert=>$secert,      example=>\@example}});
}
4
  • 2
    Can you explain exactly what you're trying to do in English instead of in Ruby? Aug 29, 2014 at 20:22
  • Basically looking for a way to track whats in a given data structure, line by line. Aug 29, 2014 at 20:31
  • 5
    Inspired by this question, I've just released Pry on CPAN. It's not by any means identical to the Ruby tool of the same name, but should be useful for similar purposes.
    – tobyink
    Aug 31, 2014 at 12:16
  • Thank You a bunch. I just noticed this and could not be more happier. Sep 2, 2014 at 15:17

3 Answers 3

3

There is this CPAN module available.

https://metacpan.org/pod/Pry

1
  • Ha, just spotted tobyink already mentioned he created it for this question. Good old Perl Weekly.
    – Hugh
    Sep 1, 2014 at 8:15
3

Perl doesn't have a direct equivalent to Pry. For your particular example I'd use the debugger. You don't need to add anything to your source code. Just invoke perl with the -d option:

perl -d script.pl

From there, it's a matter of using debugger commands. Some of the more commonly used ones are:

  • b set a breakpoint
  • c continue
  • s single step (step into)
  • n next (step over)
  • r step return
  • x examine variable
  • q quit

See perldebug for full details.

e.g. assuming that my @example = ... is on line 100 of your script and that's the variable you want to check each time through the loop:

C:\>perl -d script.pl
...
DB<1> b 101
...
DB<2> c
...
DB<3> x \@example
1
  • 2
    To make this a more direct equivalent, you can put $DB::single = 1; in your code where you want it to drop into the debugger shell. Then just run it under the debugger. Aug 31, 2014 at 15:07
1

1) You could just use a print() statement:

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) Or, you could use 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) Or, you could use perl's debugger (see 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> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.