YANZ UPLOAD ANJG
#!/usr/bin/perl
package main;
use Perl::Tidy;
my $arg_string = undef;
# give Macs a chance to provide command line parameters
if ( $^O =~ /Mac/ ) {
$arg_string = MacPerl::Ask(
'Please enter @ARGV (-h for help)',
defined $ARGV[0] ? "\"$ARGV[0]\"" : ""
);
}
# Exit codes returned by perltidy:
# 0 = no errors
# 1 = perltidy could not run to completion due to errors
# 2 = perltidy ran to completion with error messages
exit Perl::Tidy::perltidy( argv => $arg_string );
__END__
=head1 NAME
perltidy - a perl script indenter and reformatter
=head1 SYNOPSIS
perltidy [ options ] file1 file2 file3 ...
(output goes to file1.tdy, file2.tdy, file3.tdy, ...)
perltidy [ options ] file1 -o outfile
perltidy [ options ] file1 -st >outfile
perltidy [ options ] outfile
=head1 DESCRIPTION
Perltidy reads a perl script and writes an indented, reformatted script.
Many users will find enough information in L<"EXAMPLES"> to get
started. New users may benefit from the short tutorial
which can be found at
http://perltidy.sourceforge.net/tutorial.html
A convenient aid to systematically defining a set of style parameters
can be found at
http://perltidy.sourceforge.net/stylekey.html
Perltidy can produce output on either of two modes, depending on the
existence of an B<-html> flag. Without this flag, the output is passed
through a formatter. The default formatting tries to follow the
recommendations in perlstyle(1), but it can be controlled in detail with
numerous input parameters, which are described in L<"FORMATTING
OPTIONS">.
When the B<-html> flag is given, the output is passed through an HTML
formatter which is described in L<"HTML OPTIONS">.
=head1 EXAMPLES
perltidy somefile.pl
This will produce a file F containing the script reformatted
using the default options, which approximate the style suggested in
perlstyle(1). The source file F is unchanged.
perltidy *.pl
Execute perltidy on all F<.pl> files in the current directory with the
default options. The output will be in files with an appended F<.tdy>
extension. For any file with an error, there will be a file with extension
F<.ERR>.
perltidy -b file1.pl file2.pl
Modify F and F in place, and backup the originals to
F and F. If F and/or F
already exist, they will be overwritten.
perltidy -b -bext='/' file1.pl file2.pl
Same as the previous example except that the backup files F and F will be deleted if there are no errors.
perltidy -gnu somefile.pl
Execute perltidy on file F with a style which approximates the
GNU Coding Standards for C programs. The output will be F.
perltidy -i=3 somefile.pl
Execute perltidy on file F, with 3 columns for each level of
indentation (B<-i=3>) instead of the default 4 columns. There will not be any
tabs in the reformatted script, except for any which already exist in comments,
pod documents, quotes, and here documents. Output will be F.
perltidy -i=3 -et=8 somefile.pl
Same as the previous example, except that leading whitespace will
be entabbed with one tab character per 8 spaces.
perltidy -ce -l=72 somefile.pl
Execute perltidy on file F with all defaults except use "cuddled
elses" (B<-ce>) and a maximum line length of 72 columns (B<-l=72>) instead of
the default 80 columns.
perltidy -g somefile.pl
Execute perltidy on file F and save a log file F
which shows the nesting of braces, parentheses, and square brackets at
the start of every line.
perltidy -html somefile.pl
This will produce a file F containing the script with
html markup. The output file will contain an embedded style sheet in
the section which may be edited to change the appearance.
perltidy -html -css=mystyle.css somefile.pl
This will produce a file F containing the script with
html markup. This output file will contain a link to a separate style
sheet file F. If the file F does not exist,
it will be created. If it exists, it will not be overwritten.
perltidy -html -pre somefile.pl
Write an html snippet with only the PRE section to F.
This is useful when code snippets are being formatted for inclusion in a
larger web page. No style sheet will be written in this case.
perltidy -html -ss >mystyle.css
Write a style sheet to F and exit.
perltidy -html -frm mymodule.pm
Write html with a frame holding a table of contents and the source code. The
output files will be F (the frame), F
(the table of contents), and F (the source code).
=head1 OPTIONS - OVERVIEW
The entire command line is scanned for options, and they are processed
before any files are processed. As a result, it does not matter
whether flags are before or after any filenames. However, the relative
order of parameters is important, with later parameters overriding the
values of earlier parameters.
For each parameter, there is a long name and a short name. The short
names are convenient for keyboard input, while the long names are
self-documenting and therefore useful in scripts. It is customary to
use two leading dashes for long names, but one may be used.
Most parameters which serve as on/off flags can be negated with a
leading "n" (for the short name) or a leading "no" or "no-" (for the
long name). For example, the flag to outdent long quotes is B<-olq>
or B<--outdent-long-quotes>. The flag to skip this is B<-nolq>
or B<--nooutdent-long-quotes> or B<--no-outdent-long-quotes>.
Options may not be bundled together. In other words, options B<-q> and
B<-g> may NOT be entered as B<-qg>.
Option names may be terminated early as long as they are uniquely identified.
For example, instead of B<--dump-token-types>, it would be sufficient to enter
B<--dump-tok>, or even B<--dump-t>, to uniquely identify this command.
=head2 I/O control
The following parameters concern the files which are read and written.
=over 4
=item B<-h>, B<--help>
Show summary of usage and exit.
=item B<-o>=filename, B<--outfile>=filename
Name of the output file (only if a single input file is being
processed). If no output file is specified, and output is not
redirected to the standard output (see B<-st>), the output will go to
F. [Note: - does not redirect to standard output. Use
B<-st> instead.]
=item B<-st>, B<--standard-output>
Perltidy must be able to operate on an arbitrarily large number of files
in a single run, with each output being directed to a different output
file. Obviously this would conflict with outputting to the single
standard output device, so a special flag, B<-st>, is required to
request outputting to the standard output. For example,
perltidy somefile.pl -st >somefile.new.pl
This option may only be used if there is just a single input file.
The default is B<-nst> or B<--nostandard-output>.
=item B<-se>, B<--standard-error-output>
If perltidy detects an error when processing file F, its
default behavior is to write error messages to file F.
Use B<-se> to cause all error messages to be sent to the standard error
output stream instead. This directive may be negated with B<-nse>.
Thus, you may place B<-se> in a F<.perltidyrc> and override it when
desired with B<-nse> on the command line.
=item B<-oext>=ext, B<--output-file-extension>=ext
Change the extension of the output file to be F instead of the
default F (or F in case the -B<-html> option is used).
See L.
=item B<-opath>=path, B<--output-path>=path
When perltidy creates a filename for an output file, by default it merely
appends an extension to the path and basename of the input file. This
parameter causes the path to be changed to F instead.
The path should end in a valid path separator character, but perltidy will try
to add one if it is missing.
For example
perltidy somefile.pl -opath=/tmp/
will produce F. Otherwise, F will
appear in whatever directory contains F.
If the path contains spaces, it should be placed in quotes.
This parameter will be ignored if output is being directed to standard output,
or if it is being specified explicitly with the B<-o=s> parameter.
=item B<-b>, B<--backup-and-modify-in-place>
Modify the input file or files in-place and save the original with the
extension F<.bak>. Any existing F<.bak> file will be deleted. See next
item for changing the default backup extension, and for eliminating the
backup file altogether.
A B<-b> flag will be ignored if input is from standard input or goes to
standard output, or if the B<-html> flag is set.
In particular, if you want to use both the B<-b> flag and the B<-pbp>
(--perl-best-practices) flag, then you must put a B<-nst> flag after the
B<-pbp> flag because it contains a B<-st> flag as one of its components,
which means that output will go to the standard output stream.
=item B<-bext>=ext, B<--backup-file-extension>=ext
This parameter serves two purposes: (1) to change the extension of the backup
file to be something other than the default F<.bak>, and (2) to indicate
that no backup file should be saved.
To change the default extension to something other than F<.bak> see
L.
A backup file of the source is always written, but you can request that it
be deleted at the end of processing if there were no errors. This is risky
unless the source code is being maintained with a source code control
system.
To indicate that the backup should be deleted include one forward slash,
B>, in the extension. If any text remains after the slash is removed
it will be used to define the backup file extension (which is always
created and only deleted if there were no errors).
Here are some examples:
Parameter Extension Backup File Treatment
<-bext=bak> F<.bak> Keep (same as the default behavior)
<-bext='/'> F<.bak> Delete if no errors
<-bext='/backup'> F<.backup> Delete if no errors
<-bext='original/'> F<.original> Delete if no errors
=item B<-w>, B<--warning-output>
Setting B<-w> causes any non-critical warning
messages to be reported as errors. These include messages
about possible pod problems, possibly bad starting indentation level,
and cautions about indirect object usage. The default, B<-nw> or
B<--nowarning-output>, is not to include these warnings.
=item B<-q>, B<--quiet>
Deactivate error messages (for running under an editor).
For example, if you use a vi-style editor, such as vim, you may execute
perltidy as a filter from within the editor using something like
:n1,n2!perltidy -q
where C represents the selected text. Without the B<-q> flag,
any error message may mess up your screen, so be prepared to use your
"undo" key.
=item B<-log>, B<--logfile>
Save the F<.LOG> file, which has many useful diagnostics. Perltidy always
creates a F<.LOG> file, but by default it is deleted unless a program bug is
suspected. Setting the B<-log> flag forces the log file to be saved.
=item B<-g=n>, B<--logfile-gap=n>
Set maximum interval between input code lines in the logfile. This purpose of
this flag is to assist in debugging nesting errors. The value of C is
optional. If you set the flag B<-g> without the value of C, it will be
taken to be 1, meaning that every line will be written to the log file. This
can be helpful if you are looking for a brace, paren, or bracket nesting error.
Setting B<-g> also causes the logfile to be saved, so it is not necessary to
also include B<-log>.
If no B<-g> flag is given, a value of 50 will be used, meaning that at least
every 50th line will be recorded in the logfile. This helps prevent
excessively long log files.
Setting a negative value of C is the same as not setting B<-g> at all.
=item B<-npro> B<--noprofile>
Ignore any F<.perltidyrc> command file. Normally, perltidy looks first in
your current directory for a F<.perltidyrc> file of parameters. (The format
is described below). If it finds one, it applies those options to the
initial default values, and then it applies any that have been defined
on the command line. If no F<.perltidyrc> file is found, it looks for one
in your home directory.
If you set the B<-npro> flag, perltidy will not look for this file.
=item B<-pro=filename> or B<--profile=filename>
To simplify testing and switching .perltidyrc files, this command may be
used to specify a configuration file which will override the default
name of .perltidyrc. There must not be a space on either side of the
'=' sign. For example, the line
perltidy -pro=testcfg
would cause file F to be used instead of the
default F<.perltidyrc>.
A pathname begins with three dots, e.g. ".../.perltidyrc", indicates that
the file should be searched for starting in the current directory and
working upwards. This makes it easier to have multiple projects each with
their own .perltidyrc in their root directories.
=item B<-opt>, B<--show-options>
Write a list of all options used to the F<.LOG> file.
Please see B<--dump-options> for a simpler way to do this.
=item B<-f>, B<--force-read-binary>
Force perltidy to process binary files. To avoid producing excessive
error messages, perltidy skips files identified by the system as non-text.
However, valid perl scripts containing binary data may sometimes be identified
as non-text, and this flag forces perltidy to process them.
=item B<-ast>, B<--assert-tidy>
This flag asserts that the input and output code streams are identical, or in
other words that the input code is already 'tidy' according to the formatting
parameters. If this is not the case, an error message noting this is produced.
This error message will cause the process to return a non-zero exit code.
The test for this is made by comparing an MD5 hash value for the input and
output code streams. This flag has no other effect on the functioning of
perltidy. This might be useful for certain code maintenance operations.
Note: you will not see this message if you have error messages turned off with the
-quiet flag.
=item B<-asu>, B<--assert-untidy>
This flag asserts that the input and output code streams are different, or in
other words that the input code is 'untidy' according to the formatting
parameters. If this is not the case, an error message noting this is produced.
This flag has no other effect on the functioning of perltidy.
=item B<-sal=s>, B<--sub-alias-list=s>
This flag causes one or more words to be treated the same as if they were the keyword 'sub'. The string B contains one or more alias words, separated by spaces or commas.
For example,
perltidy -sal='method fun _sub M4'
will cause the perltidy to treate the words 'method', 'fun', '_sub' and 'M4' to be treated the same as if they were 'sub'. Note that if the alias words are separated by spaces then the string of words should be placed in quotes.
Note that several other parameters accept a list of keywords, including 'sub' (see L).
You do not need to include any sub aliases in these lists. Just include keyword 'sub' if you wish, and all aliases are automatically included.
=back
=head1 FORMATTING OPTIONS
=head2 Basic Options
=over 4
=item B<--notidy>
This flag disables all formatting and causes the input to be copied unchanged
to the output except for possible changes in line ending characters and any
pre- and post-filters. This can be useful in conjunction with a hierarchical
set of F<.perltidyrc> files to avoid unwanted code tidying. See also
L for a way to avoid tidying specific
sections of code.
=item B<-i=n>, B<--indent-columns=n>
Use n columns per indentation level (default n=4).
=item B<-l=n>, B<--maximum-line-length=n>
The default maximum line length is n=80 characters. Perltidy will try
to find line break points to keep lines below this length. However, long
quotes and side comments may cause lines to exceed this length.
The default length of 80 comes from the past when this was the standard CRT
screen width. Many programmers prefer to increase this to something like 120.
Setting B<-l=0> is equivalent to setting B<-l=(a very large number)>. But this is
not recommended because, for example, a very long list will be formatted in a
single long line.
=item B<-vmll>, B<--variable-maximum-line-length>
A problem arises using a fixed maximum line length with very deeply nested code
and data structures because eventually the amount of leading whitespace used
for indicating indentation takes up most or all of the available line width,
leaving little or no space for the actual code or data. One solution is to use
a vary long line length. Another solution is to use the B<-vmll> flag, which
basically tells perltidy to ignore leading whitespace when measuring the line
length.
To be precise, when the B<-vmll> parameter is set, the maximum line length of a
line of code will be M+L*I, where
M is the value of --maximum-line-length=M (-l=M), default 80,
I is the value of --indent-columns=I (-i=I), default 4,
L is the indentation level of the line of code
When this flag is set, the choice of breakpoints for a block of code should be
essentially independent of its nesting depth. However, the absolute line
lengths, including leading whitespace, can still be arbitrarily large. This
problem can be avoided by including the next parameter.
The default is not to do this (B<-nvmll>).
=item B<-wc=n>, B<--whitespace-cycle=n>
This flag also addresses problems with very deeply nested code and data
structures. When the nesting depth exceeds the value B the leading
whitespace will be reduced and start at a depth of 1 again. The result is that
blocks of code will shift back to the left rather than moving arbitrarily far
to the right. This occurs cyclically to any depth.
For example if one level of indentation equals 4 spaces (B<-i=4>, the default),
and one uses B<-wc=15>, then if the leading whitespace on a line exceeds about
4*15=60 spaces it will be reduced back to 4*1=4 spaces and continue increasing
from there. If the whitespace never exceeds this limit the formatting remains
unchanged.
The combination of B<-vmll> and B<-wc=n> provides a solution to the problem of
displaying arbitrarily deep data structures and code in a finite window,
although B<-wc=n> may of course be used without B<-vmll>.
The default is not to use this, which can also be indicated using B<-wc=0>.
=item tabs
Using tab characters will almost certainly lead to future portability
and maintenance problems, so the default and recommendation is not to
use them. For those who prefer tabs, however, there are two different
options.
Except for possibly introducing tab indentation characters, as outlined
below, perltidy does not introduce any tab characters into your file,
and it removes any tabs from the code (unless requested not to do so
with B<-fws>). If you have any tabs in your comments, quotes, or
here-documents, they will remain.
=over 4
=item B<-et=n>, B<--entab-leading-whitespace>
This flag causes each B initial space characters to be replaced by
one tab character.
The value of the integer B can be any value but can be coordinated with the
number of spaces used for intentation. For example, B<-et=4 -ci=4 -i=4> will
produce one tab for each indentation level and and one for each continuation
indentation level. You may want to coordinate the value of B with what your
display software assumes for the spacing of a tab.
=item B<-t>, B<--tabs>
This flag causes one leading tab character to be inserted for each level
of indentation. Certain other features are incompatible with this
option, and if these options are also given, then a warning message will
be issued and this flag will be unset. One example is the B<-lp>
option. This flag is retained for backwards compatability, but
if you use tabs, the B<-et=n> flag is recommended.
=item B<-dt=n>, B<--default-tabsize=n>
If the first line of code passed to perltidy contains leading tabs but no
tab scheme is specified for the output stream then perltidy must guess how many
spaces correspond to each leading tab. This number of spaces B
corresponding to each leading tab of the input stream may be specified with
B<-dt=n>. The default is B.
This flag has no effect if a tab scheme is specified for the output stream,
because then the input stream is assumed to use the same tab scheme and
indentation spaces as for the output stream (any other assumption would lead to
unstable editing).
=back
=item B<-syn>, B<--check-syntax>
This flag is now ignored for safety, but the following documentation
has been retained for reference.
This flag causes perltidy to run C to check syntax of input
and output. (To change the flags passed to perl, see the next
item, B<-pscf>). The results are written to the F<.LOG> file, which
will be saved if an error is detected in the output script. The output
script is not checked if the input script has a syntax error. Perltidy
does its own checking, but this option employs perl to get a "second
opinion".
If perl reports errors in the input file, they will not be reported in
the error output unless the B<--warning-output> flag is given.
The default is B to do this type of syntax checking (although
perltidy will still do as much self-checking as possible). The reason
is that it causes all code in BEGIN blocks to be executed, for all
modules being used, and this opens the door to security issues and
infinite loops when running perltidy.
=item B<-pscf=s>, B<-perl-syntax-check-flags=s>
When perl is invoked to check syntax, the normal flags are C<-c -T>. In
addition, if the B<-x> flag is given to perltidy, then perl will also be
passed a B<-x> flag. It should not normally be necessary to change
these flags, but it can be done with the B<-pscf=s> flag. For example,
if the taint flag, C<-T>, is not wanted, the flag could be set to be just
B<-pscf=-c>.
Perltidy will pass your string to perl with the exception that it will
add a B<-c> and B<-x> if appropriate. The F<.LOG> file will show
exactly what flags were passed to perl.
=item B<-xs>, B<--extended-syntax>
A problem with formatting Perl code is that some modules can introduce new
syntax. This flag allows perltidy to handle certain common extensions
to the standard syntax without complaint.
For example, without this flag a structure such as the following would generate
a syntax error and the braces would not be balanced:
method deposit( Num $amount) {
$self->balance( $self->balance + $amount );
}
For one of the extensions, module Switch::Plain, colons are marked as labels.
If you use this module, you may want to also use the B<--nooutdent-labels> flag
to prevent lines such as 'default:' from being outdented.
This flag is enabled by default but it can be deactivated with B<-nxs>.
Probably the only reason to deactivate this flag is to generate more diagnostic
messages when debugging a script.
=item B<-io>, B<--indent-only>
This flag is used to deactivate all whitespace and line break changes
within non-blank lines of code.
When it is in effect, the only change to the script will be
to the indentation and to the number of blank lines.
And any flags controlling whitespace and newlines will be ignored. You
might want to use this if you are perfectly happy with your whitespace
and line breaks, and merely want perltidy to handle the indentation.
(This also speeds up perltidy by well over a factor of two, so it might be
useful when perltidy is merely being used to help find a brace error in
a large script).
Setting this flag is equivalent to setting B<--freeze-newlines> and
B<--freeze-whitespace>.
If you also want to keep your existing blank lines exactly
as they are, you can add B<--freeze-blank-lines>.
With this option perltidy is still free to modify the indenting (and
outdenting) of code and comments as it normally would. If you also want to
prevent long comment lines from being outdented, you can add either B<-noll> or
B<-l=0>.
Setting this flag will prevent perltidy from doing any special operations on
closing side comments. You may still delete all side comments however when
this flag is in effect.
=item B<-enc=s>, B<--character-encoding=s>
This flag indicates the character encoding, if any, of the input data stream.
Perltidy does not look for the encoding directives in the soure stream, such
as B