Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man3 / Perl::Tidy.3
CommitLineData
86530b38
AT
1.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "Perl::Tidy 3"
132.TH Perl::Tidy 3 "2003-10-21" "perl v5.8.0" "User Contributed Perl Documentation"
133.SH "NAME"
134Perl::Tidy \- Parses and beautifies perl source
135.SH "SYNOPSIS"
136.IX Header "SYNOPSIS"
137.Vb 1
138\& use Perl::Tidy;
139.Ve
140.PP
141.Vb 10
142\& Perl::Tidy::perltidy(
143\& source => $source,
144\& destination => $destination,
145\& stderr => $stderr,
146\& argv => $argv,
147\& perltidyrc => $perltidyrc,
148\& logfile => $logfile,
149\& errorfile => $errorfile,
150\& formatter => $formatter, # callback object (see below)
151\& );
152.Ve
153.SH "DESCRIPTION"
154.IX Header "DESCRIPTION"
155This module makes the functionality of the perltidy utility available to perl
156scripts. Any or all of the input parameters may be omitted, in which case the
157\&\f(CW@ARGV\fR array will be used to provide input parameters as described
158in the \fIperltidy\fR\|(1) man page.
159.PP
160For example, the perltidy script is basically just this:
161.PP
162.Vb 2
163\& use Perl::Tidy;
164\& Perl::Tidy::perltidy();
165.Ve
166.PP
167The module accepts input and output streams by a variety of methods.
168The following list of parameters may be any of a the following: a
169filename, an \s-1ARRAY\s0 reference, a \s-1SCALAR\s0 reference, or an object with
170either a \fBgetline\fR or \fBprint\fR method, as appropriate.
171.PP
172.Vb 6
173\& source - the source of the script to be formatted
174\& destination - the destination of the formatted output
175\& stderr - standard error output
176\& perltidyrc - the .perltidyrc file
177\& logfile - the .LOG file stream, if any
178\& errorfile - the .ERR file stream, if any
179.Ve
180.PP
181The following chart illustrates the logic used to decide how to
182treat a parameter.
183.PP
184.Vb 6
185\& ref($param) $param is assumed to be:
186\& ----------- ---------------------
187\& undef a filename
188\& SCALAR ref to string
189\& ARRAY ref to array
190\& (other) object with getline (if source) or print method
191.Ve
192.PP
193If the parameter is an object, and the object has a \fBclose\fR method, that
194close method will be called at the end of the stream.
195.IP "source" 4
196.IX Item "source"
197If the \fBsource\fR parameter is given, it defines the source of the
198input stream.
199.IP "destination" 4
200.IX Item "destination"
201If the \fBdestination\fR parameter is given, it will be used to define the
202file or memory location to receive output of perltidy.
203.IP "stderr" 4
204.IX Item "stderr"
205The \fBstderr\fR parameter allows the calling program to capture the output
206to what would otherwise go to the standard error output device.
207.IP "perltidyrc" 4
208.IX Item "perltidyrc"
209If the \fBperltidyrc\fR file is given, it will be used instead of any
210\&\fI.perltidyrc\fR configuration file that would otherwise be used.
211.IP "argv" 4
212.IX Item "argv"
213If the \fBargv\fR parameter is given, it will be used instead of the
214\&\fB@ARGV\fR array. The \fBargv\fR parameter may be a string, a reference to a
215string, or a reference to an array. If it is a string or reference to a
216string, it will be parsed into an array of items just as if it were a
217command line string.
218.SH "EXAMPLE"
219.IX Header "EXAMPLE"
220The following example passes perltidy a snippet as a reference
221to a string and receives the result back in a reference to
222an array.
223.PP
224.Vb 1
225\& use Perl::Tidy;
226.Ve
227.PP
228.Vb 12
229\& # some messy source code to format
230\& my $source = <<'EOM';
231\& use strict;
232\& my @editors=('Emacs', 'Vi '); my $rand = rand();
233\& print "A poll of 10 random programmers gave these results:\en";
234\& foreach(0..10) {
235\& my $i=int ($rand+rand());
236\& print " $editors[$i] users are from Venus" . ", " .
237\& "$editors[1-$i] users are from Mars" .
238\& "\en";
239\& }
240\& EOM
241.Ve
242.PP
243.Vb 4
244\& # We'll pass it as ref to SCALAR and receive it in a ref to ARRAY
245\& my @dest;
246\& perltidy( source => \e$source, destination => \e@dest );
247\& foreach (@dest) {print}
248.Ve
249.SH "Using the \fBformatter\fP Callback Object"
250.IX Header "Using the formatter Callback Object"
251The \fBformatter\fR parameter is an optional callback object which allows
252the calling program to receive tokenized lines directly from perltidy for
253further specialized processing. When this parameter is used, the two
254formatting options which are built into perltidy (beautification or
255html) are ignored. The following diagram illustrates the logical flow:
256.PP
257.Vb 3
258\& |-- (normal route) -> code beautification
259\& caller->perltidy->|-- (-html flag ) -> create html
260\& |-- (formatter given)-> callback to write_line
261.Ve
262.PP
263This can be useful for processing perl scripts in some way. The
264parameter \f(CW$formatter\fR in the perltidy call,
265.PP
266.Vb 1
267\& formatter => $formatter,
268.Ve
269.PP
270is an object created by the caller with a \f(CW\*(C`write_line\*(C'\fR method which
271will accept and process tokenized lines, one line per call. Here is
272a simple example of a \f(CW\*(C`write_line\*(C'\fR which merely prints the line number,
273the line type (as determined by perltidy), and the text of the line:
274.PP
275.Vb 1
276\& sub write_line {
277.Ve
278.PP
279.Vb 8
280\& # This is called from perltidy line-by-line
281\& my $self = shift;
282\& my $line_of_tokens = shift;
283\& my $line_type = $line_of_tokens->{_line_type};
284\& my $input_line_number = $line_of_tokens->{_line_number};
285\& my $input_line = $line_of_tokens->{_line_text};
286\& print "$input_line_number:$line_type:$input_line";
287\& }
288.Ve
289.PP
290The complete program, \fBperllinetype\fR, is contained in the examples section of
291the source distribution. As this example shows, the callback method
292receives a parameter \fB$line_of_tokens\fR, which is a reference to a hash
293of other useful information. This example uses these hash entries:
294.PP
295.Vb 3
296\& $line_of_tokens->{_line_number} - the line number (1,2,...)
297\& $line_of_tokens->{_line_text} - the text of the line
298\& $line_of_tokens->{_line_type} - the type of the line, one of:
299.Ve
300.PP
301.Vb 14
302\& SYSTEM - system-specific code before hash-bang line
303\& CODE - line of perl code (including comments)
304\& POD_START - line starting pod, such as '=head'
305\& POD - pod documentation text
306\& POD_END - last line of pod section, '=cut'
307\& HERE - text of here-document
308\& HERE_END - last line of here-doc (target word)
309\& FORMAT - format section
310\& FORMAT_END - last line of format section, '.'
311\& DATA_START - __DATA__ line
312\& DATA - unidentified text following __DATA__
313\& END_START - __END__ line
314\& END - unidentified text following __END__
315\& ERROR - we are in big trouble, probably not a perl script
316.Ve
317.PP
318Most applications will be only interested in lines of type \fB\s-1CODE\s0\fR. For
319another example, let's write a program which checks for one of the
320so-called \fInaughty matching variables\fR \f(CW\*(C`&`\*(C'\fR, \f(CW$&\fR, and \f(CW$'\fR, which
321can slow down processing. Here is a \fBwrite_line\fR, from the example
322program \fBfind_naughty.pl\fR, which does that:
323.PP
324.Vb 1
325\& sub write_line {
326.Ve
327.PP
328.Vb 3
329\& # This is called back from perltidy line-by-line
330\& # We're looking for $`, $&, and $'
331\& my ( $self, $line_of_tokens ) = @_;
332.Ve
333.PP
334.Vb 7
335\& # pull out some stuff we might need
336\& my $line_type = $line_of_tokens->{_line_type};
337\& my $input_line_number = $line_of_tokens->{_line_number};
338\& my $input_line = $line_of_tokens->{_line_text};
339\& my $rtoken_type = $line_of_tokens->{_rtoken_type};
340\& my $rtokens = $line_of_tokens->{_rtokens};
341\& chomp $input_line;
342.Ve
343.PP
344.Vb 2
345\& # skip comments, pod, etc
346\& return if ( $line_type ne 'CODE' );
347.Ve
348.PP
349.Vb 2
350\& # loop over tokens looking for $`, $&, and $'
351\& for ( my $j = 0 ; $j < @$rtoken_type ; $j++ ) {
352.Ve
353.PP
354.Vb 2
355\& # we only want to examine token types 'i' (identifier)
356\& next unless $$rtoken_type[$j] eq 'i';
357.Ve
358.PP
359.Vb 2
360\& # pull out the actual token text
361\& my $token = $$rtokens[$j];
362.Ve
363.PP
364.Vb 7
365\& # and check it
366\& if ( $token =~ /^\e$[\e`\e&\e']$/ ) {
367\& print STDERR
368\& "$input_line_number: $token\en";
369\& }
370\& }
371\& }
372.Ve
373.PP
374This example pulls out these tokenization variables from the \f(CW$line_of_tokens\fR
375hash reference:
376.PP
377.Vb 2
378\& $rtoken_type = $line_of_tokens->{_rtoken_type};
379\& $rtokens = $line_of_tokens->{_rtokens};
380.Ve
381.PP
382The variable \f(CW$rtoken_type\fR is a reference to an array of token type codes,
383and \f(CW$rtokens\fR is a reference to a corresponding array of token text.
384These are obviously only defined for lines of type \fB\s-1CODE\s0\fR.
385Perltidy classifies tokens into types, and has a brief code for each type.
386You can get a complete list at any time by running perltidy from the
387command line with
388.PP
389.Vb 1
390\& perltidy --dump-token-types
391.Ve
392.PP
393In the present example, we are only looking for tokens of type \fBi\fR
394(identifiers), so the for loop skips past all other types. When an
395identifier is found, its actual text is checked to see if it is one
396being sought. If so, the above write_line prints the token and its
397line number.
398.PP
399The \fBformatter\fR feature is relatively new in perltidy, and further
400documentation needs to be written to complete its description. However,
401several example programs have been written and can be found in the
402\&\fBexamples\fR section of the source distribution. Probably the best way
403to get started is to find one of the examples which most closely matches
404your application and start modifying it.
405.PP
406For help with perltidy's pecular way of breaking lines into tokens, you
407might run, from the command line,
408.PP
409.Vb 1
410\& perltidy -D filename
411.Ve
412.PP
413where \fIfilename\fR is a short script of interest. This will produce
414\&\fIfilename.DEBUG\fR with interleaved lines of text and their token types.
415The \-D flag has been in perltidy from the beginning for this purpose.
416If you want to see the code which creates this file, it is
417\&\f(CW\*(C`write_debug_entry\*(C'\fR in Tidy.pm.
418.SH "EXPORT"
419.IX Header "EXPORT"
420.Vb 1
421\& &perltidy
422.Ve
423.SH "CREDITS"
424.IX Header "CREDITS"
425Thanks to Hugh Myers who developed the initial modular interface
426to perltidy.
427.SH "VERSION"
428.IX Header "VERSION"
429This man page documents Perl::Tidy version 20031021.
430.SH "AUTHOR"
431.IX Header "AUTHOR"
432.Vb 2
433\& Steve Hancock
434\& perltidy at users.sourceforge.net
435.Ve
436.SH "SEE ALSO"
437.IX Header "SEE ALSO"
438The \fIperltidy\fR\|(1) man page describes all of the features of perltidy. It
439can be found at http://perltidy.sourceforge.net.