Where am I now — Really?

B::DeparseTree for Better Location Reporting

(enter "s" to see presenter text for slides)

A Perl Error


Use of uninitialized value $prev[0] in array element at YAPC2018/prev.pl line 5.
		

where line 5 is:


sub boom { $prev[$prev[$_[1]]]; }
		

Which prev were we talking about?

More errors where line numbers are not good enough

Use of uninitialized value $_ in numeric gt (>) at YAPC2018/map.pl line 4
    

where line 5 is:


map { $_ > 1 ? 1 : $_ } @_ if $_;
    

and finally...


Useless use of private variable in void context at YAPC2018/eval.pl line 8
		

where line 4 is...


eval { $code };
    

Laziness, Impatience, Hubris

We will encourage you to develop the three great virtues of a programmer: laziness, impatience, and hubris.

Laziness: The quality that makes you go to great effort to reduce overall energy expenditure.

Impatience: The anger you feel when the computer is being lazy.

Programmer to Computer

A programmer may write ...


$a / $b / $c
		

The Perl 5 interpreter sees (according to B::Concise)...


8  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 1 div.pl:1) v:{ ->3
7     <2> divide[t5] vK/2 ->8
5        <2> divide[t3] sK/2 ->6
3              <#> gvsv[*a] s ->4
4              <#> gvsv[*b] s ->5
6           <#> gvsv[*c] s ->7
		

Gaelic to English

Gaelic: Tha mi ag ithe an taigeis

English: I am eating the haggis

Gaelic: Tha (= am/are/is) mi (= I/me) ag ithe an taigeis

English: I am eating the haggis

Side-by-side Scots-English Translation

1: Fair fa' your honest, sonsie face,
2: Great chieftain o the puddin'-race!

1: Good luck to you and your honest, plump face,
2: Great chieftain of the sausage race!

Side-by-side Perl translation


  $x = 1;  # line 1
  $y = 2;  # line 2
		

1  <0> enter
2  <;> nextstate(main 1 assign.pl:1) v:{
3  <$> const[IV 1] s
4  <#> gvsv[*x] sqnnpp
5  <2> sassign vKS/2

6  <;> nextstate(main 1 assign.pl:2) v:{
7  <$> const[IV 2] s
8  <#> gvsv[*y] s
9  <2> sassign vKS/2
a  <@> leave[1 ref] vKP/REFC
    

Parse to Text

Perl fragments associated with B::OPs

divide rule


sub divide {
    my ($self, $op, $context) = @_;
    my $left = $self->deparse($op->first);
    my $right = $self->deparse($op->last);

    my $str =  $left . '/' . $right;
    return $self->maybe_parens($str, $context, 19)
}
      

gvsv rule


sub gvsv {
   my ($self, $op, $context) = @_;
   my $str = $self->gv_name($op);
   return $self->maybe_local($op, $str);
}
      

Simplifying the 100's of rule codes

Compare


my $str =  $left . '/' . $right;
				

with:


my $str =  sprintf("%s/%s", $left , $right);
			 

Simplifying rule code - templating


our $PP_RULES = {
    divide => ("%p/%p", [[0, 19], [1, 19]]),
    ...
}
      

Lingering Problems: Translation

Fair fa' your honest..

Good luck to you and your honest...

If we are are reading "to" where are we in the Scots?


#1: my @prev = (undef, 0, 1);
    COP (0x7fee65ad8728) dbstate
    BINOP (0x7fee65ad8788) aassign [2]
        UNOP (0x7fee60d08198) null [157]
            OP (0x7fee60d08160) PUSHMARK  // What's this? ...
            OP (0x7fee60d08220) undef
            SVOP (0x7fee60d081e0) const  IV (0x7fee61806c88) 0
            SVOP (0x7fee60d08120) const  IV (0x7fee61806c10) 1
        UNOP (0x7fee60d080c0) null [157]
            OP (0x7fee65ad87d0) PUSHMARK  // and this?
            OP (0x7fee60d08258) padav [1] my @prev = (undef, 0, 1);
			

pushmark, pushmark B::OP=SCALAR(0x7fee6823fe78)
	at address 0x7fee60d08160:
my(@prev) = (undef, 0, 1)
             |
				

Lingering Problem: PC location in a call


foreach( my $x =0 ; $x < 1; $x++) {   # Line 1
  print_line();                       # Line 2
}
sub print_line {
  print "call from line " . [caller(0)]->[2] . "\n";
}

prints "call from line 1".

From StackOverflow

Devel::Trepan Deparse with Fibonacci

Links...












- fin