|
NAMEPerl::Tidy - Parses and beautifies perl sourceSYNOPSISuse Perl::Tidy; my $error_flag = Perl::Tidy::perltidy( source => $source, destination => $destination, stderr => $stderr, argv => $argv, perltidyrc => $perltidyrc, logfile => $logfile, errorfile => $errorfile, teefile => $teefile, debugfile => $debugfile, formatter => $formatter, # callback object (see below) dump_options => $dump_options, dump_options_type => $dump_options_type, prefilter => $prefilter_coderef, postfilter => $postfilter_coderef, ); DESCRIPTIONThis module makes the functionality of the perltidy utility available to perl scripts. Any or all of the input parameters may be omitted, in which case the @ARGV array will be used to provide input parameters as described in the perltidy(1) man page.For example, the perltidy script is basically just this: use Perl::Tidy; Perl::Tidy::perltidy(); The call to perltidy returns a scalar $error_flag which is TRUE if an error caused premature termination, and FALSE if the process ran to normal completion. Additional discuss of errors is contained below in the ERROR HANDLING section. The module accepts input and output streams by a variety of methods. The following list of parameters may be any of the following: a filename, an ARRAY reference, a SCALAR reference, or an object with either a getline or print method, as appropriate. source - the source of the script to be formatted destination - the destination of the formatted output stderr - standard error output perltidyrc - the .perltidyrc file logfile - the .LOG file stream, if any errorfile - the .ERR file stream, if any dump_options - ref to a hash to receive parameters (see below), dump_options_type - controls contents of dump_options dump_getopt_flags - ref to a hash to receive Getopt flags dump_options_category - ref to a hash giving category of options dump_abbreviations - ref to a hash giving all abbreviations The following chart illustrates the logic used to decide how to treat a parameter. ref($param) $param is assumed to be: ----------- --------------------- undef a filename SCALAR ref to string ARRAY ref to array (other) object with getline (if source) or print method If the parameter is an object, and the object has a close method, that close method will be called at the end of the stream.
ERROR HANDLINGAn exit value of 0, 1, or 2 is returned by perltidy to indicate the status of the result.A exit value of 0 indicates that perltidy ran to completion with no error messages. An exit value of 1 indicates that the process had to be terminated early due to errors in the input parameters. This can happen for example if a parameter is misspelled or given an invalid value. The calling program should check for this flag because if it is set the destination stream will be empty or incomplete and should be ignored. Error messages in the stderr stream will indicate the cause of any problem. An exit value of 2 indicates that perltidy ran to completion but there there are warning messages in the stderr stream related to parameter errors or conflicts and/or warning messages in the errorfile stream relating to possible syntax errors in the source code being tidied. In the event of a catastrophic error for which recovery is not possible perltidy terminates by making calls to croak or confess to help the programmer localize the problem. These should normally only occur during program development. NOTES ON FORMATTING PARAMETERSParameters which control formatting may be passed in several ways: in a .perltidyrc configuration file, in the perltidyrc parameter, and in the argv parameter.The -syn (--check-syntax) flag may be used with all source and destination streams except for standard input and output. However data streams which are not associated with a filename will be copied to a temporary file before being passed to Perl. This use of temporary files can cause somewhat confusing output from Perl. If the -pbp style is used it will typically be necessary to also specify a -nst flag. This is necessary to turn off the -st flag contained in the -pbp parameter set which otherwise would direct the output stream to the standard output. EXAMPLESThe following example uses string references to hold the input and output code and error streams, and illustrates checking for errors.use Perl::Tidy; my $source_string = <<'EOT'; my$error=Perl::Tidy::perltidy(argv=>$argv,source=>\$source_string, destination=>\$dest_string,stderr=>\$stderr_string, errorfile=>\$errorfile_string,); EOT my $dest_string; my $stderr_string; my $errorfile_string; my $argv = "-npro"; # Ignore any .perltidyrc at this site $argv .= " -pbp"; # Format according to perl best practices $argv .= " -nst"; # Must turn off -st in case -pbp is specified $argv .= " -se"; # -se appends the errorfile to stderr ## $argv .= " --spell-check"; # uncomment to trigger an error print "<<RAW SOURCE>>\n$source_string\n"; my $error = Perl::Tidy::perltidy( argv => $argv, source => \$source_string, destination => \$dest_string, stderr => \$stderr_string, errorfile => \$errorfile_string, # ignored when -se flag is set ##phasers => 'stun', # uncomment to trigger an error ); if ($error) { # serious error in input parameters, no tidied output print "<<STDERR>>\n$stderr_string\n"; die "Exiting because of serious errors\n"; } if ($dest_string) { print "<<TIDIED SOURCE>>\n$dest_string\n" } if ($stderr_string) { print "<<STDERR>>\n$stderr_string\n" } if ($errorfile_string) { print "<<.ERR file>>\n$errorfile_string\n" } Additional examples are given in examples section of the perltidy distribution. Using the formatter Callback ObjectThe formatter parameter is an optional callback object which allows the calling program to receive tokenized lines directly from perltidy for further specialized processing. When this parameter is used, the two formatting options which are built into perltidy (beautification or html) are ignored. The following diagram illustrates the logical flow:|-- (normal route) -> code beautification caller->perltidy->|-- (-html flag ) -> create html |-- (formatter given)-> callback to write_line This can be useful for processing perl scripts in some way. The parameter $formatter in the perltidy call, formatter => $formatter, is an object created by the caller with a "write_line" method which will accept and process tokenized lines, one line per call. Here is a simple example of a "write_line" which merely prints the line number, the line type (as determined by perltidy), and the text of the line: sub write_line { # This is called from perltidy line-by-line my $self = shift; my $line_of_tokens = shift; my $line_type = $line_of_tokens->{_line_type}; my $input_line_number = $line_of_tokens->{_line_number}; my $input_line = $line_of_tokens->{_line_text}; print "$input_line_number:$line_type:$input_line"; } The complete program, perllinetype, is contained in the examples section of the source distribution. As this example shows, the callback method receives a parameter $line_of_tokens, which is a reference to a hash of other useful information. This example uses these hash entries: $line_of_tokens->{_line_number} - the line number (1,2,...) $line_of_tokens->{_line_text} - the text of the line $line_of_tokens->{_line_type} - the type of the line, one of: SYSTEM - system-specific code before hash-bang line CODE - line of perl code (including comments) POD_START - line starting pod, such as '=head' POD - pod documentation text POD_END - last line of pod section, '=cut' HERE - text of here-document HERE_END - last line of here-doc (target word) FORMAT - format section FORMAT_END - last line of format section, '.' DATA_START - __DATA__ line DATA - unidentified text following __DATA__ END_START - __END__ line END - unidentified text following __END__ ERROR - we are in big trouble, probably not a perl script Most applications will be only interested in lines of type CODE. For another example, let's write a program which checks for one of the so-called naughty matching variables "&`", $&, and "$'", which can slow down processing. Here is a write_line, from the example program find_naughty.pl, which does that: sub write_line { # This is called back from perltidy line-by-line # We're looking for $`, $&, and $' my ( $self, $line_of_tokens ) = @_; # pull out some stuff we might need my $line_type = $line_of_tokens->{_line_type}; my $input_line_number = $line_of_tokens->{_line_number}; my $input_line = $line_of_tokens->{_line_text}; my $rtoken_type = $line_of_tokens->{_rtoken_type}; my $rtokens = $line_of_tokens->{_rtokens}; chomp $input_line; # skip comments, pod, etc return if ( $line_type ne 'CODE' ); # loop over tokens looking for $`, $&, and $' for ( my $j = 0 ; $j < @$rtoken_type ; $j++ ) { # we only want to examine token types 'i' (identifier) next unless $$rtoken_type[$j] eq 'i'; # pull out the actual token text my $token = $$rtokens[$j]; # and check it if ( $token =~ /^\$[\`\&\']$/ ) { print STDERR "$input_line_number: $token\n"; } } } This example pulls out these tokenization variables from the $line_of_tokens hash reference: $rtoken_type = $line_of_tokens->{_rtoken_type}; $rtokens = $line_of_tokens->{_rtokens}; The variable $rtoken_type is a reference to an array of token type codes, and $rtokens is a reference to a corresponding array of token text. These are obviously only defined for lines of type CODE. Perltidy classifies tokens into types, and has a brief code for each type. You can get a complete list at any time by running perltidy from the command line with perltidy --dump-token-types In the present example, we are only looking for tokens of type i (identifiers), so the for loop skips past all other types. When an identifier is found, its actual text is checked to see if it is one being sought. If so, the above write_line prints the token and its line number. The formatter feature is relatively new in perltidy, and further documentation needs to be written to complete its description. However, several example programs have been written and can be found in the examples section of the source distribution. Probably the best way to get started is to find one of the examples which most closely matches your application and start modifying it. For help with perltidy's peculiar way of breaking lines into tokens, you might run, from the command line, perltidy -D filename where filename is a short script of interest. This will produce filename.DEBUG with interleaved lines of text and their token types. The -D flag has been in perltidy from the beginning for this purpose. If you want to see the code which creates this file, it is "write_debug_entry" in Tidy.pm. EXPORT&perltidy INSTALLATIONThe module 'Perl::Tidy' comes with a binary 'perltidy' which is installed when the module is installed. The module name is case-sensitive. For example, the basic command for installing with cpanm is 'cpanm Perl::Tidy'.VERSIONThis man page documents Perl::Tidy version 20211029LICENSEThis package is free software; you can redistribute it and/or modify it under the terms of the "GNU General Public License".Please refer to the file "COPYING" for details. BUG REPORTSA list of current bugs and issues can be found at the CPAN site <https://rt.cpan.org/Public/Dist/Display.html?Name=Perl-Tidy>To report a new bug or problem, use the link on this page. The source code repository is at <https://github.com/perltidy/perltidy>. SEE ALSOThe perltidy(1) man page describes all of the features of perltidy. It can be found at http://perltidy.sourceforge.net.
Visit the GSP FreeBSD Man Page Interface. |