|
NAMEPPIx::QuoteLike - Parse Perl string literals and string-literal-like things.SYNOPSISuse PPIx::QuoteLike; my $str = PPIx::QuoteLike->new( q<"fu$bar"> ); say $str->interpolates() ? 'interpolates' : 'does not interpolate'; DESCRIPTIONThis Perl class parses Perl string literals and things that are reasonably like string literals. Its real reason for being is to find interpolated variables for Perl::Critic policies and similar code.The parse is fairly straightforward, and a little poking around with eg/pqldump should show how it normally goes. But there is at least one quote-like thing that probably needs some explanation. Indented Here DocumentsThese were introduced in Perl 5.25.7 (November 2016) but not recognized by this module until its version 0.015 (February 2021). The indentation is parsed as PPIx::QuoteLike::Token::Whitespace objects, provided it is at least one character wide, otherwise it is not represented in the parse. That is to say,<<~EOD How doth the little crocodile Improve his shining tail EOD will have the three indentations represented by whitespace objects and each line of the literal represented by its own string object, but <<~EOD How doth the little crocodile Improve his shining tail EOD will parse the same as the non-indented version, except for the addition of the token representing the '~'. PPI is ahead of this module, and recognized indented here documents as of its version 1.246 (May 2019). Unfortunately, as of version 1.270 the indent gets lost in the parse, so a "PPIx::QuoteLike" object initialized from such a PPI::Token::HereDoc will be seen as having an indentation of '' regardless of the actual indentation in the source. I believe this restriction will go away when <https://github.com/Perl-Critic/PPI/issues/251> is resolved. DEPRECATION NOTICEThe postderef argument to new() is being put through a deprecation cycle and retracted. After the retraction, postfix dereferences will always be recognized. This is the default behaviour now.Starting with version 0.012_01, the first use of this argument warned. With version 0.016_01, all uses will warn. With version 0.017_01 all uses will be fatal. With the first release after April 15 2022, all mention of this argument will be removed. INHERITANCE"PPIx::QuoteLike" is not descended from any other class."PPIx::QuoteLike" has no descendants. METHODSThis class supports the following public methods:newmy $str = PPIx::QuoteLike->new( $source, %arg ); This static method parses the argument, and returns a new object containing the parse. The $source argument can be either a scalar or an appropriate PPI::Element object. If the $source argument is a scalar, it is presumed to represent a quote-like literal of some sort, provided it begins like one. Otherwise this method will return nothing. The scalar representation of a here document is a multi-line string whose first line consists of the leading " << " and the start delimiter, and whose subsequent lines consist of the content of the here document and the end delimiter. Indented here documents were not supported by this class until version 0.015. "PPI" classes that can be handled are PPI::Token::Quote, PPI::Token::QuoteLike::Backtick, PPI::Token::QuoteLike::Command, PPI::Token::QuoteLike::Readline, and PPI::Token::HereDoc. Any other object will cause "new()" to return nothing. Additional optional arguments can be passed as name/value pairs. Supported arguments are:
All other arguments are unsupported and reserved to the author. childmy $kid = $str->child( 0 ); This method returns the child element whose index is given as the argument. Children do not include the type(), or the start() or finish() delimiters. Negative indices are valid, and given the usual Perl interpretation. childrenmy @kids = $str->children(); This method returns all child elements. Children do not include the type(), or the start() or finish() delimiters. column_numberThis method returns the column number of the first character in the element, or "undef" if that can not be determined.contentsay $str->content(); This method returns the content of the object. If the original argument was a valid Perl string, this should be the same as the originally-parsed string. delimiterssay $str->delimiters(); This method returns the delimiters of the object, as a string. This will be two characters unless the argument to new() was a here document, missing its end delimiter, or an invalid string. In the latter case the return might be anything. elementsmy @elem = $str->elements(); This method returns all elements of the object. This includes type(), start(), children(), and finish(), in that order. failuressay $str->failures(); This method returns the number of parse failures found. These are instances where the parser could not figure out what was going on, and should be the same as the number of PPIx::QuoteLike::Token::Unknown objects returned by elements(). findfor ( @{[ $str->find( $criteria ) || [] } ) { ... } This method finds and returns a reference to an array of all elements that meet the given criteria. If nothing is found, a false value is returned. The $criteria can be either the name of a PPIx::QuoteLike::Token class, or a code reference. In the latter case, the code is called for each element in elements(), with the element as the only argument. The element is included in the output if the code returns a true value. finishsay map { $_->content() } $str->finish(); This method returns the finishing elements of the parse. It is actually an array, with the first element being a PPIx::QuoteLike::Token::Delimiter. If the parse is of a here document there will be a second element, which will be a PPIx::QuoteLike::Token::Whitespace containing the trailing new line character. If called in list context you get the whole array. If called in scalar context you get the element whose index is given in the argument, or element zero if no argument is specified. handlessay PPIx::QuoteLike->handles( $string ) ? "We can handle $string" : "We can not handle $string"; This convenience static method returns a true value if this package can be expected to handle the content of $string (be it scalar or object), and a false value otherwise. indentationThis method returns the indentation string if the object represents an indented here document, or "undef" if it represents anything else, including an unindented here document.Note that if indented syntax is used but the here document is not in fact indented, this will return '', which evaluates to false. interpolatessay $str->interpolates() ? 'The string interpolates' : 'The string does not interpolate'; This method returns a true value if the parsed string interpolates, and a false value if it does not. This does not indicate whether any interpolation actually takes place, only whether the string is double-quotish or single-quotish. line_numberThis method returns the line number of the first character in the element, or "undef" if that can not be determined.locationThis method returns a reference to an array describing the position of the string, or "undef" if the location is unavailable.The array is compatible with the corresponding PPI::Element method. logical_filenameThis method returns the logical file name (taking "#line" directives into account) of the file containing first character in the element, or "undef" if that can not be determined.logical_line_numberThis method returns the logical line number (taking "#line" directives into account) of the first character in the element, or "undef" if that can not be determined.parentThis method returns nothing, since the invocant is only used at the top of the object hierarchy.perl_version_introducedThis method returns the maximum value of "perl_version_introduced" returned by any of its elements. In other words, it returns the minimum version of Perl under which this quote-like object is valid. If there are no elements, 5.000 is returned, since that is the minimum value of Perl supported by this package.perl_version_removedThis method returns the minimum defined value of "perl_version_removed" returned by any of the quote-like object's elements. In other words, it returns the lowest version of Perl in which this object is "not" valid. If there are no elements, or if no element has a defined "perl_version_removed", "undef" is returned.schildmy $skid = $str->schild( 0 ); This method returns the significant child elements whose index is given by the argument. Negative indices are interpreted in the usual way. schildrenmy @skids = $str->schildren(); This method returns the significant children. sourcemy $source = $str->source(); This method returns the $source argument to new(), whatever it was. startsay map { $_->content() } $str->start(); This method returns the starting elements of the parse. It is actually an array, with the first element being a PPIx::QuoteLike::Token::Delimiter. If the parse is of a here document there will be a second element, which will be a PPIx::QuoteLike::Token::Whitespace containing the trailing new line character. If called in list context you get the whole array. If called in scalar context you get the element whose index is given in the argument, or element zero if no argument is specified. statementThis method returns the PPI::Statement that contains this string, or nothing if the statement can not be determined.In general this method will return something only under the following conditions:
topThis method returns the top of the hierarchy -- in this case, the invocant.typemy $type = $str->type(); This method returns the type object. This will be a PPIx::QuoteLike::Token::Structure if the parse was successful; otherwise it might be "undef". Its contents will be everything up to the start delimiter, and will typically be 'q', 'qq', 'qx', '<<' (for here documents), or '' (for quoted strings). The type data are actually an array. If the second element is present it will be the white space (if any) separating the actual type from the value. If called in list context you get the whole array. If called in scalar context you get the element whose index is given in the argument, or element zero if no argument is specified. variablessay "Interpolates $_" for $str->variables(); NOTE that this method is discouraged, and may well be deprecated and removed. My problem with it is that it returns variable names rather than PPI::Element objects, leaving you no idea how the variables are used. It was originally written for the benefit of Perl::Critic::Policy::Variables::ProhibitUnusedVarsStricter, but has proven inadequate to that policy's needs. This convenience method returns all interpolated variables. Each is returned only once, and they are returned in no particular order. If the object does not represent a string that interpolates, nothing is returned. visual_column_numberThis method returns the visual column number (taking tabs into account) of the first character in the element, or "undef" if that can not be determined.RESTRICTIONSBy the nature of this module, it is never going to get everything right. Many of the known problem areas involve interpolations one way or another.Changes in SyntaxSometimes the introduction of new syntax changes the way a string is parsed. For example, the "\F" (fold case) case control was introduced in Perl 5.15.8. But it did not represent a syntax error prior to that version of Perl, it was simply parsed as "F". So$ perl -le 'print "Foo\FBar"' prints "FooFBar" under Perl 5.14.4, but "Foobar" under 5.16.0. "PPIx::QuoteLike" generally assumes the more modern parse in cases like this. Static ParsingIt is well known that Perl can not be statically parsed. That is, you can not completely parse a piece of Perl code without executing that same code.Nevertheless, this class is trying to statically parse quote-like things. I do not have any examples of where the parse of a quote-like thing would change based on what is interpolated, but neither can I rule it out. Caveat user. PPI RestrictionsAs of version 0.015 of this module, the only known instance of this is the handling of indented here documents, as discussed above under Indented Here Documents.Non-Standard SyntaxThere are modules out there that alter the syntax of Perl. If the syntax of a quote-like string is altered, this module has no way to understand that it has been altered, much less to adapt to the alteration. The following modules are known to cause problems:Acme::PerlML, which renders Perl as XML. "Data::PostfixDeref", which causes Perl to interpret suffixed empty brackets as dereferencing the thing they suffix. This module by Ben Morrow ("BMORROW") appears to have been retracted. Filter::Trigraph, which recognizes ANSI C trigraphs, allowing Perl to be written in the ISO 646 character set. Perl6::Pugs. Enough said. SUPPORTSupport is by the author. Please file bug reports at <https://rt.cpan.org/Public/Dist/Display.html?Name=PPIx-QuoteLike>, <https://github.com/trwyant/perl-PPIx-QuoteLike/issues>, or in electronic mail to the author.AUTHORThomas R. Wyant, III wyant at cpan dot orgCOPYRIGHT AND LICENSECopyright (C) 2016-2022 by Thomas R. Wyant, IIIThis program is free software; you can redistribute it and/or modify it under the same terms as Perl 5.10.0. For more details, see the full text of the licenses in the directory LICENSES. This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.
Visit the GSP FreeBSD Man Page Interface. |