PHP::Decode::Parser
# Create an instance sub warn_msg { my ($action, $fmt) = (shift, shift); my $msg = sprintf $fmt, @_; print 'WARN: ', $action, ': ', $msg, "\n"; } my %strmap; my $parser = PHP::Decode::Parser->new(strmap => \%strmap, filename => 'test', warn => \&warn_msg); # Parse php token list my $line = '<?php echo "test"; ?>'; my $quote = $parser->tokenize_line($line); my $tokens = $parser->tokens(); my $stmt = $parser->read_code($tokens); # Expand to code again my $code = $parser->format_stmt($stmt, {format => 1}); print $code;
The PHP::Decode::Parser Module tokenizes and parses php code strings. The parser does not depend on a special php version. It supports most php syntax of interpreters from php5 to php8.
The parser assumes that the input file is a valid php script, and does not enforce strict syntactic checks. Unrecognized tokens are simply passed through.
The parser converts the php code into a unified form when the resulting output is formatted (for example intermittent php-script-tags and variables from string interpolation are removed and rewritten to php echo statements).
$parser = PHP::Decode::Parser->new(%args);
Create a PHP::Decode::Parser object. Arguments are passed in key => value pairs.
The only required argument is `strmap`.
The new constructor dies when arguments are invalid, or if required arguments are missing.
The accepted arguments are:
Tokenize a php code string
quote = $parser->tokenize_line($line);
See the description of the PHP::Decode::Tokenizer module for the token types.
Parse a token list
<tok> php token list $stmt = $parser->read_code($tokens);
The read_code method converts the token list to an internal representation of php statements. If a script contains more than one top level statement, the method returns block with a list of these statements.
The following types are used to represent php statements:
Each of of these statements is uniquely numbered and stored in the strmap of the parser.
Format a php statement to a php code string.
$code = $parser->format_stmt($stmt, $fmt);
Requires the PHP::Decode::Tokenizer Module.
Barnim Dzwillo @ Strato AG
To install PHP::Decode, copy and paste the appropriate command in to your terminal.
cpanm
cpanm PHP::Decode
CPAN shell
perl -MCPAN -e shell install PHP::Decode
For more information on module installation, please visit the detailed CPAN module installation guide.