1Text::FindIndent(3) User Contributed Perl Documentation Text::FindIndent(3)
2
3
4
6 Text::FindIndent - Heuristically determine the indent style
7
9 use Text::FindIndent;
10 my $indentation_type = Text::FindIndent->parse($text, skip_pod => 1);
11 if ($indentation_type =~ /^s(\d+)/) {
12 print "Indentation with $1 spaces\n";
13 }
14 elsif ($indentation_type =~ /^t(\d+)/) {
15 print "Indentation with tabs, a tab should indent by $1 characters\n";
16 }
17 elsif ($indentation_type =~ /^m(\d+)/) {
18 print "Indentation with $1 characters in tab/space mixed mode\n";
19 }
20 else {
21 print "Indentation style unknown\n";
22 }
23
25 This is a module that attempts to intuit the underlying indent "policy"
26 for a text file (most likely a source code file).
27
29 parse
30 The class method "parse" tries to determine the indentation style of
31 the given piece of text (which must start at a new line and can be
32 passed in either as a string or as a reference to a scalar containing
33 the string).
34
35 Returns a letter followed by a number. If the letter is "s", then the
36 text is most likely indented with spaces. The number indicates the
37 number of spaces used for indentation. A "t" indicates tabs. The number
38 after the "t" indicates the number characters each level of indentation
39 corresponds to. A "u" indicates that the indenation style could not be
40 determined. Finally, an "m" followed by a number means that this many
41 characters are used for each indentation level, but the indentation is
42 an arbitrary number of tabs followed by 0-7 spaces. This can happen if
43 your editor is stupid enough to do smart indentation/whitespace
44 compression. (I.e. replaces all indentations many tabs as possible but
45 leaves the rest as spaces.)
46
47 The function supports parsing of "vim" modelines. Those settings
48 override the heuristics. The modeline's options that are recognized are
49 "sts"/"softtabstob", "et"/"noet"/"expandtabs"/"noexpandtabs", and
50 "ts"/"tabstop".
51
52 Similarly, parsing of "emacs" Local Variables is somewhat supported.
53 "parse" use explicit settings to override the heuristics but uses style
54 settings only as a fallback. The following options are recognized:
55 "tab-width", "indent-tabs-mode", "c-basic-offset", and "style".
56
57 There is one named option that you can pass to "parse()": "skip_pod".
58 When set to true, any section of POD (see perlpod) will be ignored for
59 indentation finding. This is because verbatim paragraphs and examples
60 embedded in POD or quite often indented differently from normal Perl
61 code around the POD section. Defaults to false. Example:
62
63 my $mode = Text::FindIndent->parse(\$text, skip_pod => 1);
64
66 Bugs should be reported via the CPAN bug tracker at
67
68 http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-FindIndent
69 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-FindIndent>
70
71 For other issues, contact the author.
72
74 Adam Kennedy <adamk@cpan.org>, Steffen Mueller <smueller@cpan.org>
75
77 Copyright 2008-2009 Adam Kennedy,
78
79 Copyright 2008-2009 Steffen Mueller.
80
81 This program is free software; you can redistribute it and/or modify it
82 under the same terms as Perl itself.
83
84 The full text of the license can be found in the LICENSE file included
85 with this module.
86
87
88
89perl v5.12.0 2009-12-23 Text::FindIndent(3)