Syntax::Highlight::Perl::Improved(3pm) | User Contributed Perl Documentation | Syntax::Highlight::Perl::Improved(3pm) |
Syntax::Highlight::Perl::Improved - Highlighting of Perl Syntactical Structures
This file documents Syntax::Highlight::Perl::Improved version 1.0.
# simple procedural use Syntax::Highlight::Perl::Improved ':BASIC'; # or ':FULL' print format_string($my_string); # OO use Syntax::Highlight::Perl::Improved; my $formatter = new Syntax::Highlight::Perl::Improved; print $formatter->format_string($my_string);
This module provides syntax highlighting for Perl code. The design bias is roughly line-oriented and streamed (ie, processing a file line-by-line in a single pass). Provisions may be made in the future for tasks related to "back-tracking" (ie, re-doing a single line in the middle of a stream) such as speeding up state copying.
The only constructor provided is "new()". When called on an existing object, "new()" will create a new copy of that object. Otherwise, "new()" creates a new copy of the (internal) Default Object. Note that the use of the procedural syntax modifies the Default Object and that those changes will be reflected in any subsequent "new()" calls.
Formatting is done using the "format_string()" method. Call "format_string()" with one or more strings to format, or it will default to using $_.
You can set the text used for formatting a syntax element using "set_format()" (or set the start and end format individually using "set_start_format()" and "set_end_format()", respectively).
You can also retrieve the text used for formatting for an element via "get_start_format()" or "get_end_format". Bulk retrieval of the names or values of defined formats is possible via "get_format_names_list()" (names), "get_start_format_values_list()" and "get_end_format_values_list()".
See "FORMAT TYPES" later in this document for information on what format elements can be used.
You can check certain aspects of the state of the formatter via the methods: "in_heredoc()", "in_string()", "in_pod()", "was_pod()", "in_data()", and "line_count()".
You can reset all of the above states (and a few other internal ones) using "reset()".
You can set or check the stability of formatting via "unstable()".
In unstable (TRUE) mode, formatting is not considered to be persistent with nested formats. Or, put another way, when unstable, the formatter can only "remember" one format at a time and must reinstate formatting for each token. An example of unstable formatting is using ANSI color escape sequences in a terminal.
In stable (FALSE) mode (the default), formatting is considered persistent within arbitrarily nested formats. Even in stable mode, however, formatting is never allowed to span multiple lines; it is always fully closed at the end of the line and reinstated at the beginning of a new line, if necessary. This is to ensure properly balanced tags when only formatting a partial code snippet. An example of stable formatting is HTML.
Using "define_substitution()", you can have the formatter substitute certain strings with others, after the original string has been parsed (but before formatting is applied). This is useful for escaping characters special to the output mode (eg, > and < in HTML) without them affecting the way the code is parsed.
You can retrieve the current substitutions (as a hash-ref) via "substitutions()".
The Syntax::Highlight::Perl::Improved formatter recognizes and differentiates between many Perl syntactical elements. Each type of syntactical element has a Format Type associated with it. There is also a 'DEFAULT' type that is applied to any element who's Format Type does not have a value.
Several of the Format Types have underscores in their name. This underscore is special, and indicates that the Format Type can be "generalized." This means that you can assign a value to just the first part of the Format Type name (the part before the underscore) and that value will be applied to all Format Types with the same first part. For example, the Format Types for all types of variables begin with "Variable_". Thus, if you assign a value to the Format Type "Variable", it will be applied to any type of variable. Generalized Format Types take precedence over non-generalized Format Types. So the value assigned to "Variable" would be applied to "Variable_Scalar", even if "Variable_Scalar" had a value explicitly assigned to it.
You can also define a "short-cut" name for each Format Type that can be generalized. The short-cut name would be the part of the Format Type name after the underscore. For example, the short-cut for "Variable_Scalar" would be "Scalar". Short-cut names have the least precedence and are only assigned if neither the generalized Type name, nor the full Type name have values.
Following is a list of all the syntactical elements that Syntax::Highlight::Perl::Improved currently recognizes, along with a short description of what each would be applied to.
Note that (theoretically) this format is not applied to non-scalar variables that are being used as scalars (ie: array or hash lookups, nor references to anything other than scalars). Syntax::Highlight::Perl::Improved figures out (or at least tries to) the actual type of the variable being used (by looking at how you're subscripting it) and formats it accordingly. The first character of the variable (ie, the "$", "@", "%", or "*") tells you the type of value being used, and the color (hopefully) tells you the type of variable being used to get that value.
(See "KNOWN ISSUES" for information about when this doesn't work quite right.)
Only occurs within strings or regular expressions.
Note that Perl does not make any distinction between keywords and built-in functions (at least not in the documentation). Thus I had to make a subjective call as to what would be considered keywords and what would be built-in functions.
The list of keywords can be found (and overloaded) in the variable $Syntax::Highlight::Perl::Improved::keyword_list_re as a pre-compiled regular expression.
The list of built-in functions can be found (and overloaded) in the variable $Syntax::Highlight::Perl::Improved::builtin_list_re as a pre-compiled regular expression.
The list of built-in functions can be found (and overloaded) in the variable $Syntax::Highlight::Perl::Improved::builtin_list_re as a pre-compiled regular expression.
The list of operators can be found (and overloaded) in the variable $Syntax::Highlight::Perl::Improved::operator_list_re as a pre-compiled regular expression.
Note that this does not apply to the package portion of a fully qualified variable name.
Syntax::Highlight::Perl::Improved uses OO method-calls internally (and actually defines a Default Object that is used when the functions are invoked procedurally) so you will not gain anything (efficiency-wise) by using the procedural interface. It is just a matter of style.
It is actually recommended that you use the OO interface, as this allows you to instantiate multiple, concurrent-yet-separate formatters. Though I cannot think of why you would need multiple formatters instantiated. :-)
One point to note: the "new()" method uses the Default Object to initialize new objects. This means that any changes to the state of the Default Object (including Format definitions) made by using the procedural interface will be reflected in any subsequently created objects. This can be useful in some cases (eg, call "set_format()" procedurally just before creating a batch of new objects to define default Formats for them all) but will most likely lead to trouble.
Note that this does not reset any user options (including formats and format stability).
If called with a non-zero number, puts the formatter into unstable formatting mode.
In unstable mode, it is assumed that formatting is not persistent one token to the next and that each token must be explicitly formatted.
If the first parameter is a reference to a hash, the formatter will replace it's own hash with the given one, and subsequent changes to the hash outside the formatter will be reflected.
Otherwise, it will copy the arguments passed into it's own hash, and any substitutions already defined (but not in the parameter list) will be preserved. (ie, the new substitutions will be added, without destroying what was there already.)
You should pass a list of keys/values where the keys are the format names and the values are references to arrays containing the starting and ending formatting strings (in that order) for that format.
The names are looked for in the following order:
First: Prefer the names joined by underscore, from most general to least. For example, given ("Variable", "Scalar"): "Variable" then "Variable_Scalar".
Second: Then try each name singly, in reverse order. For example, "Scalar" then "Variable".
See "FORMAT TYPES" for more information.
Note: The end of the string is considered to be the end of a line, regardless of whether or not there is a trailing line-break (but trailing line-breaks will not cause an extra, empty line).
Another Note: The function actually uses $/ to determine line-breaks, unless $/ is set to "\n" (newline). If $/ is "\n", then it looks for the first match of "m/\r?\n|\n?\r/" in the string and uses that to determine line-breaks. This is to make it easy to handle non-unix text. Whatever characters it ends up using as line-breaks are preserved.
No syntax checking is done on TOKEN but substitutions defined with "define_substitution()" are performed.
Bug reports are always welcome. Email me at b<davidcyl@cpan.org>.
David C.Y. Liu b<davidcyl@cpan.org>
based on code by Cory Johns darkness@yossman.net
Copyright (c) 2004 David C.Y. Liu. This library is free software; you can redistribute and/or modify it under the same conditions as Perl itself.
Note: This is Cory John's todo list, not mine. Currently none of these features are planned for the near future.
2022-11-19 | perl v5.36.0 |