Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Visualize Perl Program -- PPI

by Discipulus (Canon)
on Oct 01, 2020 at 10:53 UTC ( [id://11122410]=note: print w/replies, xml ) Need Help??


in reply to Visualize Perl Program

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11122410]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-26 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found