Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-20 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found