Devel-DumpTrace

 view release on metacpan or  search on metacpan

lib/Devel/DumpTrace/PPI.pm  view on Meta::CPAN

  }
  print $y;

  $ perl -d:DumpTrace::PPI simple-for.pl
  >>>>> simple-for.pl:1:[__top__]:  for ($i:0=0; $i:undef<3; $i:0++) {
  >>>>> simple-for.pl:2:[__top__]:  $y:0 += $i:0;
  >>>>> simple-for.pl:2:[__top__]:  FOR-UPDATE: {$i:2++ } FOR-COND: {$i:1<3; } 
                                    $y:1 += $i:1;
  >>>>> simple-for.pl:2:[__top__]:  FOR-UPDATE: {$i:3++ } FOR-COND: {$i:2<3; } 
                                    $y:3 += $i:2;
  >>>   simple-for.pl:4:[__top__]:  FOR-COND: {$i:3<3;} 
                                    print $y:3;

  $ perl -d:DumpTrace::PPI=verbose simple-for.pl
  >>    simple-for.pl:1:[__top__]:
  >>>              for ($i=0; $i<3; $i++) {
  >>>>             for ($i=0; undef<3; $i++) {
  >>>>>            for (0=0; undef<3; 0++) {
  -------------------------------------------
  >>    simple-for.pl:2:[__top__]:
  >>>              $y += $i;
  >>>>             $y += 0;
  >>>>>            0 += 0;
  -------------------------------------------
  >>    simple-for.pl:2:[__top__]:
  >>>              FOR-UPDATE: {$i++ } FOR-COND: {$i<3; }  $y += $i;
  >>>>             FOR-UPDATE: {$i++ } FOR-COND: {1<3; }  $y += 1;
  >>>>>            FOR-UPDATE: {2++ } FOR-COND: {1<3; }  1 += 1;
  -------------------------------------------
  >>    simple-for.pl:2:[__top__]:
  >>>              FOR-UPDATE: {$i++ } FOR-COND: {$i<3; }  $y += $i;
  >>>>             FOR-UPDATE: {$i++ } FOR-COND: {2<3; }  $y += 2;
  >>>>>            FOR-UPDATE: {3++ } FOR-COND: {2<3; }  3 += 2;
  -------------------------------------------
  >>    simple-for.pl:4:[__top__]:
  >>>              FOR-COND: {$i<3;}  print $y;
  >>>>             FOR-COND: {3<3;}  print 3;
  -------------------------------------------

The first time the loop's block code is executed, there is no need
to evaluate the conditional or the update expression, because
they were just evaluated in the previous line. But the second and third
time through the loop, the original source code is decorated with
C<FOR-UPDATE: {> I<expression> C<}> and C<FOR-COND: {> I<expression>
C<}>, showing what code was executed when the previous iteration 
finished, and what expression was evaluated to determine whether to
continue with the C<for> loop, respectively.

B<Note:> the final C<FOR-COND ...> statement, where the 
condition is false and Perl breaks out of the loop, will only be
displayed when the compound C<for> statement is not the last statement
in the current block, as this feature works by attaching
additional information to the statement that I<follows> the end
of the C<for> loop.

=head2 Special handling for other foreach loops

When a program containing the regular C<foreach [$var] LIST> construction
is traced, the C<foreach ...> statement only appears in the trace 
output for the first iteration of the loop, just like the C-style
for loop construct. For all subsequent iterations the 
C<Devel::DumpTrace::PPI> module will prepend the first statement in the
block with C<FOREACH: {> I<loop-variable> C<}> to show the new
value of the loop variable at the beginning of each iteration.

  $ perl -d:DumpTrace::PPI -e '
  for (1 .. 6) {
    $n += 2 * $_ - 1;
    print $_, "\t", $n, "\n"
  }
  '
  >>>>> -e:2:[__top__]:   for $_:1 (1 .. 6) {
  >>>>> -e:3:[__top__]:     $n:1 += 2 * $_:1 - 1;
  >>>   -e:4:[__top__]:     print $_:1, "\t", $n:1, "\n"
  1       1
  >>>>> -e:3:[__top__]:   FOREACH: {$_:2}         $n:4 += 2 * $_:2 - 1;
  >>>   -e:4:[__top__]:     print $_:2, "\t", $n:4, "\n"
  2       4
  >>>>> -e:3:[__top__]:   FOREACH: {$_:3}         $n:9 += 2 * $_:3 - 1;
  >>>   -e:4:[__top__]:     print $_:3, "\t", $n:9, "\n"
  3       9
  >>>>> -e:3:[__top__]:   FOREACH: {$_:4}         $n:16 += 2 * $_:4 - 1;
  >>>   -e:4:[__top__]:     print $_:4, "\t", $n:16, "\n"
  4       16
  >>>>> -e:3:[__top__]:   FOREACH: {$_:5}         $n:25 += 2 * $_:5 - 1;
  >>>   -e:4:[__top__]:     print $_:5, "\t", $n:25, "\n"
  5       25
  >>>>> -e:3:[__top__]:   FOREACH: {$_:6}         $n:36 += 2 * $_:6 - 1;
  >>>   -e:4:[__top__]:     print $_:6, "\t", $n:36, "\n"
  6       36

=head2 Special handling for while/until loops

As with a C<for> loop, the conditional expression of a 
C<while> or C<until> loop is only included in trace output
on the initial entrance to the loop. C<Devel::DumpTrace::PPI>
decorates the first statement of the block inside the C<while/until>
loop to show how the conditional expression is evaluated at the
beginning of every iteration of the loop:

  $ cat ./simple-while.pl
  my ($i, $j, $l) = (0, 9, 0);
  while ($i++ < 6) {
    my $k = $i * $j--;
    next if $k % 5 == 1;
    $l = $l + $k;
  }
  print "L is $l\n";

  $ perl -d:DumpTrace::PPI ./simple-while.pl
  >>>>> simple-while.pl:1:[__top__]:  my ($i:0, $j:9, $l:0) = (0, 9, 0);
  >>>>> simple-while.pl:2:[__top__]:  while ($i:1++ < 6) {
  >>>>> simple-while.pl:3:[__top__]:  my $k:9 = $i:1 * $j:8--;
  >>>   simple-while.pl:4:[__top__]:  next if $k:9 % 5 == 1;
  >>>>> simple-while.pl:5:[__top__]:  $l:9 = $l:0 + $k:9;
  >>>>> simple-while.pl:3:[__top__]:  WHILE: ($i:2++ < 6) 
                                      my $k:16 = $i:2 * $j:7--;
  >>>   simple-while.pl:4:[__top__]:  next if $k:16 % 5 == 1;
  >>>>> simple-while.pl:3:[__top__]:  WHILE: ($i:3++ < 6) 
                                      my $k:21 = $i:3 * $j:6--;
  >>>   simple-while.pl:4:[__top__]:  next if $k:21 % 5 == 1;



( run in 0.556 second using v1.01-cache-2.11-cpan-71847e10f99 )