Perl-Critic / PPI

53 stars 44 forks source link

PPI documented example doesn't work #193

Closed Astara closed 2 years ago

Astara commented 8 years ago

I tried a simple (program from a string using the example:

Create a document from source

$Document = PPI::Document->new(\'print "Hello World!\n"');

Prog: ppi_dump:

!/usr/bin/perl

use PPI; use PPI::Dumper; use P;

P "parse: %s", $ARGV[0]; my $doc=PPI::Document->new($ARGV[0]); my $dumpr=PPI::Dumper->new($doc, qw(whitespace 0 comments 0));

my $toks = $dumpr->list;

called in various ways:

ppi_dump "'print \"Hello World!\n\"'" parse: 'print "Hello World!\n"' Can't call method "list" on an undefined value at ./ppi_dump line 10. ppi_dump "print \"Hello World!\n\""
parse: print "Hello World!\n" Can't call method "list" on an undefined value at ./ppi_dump line 10.

-- It doesn't seem the example listed in the pod/manpage documentation works. So what am I doing wrong? I can't imagine such a basic example/function not working. Sorry for the bother!... sigh

BillCruise commented 6 years ago

When you pass a normal string to PPI::Document->new, it assumes that you passed a filename and tries to load code from a file. Since you're passing code directly to new, you have to pass it as a reference to a scalar. Note that \

$doc = PPI::Document->new( \$source );

So your example should be

$Document = PPI::Document->new( \'print "Hello World!\n"' );

Reference: http://search.cpan.org/~mithaldu/PPI-1.236/lib/PPI/Document.pm

Astara commented 6 years ago

Bill Cruise wrote:

When you pass a normal string to PPI::Document->new, it assumes that you passed a filename and tries to load code from a file. Since you're passing code directly to new, you have to pass it as a reference to a scalar. Note that \

$doc = PPI::Document->new( $source );


Yeah, thought it was something like that, but had problems getting the syntax to work at the time. So shelved that project, for now, to work on something else. Will likely be a while before I get back to that project (tty calc improvements) as it wasn't very high prio.

Wanted to be able to know if the line just entered was a complete

line or not and would be safe to pass to eval. Right now, in the calc, if I want to enter a function or such -- all needs to be on one line, as the calculator calls 'eval' after every line. Thought it would be a nice 'upgrade', but have other projects that are a bit higher prio (not that they are that high either... ;-))...

Thx...