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

Parse::Tokens - class for parsing text with embedded tokens

SYNOPSIS

  package MyParser;
  use base 'Parse::Tokens';

  MyParser->new->parse({
      text => q{Hi my name is <? $name ?>.},
      hash => {name=>'John Doe'},
      delimiters => [['<?','?>']],
  });

  # override SUPER::token
  sub token
  {
      my( $self, $token ) = @_;
      # $token->[0] - left bracket
      # $token->[1] - contents
      # $token->[2] - right bracket
      # do something with the token...
  }

  # override SUPER::token
  sub ether
  {
      my( $self, $text ) = @_;
      # do something with the text...
  }

DESCRIPTION Parse::Tokens provides a base class for parsing delimited strings from text blocks. Use Parse::Tokens as a base class for your own module or script. Very similar in style to HTML::Parser.

SETTER/GETTER FUNCTIONS

autoflush() Turn on autoflushing causing the template cash (not the text) to be purged before each call to parse();.
delimiters() Specify delimiters as an array reference pointing to the left and right delimiters. Returns array reference containing two array references of delimiters and escaped delimiters.
debug() Turn on debug mode. 1 is on, 0 is off.
ether_callback() Sets/gets the callback code reference for the 'ether' event.
flush() Flush the template cash.
loose_paring() Allow any combination of delimiters to match. Default is turned of requiring exactly specified pair matches only.
parse() Run the parser.
post_callback() Sets/gets the callback code reference for the 'post_parse' event.
pre_callback() Sets/gets the callback code reference for the 'pre_parse' event.
push_delimiters() Add a delimiter pair (array ref) to the list of delimiters.
new() Pass parameter as a hash reference. Options are: TEXT - a block of text; DELIMITERS - a array reference consisting of the left and right token delimiters (eg ['<?', '?>']); AUTOFLUSH - 0 or 1 (default). While these are all optional at initialization, both TEXT and DELIMITERS must be set prior to calling parse() or as parameters to parse().
text() Load text.
token_callback() Sets/gets the callback code reference for the 'token' event.

EVENT METHODS

ether() Event method that gets called when non-token text is encountered during parsing.
post_parse() Event method that gets called after parsing has completed.
pre_parse() Event method that gets called prior to parsing commencing.
token() Event method that gets called when a token is encountered during parsing.

CHANGES

0.25 - added support for callbacks. - improved debug messaging. - fixed bug in delimiter assignment. - rearranged distribution files. 0.24 - added sample script and sample data. 0.23 - fixed pseudo bug relation to regular expression 'o' option. - aliased 'add_delimiters' to 'push_delimiters'. - misc internal changes. 0.22 - add push_delimiters method for adding to the delimiter array. 0.21 - add pre_parse and post_parse methods; add minimal debug message support. 0.20 - add multi-token support.

AUTHOR

Steve McKay, steve@colgreen.com

COPYRIGHT

Copyright 2000-2001 by Steve McKay. All rights reserved.

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

perl(1).