Hello aartist,

I'm currently experimenting with PPI and it can be used to visualize your program.

Using davido sample code I arranged:

use strict; use warnings; use PPI; use PPI::Dumper; # retrieve the example standalone script from DATA my $doc; while (<DATA>){ $doc .= $_} my $pdom = PPI::Document->new( \$doc ); # uncomment to see the whole pdom #PPI::Dumper->new($pdom)->print; $pdom->find( sub{ # skip white spaces, for example return if $_[1]->isa('PPI::Token::Whitespace'); # just print top level elements if ( # is a top level element $_[1]->parent == $pdom ){ # PPI class remove \n at the en +d starting line print "found TOPLEVEL ",ref $_[1]," ", $_[1]=~s/\n$//r ," +(line ",$_[1]->line_number,")\n"; } } ); __DATA__ #!/usr/bin/env perl my $fb = fizzbuzz(); print "$_\n" for @$fb; sub fizzbuzz { my ($n, $m) = (1, 100); my @out; for my $num ($n .. $m) { my $line = ''; $line .= 'fizz' if $num % 3 == 0; $line .= 'buzz' if $num % 5 == 0; push @out, $line || $num; } return \@out; }

and the output is:

found TOPLEVEL PPI::Token::Comment #!/usr/bin/env perl (line 1) found TOPLEVEL PPI::Statement::Variable my $fb = fizzbuzz(); (line 3) found TOPLEVEL PPI::Statement print "$_\n" for @$fb; (line 4) found TOPLEVEL PPI::Statement::Sub sub fizzbuzz { my ($n, $m) = (1, 100); my @out; for my $num ($n .. $m) { my $line = ''; $line .= 'fizz' if $num % 3 == 0; $line .= 'buzz' if $num % 5 == 0; push @out, $line || $num; } return \@out; } (line 6)

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: Visualize Perl Program -- PPI by Discipulus
in thread Visualize Perl Program by aartist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.