X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=docs%2FTidy.html;h=f81e27863ac50ae3265d03850610c24e0bf0bbc9;hb=8360fafa7774a02a63bd43854a82f22c335851d9;hp=2d3a162aa8ad9fffba3724f58ce3b897317eba95;hpb=657098da8da16dccd551721ffc180956d8aab7fc;p=perltidy.git diff --git a/docs/Tidy.html b/docs/Tidy.html index 2d3a162..f81e278 100644 --- a/docs/Tidy.html +++ b/docs/Tidy.html @@ -20,6 +20,7 @@
  • EXAMPLES
  • Using the formatter Callback Object
  • EXPORT
  • +
  • INSTALLATION
  • VERSION
  • LICENSE
  • BUG REPORTS
  • @@ -42,6 +43,8 @@ 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, @@ -58,7 +61,7 @@
        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 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.

    @@ -66,9 +69,9 @@ destination - the destination of the formatted output stderr - standard error output perltidyrc - the .perltidyrc file - logfile - the .LOG file stream, if any + 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 - 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 @@ -87,31 +90,60 @@
    -
    source
    +
    source

    If the source parameter is given, it defines the source of the input stream. If an input stream is defined with the source parameter then no other source filenames may be specified in the @ARGV array or argv parameter.

    -
    destination
    +
    destination

    If the destination parameter is given, it will be used to define the file or memory location to receive output of perltidy.

    +

    Important note if destination is a string or array reference. Perl strings of characters which are decoded as utf8 by Perl::Tidy can be returned in either of two possible states, decoded or encoded, and it is important that the calling program and Perl::Tidy are in agreement regarding the state to be returned. A flag --encode-output-strings, or simply -eos, was added in Perl::Tidy version 20220217 for this purpose.

    + + + +

    For some background information see https://github.com/perltidy/perltidy/blob/master/docs/eos_flag.md.

    + +

    This change in default behavior was made over a period of time as follows:

    + + +
    -
    stderr
    +
    stderr

    The stderr parameter allows the calling program to redirect the stream that would otherwise go to the standard error output device to any of the stream types listed above. This stream contains important warnings and errors related to the parameters passed to perltidy.

    -
    perltidyrc
    +
    perltidyrc

    If the perltidyrc file is given, it will be used instead of any .perltidyrc configuration file that would otherwise be used.

    -
    errorfile
    +
    errorfile

    The errorfile parameter allows the calling program to capture the stream that would otherwise go to either a .ERR file. This stream contains warnings or errors related to the contents of one source file or stream.

    @@ -121,19 +153,31 @@

    However if perltidy is called to process just a single perl script then it may be more convenient to combine the errorfile stream with the stderr stream. This can be done by setting the -se parameter, in which case this parameter is ignored.

    -
    logfile
    +
    logfile

    The logfile parameter allows the calling program to capture the log stream. This stream is only created if requested with a -g parameter. It contains detailed diagnostic information about a script which may be useful for debugging.

    -
    argv
    +
    teefile
    +
    + +

    The teefile parameter allows the calling program to capture the tee stream. This stream is only created if requested with one of the 'tee' parameters, a --tee-pod , --tee-block-comments, --tee-side-commnts, or --tee-all-comments.

    + +
    +
    debugfile
    +
    + +

    The debugfile parameter allows the calling program to capture the stream produced by the --DEBUG parameter. This parameter is mainly used for debugging perltidy itself.

    + +
    +
    argv

    If the argv parameter is given, it will be used instead of the @ARGV array. The argv parameter may be a string, a reference to a string, or a reference to an array. If it is a string or reference to a string, it will be parsed into an array of items just as if it were a command line string.

    -
    dump_options
    +
    dump_options

    If the dump_options parameter is given, it must be the reference to a hash. In this case, the parameters contained in any perltidyrc configuration file will be placed in this hash and perltidy will return immediately. This is equivalent to running perltidy with --dump-options, except that the parameters are returned in a hash rather than dumped to standard output. Also, by default only the parameters in the perltidyrc file are returned, but this can be changed (see the next parameter). This parameter provides a convenient method for external programs to read a perltidyrc file. An example program using this feature, perltidyrc_dump.pl, is included in the distribution.

    @@ -141,37 +185,37 @@

    Any combination of the dump_ parameters may be used together.

    -
    dump_options_type
    +
    dump_options_type

    This parameter is a string which can be used to control the parameters placed in the hash reference supplied by dump_options. The possible values are 'perltidyrc' (default) and 'full'. The 'full' parameter causes both the default options plus any options found in a perltidyrc file to be returned.

    -
    dump_getopt_flags
    +
    dump_getopt_flags

    If the dump_getopt_flags parameter is given, it must be the reference to a hash. This hash will receive all of the parameters that perltidy understands and flags that are passed to Getopt::Long. This parameter may be used alone or with the dump_options flag. Perltidy will exit immediately after filling this hash. See the demo program perltidyrc_dump.pl for example usage.

    -
    dump_options_category
    +
    dump_options_category

    If the dump_options_category parameter is given, it must be the reference to a hash. This hash will receive a hash with keys equal to all long parameter names and values equal to the title of the corresponding section of the perltidy manual. See the demo program perltidyrc_dump.pl for example usage.

    -
    dump_abbreviations
    +
    dump_abbreviations

    If the dump_abbreviations parameter is given, it must be the reference to a hash. This hash will receive all abbreviations used by Perl::Tidy. See the demo program perltidyrc_dump.pl for example usage.

    -
    prefilter
    +
    prefilter

    A code reference that will be applied to the source before tidying. It is expected to take the full content as a string in its input, and output the transformed content.

    -
    postfilter
    +
    postfilter

    A code reference that will be applied to the tidied result before outputting. It is expected to take the full content as a string in its input, and output the transformed content.

    @@ -183,9 +227,13 @@

    ERROR HANDLING

    -

    Perltidy will return with an error flag indicating if 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 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 0, 1, or 2 is returned by perltidy to indicate the status of the result.

    -

    If the error flag is not set then perltidy ran to completion. However there may still be warning messages in the stderr stream related to control parameters, and there may be warning messages in the errorfile stream relating to possible syntax errors in the source code being tidied.

    +

    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.

    @@ -202,13 +250,13 @@

    The 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;
    @@ -217,9 +265,9 @@
       $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,
    @@ -228,14 +276,14 @@
           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" }
    @@ -247,17 +295,17 @@

    The 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 
    +  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,  
    +
            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;
    @@ -291,11 +339,11 @@
     

    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};
    @@ -303,19 +351,19 @@
          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
    @@ -335,21 +383,25 @@
     
     

    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.

    +

    The examples section of the source distribution has some examples of programs which use the formatter option.

    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.

    +

    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 sub Perl::Tidy::Debugger::write_debug_entry

    EXPORT

      &perltidy
    +

    INSTALLATION

    + +

    The 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'.

    +

    VERSION

    -

    This man page documents Perl::Tidy version 20190601

    +

    This man page documents Perl::Tidy version 20221112

    LICENSE

    @@ -359,12 +411,10 @@

    BUG REPORTS

    -

    A 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.

    +

    To report a new bug or problem, use the "issues" link on this page.

    +

    SEE ALSO

    The perltidy(1) man page describes all of the features of perltidy. It can be found at http://perltidy.sourceforge.net.