The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

CodeGen::Cpppp::AntiCharacter - AntiCharacters combine with characters to produce nothing

SYNOPSIS

  my $anticomma= CodeGen::Cpppp::AntiCharacter->new(qr/,/);
  say '1,2,3,' . $anticomma;  # 1,2,3\n
  say '1,2,3' . $anticomma;   # 1,2,3\n
  
  my $antiword= CodeGen::Cpppp::AntiCharacter->new(qr/\w+/);
  say "apple,bananna" . $antiword; # apple,\n
  
  # skip over whitespace, but preserve the whitespace
  my $anticomma= CodeGen::Cpppp::AntiCharacter->new(qr/,/, qr/\s*/);
  say "1,2,\n  " . $anticomma;  # "1,2\n  \n"

DESCRIPTION

Anticharacter is an object that eliminates characters at the end of a string when it is concatenated to that string. It has an optional second parameter of characters to ignore while looking for the characters to remove.

If the concatenation does not remove all of the target characters, the concatenation returns another object which can continue the hunt.

Anticharacters do not work with join, only with regular concatenation. Stringifying an anticharacter ends the search and returns a simple string of anything that had accumulated during the search for the characters to remove.

CONSTRUCTOR

new

  $antichar= $class->new( $negate_regex, $skip_regex = undef );

Return a new AntiCharacter object. The $negate_regex is required, and should be the pattern you want to find on the end of a string as if you had written

    s/$negate_regex\Z//

The optional $skip_regex is a pattern to ignore, as if you had written

    s/$negate_regex($skip_regex)\Z/$1/

METHODS

concat

This is the method invoked when an AntiCharacter is concatenated with anything else.

  $maybe_antichar= $antichar->concat( $other_thing, $reverse );

If reverse is false, meaning $other_thing is being appended to the antichar, the result is a new AntiCharacter with that string as an additional suffix.

If reverse is true, meaning $other_thing is a prefix of the AntiCharacter, this checks to see if the $negate_regex can be applied and does not reach the start of the string, meaning it has negated exactly what it was supposed to. In that case it returns a plain string. If the entire string would be consumed, it returns another anticharacter in case further concatenations find a larger match to remove. If the negate pattern does not match, this returns a plain string assuming the anticharacter will never match.

AUTHOR

Michael Conrad <mike@nrdvana.net>

VERSION

version 0.003

COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by Michael Conrad.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.