PPIx::QuoteLike(3pm)

πŸ’– PPIx::QuoteLike(3pm) β€” string-literal parser supremacy πŸ’–

okay so bestie, this is the PPIx::QuoteLike(3pm) page from User Contributed Perl Documentation, and we're starting with the first part of the group chat. It's literally a Perl class for parsing Perl string literals and things that are reasonably like string literals, and it has main character energy for exactly that reason.

✨ NAME ✨

  • PPIx::QuoteLike β€” parses Perl string literals and string-literal-like things. That's the whole assignment, no cap.

πŸ¦‹ SYNOPSIS πŸ¦‹

        use PPIx::QuoteLike;

        my $str = PPIx::QuoteLike->new( q<"fu$bar"> );
        say $str->interpolates() ?
           'interpolates' :
           'does not interpolate';

So you build a PPIx::QuoteLike object with q<"fu$bar">, then ask interpolates(), and it tells you either interpolates or does not interpolate. Lowkey elegant, highkey useful. πŸ’…

🌸 DESCRIPTION 🌸

This 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 Documents πŸŽ€

These 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 NOTICE 🚨

The postderef argument to new() is being put through a deprecation cycle and retracted. After the retraction, postfix dereferences will always be recognized.

Starting with version 0.012_01, the first use of this argument warned. With version 0.016_01, all uses warn. With version 0.017_01 all uses are fatal. With 0.0.021_01, all mention of this argument is removed, except of course for this notice.

πŸ‘‘ INHERITANCE πŸ‘‘

PPIx::QuoteLike is not descended from any other class.

PPIx::QuoteLike has no descendants. periodt.

✨ METHODS ✨

Okay bestie, this is the good part β€” all the things you can actually call on these objects. This class supports the following public methods:


new

my $str = PPIx::QuoteLike->new( $source, %arg );

This static method parses the argument and hands you back a shiny 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's presumed to represent a quote-like literal of some sort β€” provided it begins like one. Otherwise this method will return nothing (rude, but fair). 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:

encoding

This is the encoding of the $source. If this is specified as something other than undef, the $source will be decoded before processing.

If the $source is a PPI::Element, this encoding is used only if the document that contains the element has neither a byte order mark nor 'use utf8'.

index_locations

This Boolean argument determines whether the locations of the tokens should be computed. It defaults to true if the $source argument is a PPI::Element or if the location argument was provided, and false otherwise.

location

This argument is a reference to an array compatible with that returned by the PPI::Element location() method. It defaults to the location of the $source argument if that was a PPI::Element, otherwise no locations will be available.

trace

This Boolean argument causes a trace of the parse to be written to standard out. Setting this to a true value is unsupported in the sense that the author makes no representation as to what will happen if you do it, and reserves the right to make changes to the functionality, or retract it completely, without notice.

⚠️ Yeah so… trace is giving "abandon hope all ye who enter" energy. It's explicitly unsupported, the author won't promise you anything, and it could vanish without warning. You've been warned, bestie. πŸ’€

All other arguments are unsupported and reserved to the author. Don't come for me, I don't make the rules. πŸ™ˆ


child

my $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 get the usual Perl interpretation. πŸ”₯


children

my @kids = $str->children();

This method returns all child elements. Children do not include the type(), or the start() or finish() delimiters.


column_number

This method returns the column number of the first character in the element, or undef if that can not be determined.


content

say $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. Iconic behaviour honestly. ✨


delimiters

say $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. πŸŒ€


elements

my @elem = $str->elements();

This method returns all elements of the object. This includes type(), start(), children(), and finish(), in that order. The whole squad, in formation. πŸ‘‘


failures

say $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(). 😭 Not the flex we wanted.


find

for ( @{[ $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. A little filter, a little vibe check, we love to see it. πŸ¦‹


finish

say 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. Context sensitivity, we stan. πŸŽ€


handles

say 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. It's the vibe check before the vibe check. 🌸


indentation

This 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. So like… an empty string that's technically truthy-adjacent but nope, falsey. Check with defined if you care, bestie.

interpolates

say $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. It's giving "potential, not promise". πŸ’œ


line_number

This method returns the line number of the first character in the element, or undef if that can not be determined.


location

This 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_filename

This 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_number

This 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.


parent

This method returns nothing, since the invocant is only used at the top of the object hierarchy. No parents here, we're the main character. πŸ’…


perl_version_introduced

This 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_removed

This 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.


schild

my $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. ✨ Significant, because apparently some children are just background characters.


schildren

my @skids = $str->schildren();

This method returns the significant children. That's it, that's the whole method.


source

my $source = $str->source();

This method returns the $source argument to new(), whatever it was. No judgment, no transformation. 🫢


start

say 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.


statement

This 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:

  • The string is contained in a PPIx::QuoteLike object;
  • That object was initialized from a PPI::Element;
  • The PPI::Element is contained in a statement.

So like, three little hoops to jump through. No cap. πŸ¦‹


top

This method returns the top of the hierarchy β€” in this case, the invocant. You are the moment. Come on now. πŸ‘‘


type

my $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.


variables

say "Interpolates $_" for $str->variables();
πŸ’€ NOTE that this method is discouraged, and may well be deprecated and removed. Girl, it's on thin ice.

I have two problems with it. The first is that it returns variable names rather than PPI::Element objects, leaving you no idea how the variables are used. The second is that it does not properly handle things like ${^CAPTURE[0]}, and it seems infeasible to make it do so. It was originally written for the benefit of Perl::Critic::Policy::Variables::ProhibitUnusedVarsStricter, but has proven inadequate to that policy's needs. 😭 Not the glow-up we hoped for.

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_number

This 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. Tabs get respect here, as they should. 🌈

πŸ’€ RESTRICTIONS πŸ’€

Okay bestie, real talk: by the very nature of this module, it is never going to get everything right. That's just the vibe. And lowkey, a lot of the known problem areas involve interpolations, one way or another. 😭

🌸 Changes in Syntax 🌸

Sometimes new syntax drops and it literally changes how a string gets parsed. Iconic example: the \F (fold case) case control was introduced in Perl 5.15.8. But before that Perl version, it wasn't a syntax error or anything dramatic β€” it was just parsed as F. So check this out:

$ perl -le 'print "Foo\FBar"'

Under Perl 5.14.4 that prints FooFBar, but under 5.16.0 it prints Foobar. Same code, different era, different energy. ✨ PPIx::QuoteLike generally assumes the more modern parse in cases like this.

πŸ¦‹ Static Parsing πŸ¦‹

It's well known that Perl can not be statically parsed. Like, at all. You can not completely parse a piece of Perl code without executing that same code. That's just how Perl is. πŸ’…

Nevertheless, this class is trying to statically parse quote-like things. The author does not have any examples of where the parse of a quote-like thing would change based on what is interpolated, but neither can he rule it out. Caveat user. πŸ™ˆ

πŸŽ€ PPI Restrictions πŸŽ€

As 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. Noted and filed, bestie. πŸ“

⭐ Non-Standard Syntax ⭐

There 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. So basically, it's giving "I can't even." 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. πŸ’€

🫢 SUPPORT 🫢

Support 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. He literally wants to hear from you. πŸ‘‘

πŸ‘‘ AUTHOR πŸ‘‘

Thomas R. Wyant, III β€” wyant at cpan dot org

Copyright (C) 2016-2022 by Thomas R. Wyant, III

This 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. (Yeah, all caps, it's that serious. No cap. 🫑)


Okay that's the whole page, bestie β€” you made it to the end, so proud of you. πŸ₯Ή This one's signed off as perl v5.42.0, dated 2022-09-17, and it's the PPIx::QuoteLike(3pm) page. Now go parse something quote-like. Slay. πŸ’–βœ¨

πŸ“¦ where this page comes from