String::Pattern - create / identify strings by pattern
use String::Pattern; use strict; my $code = "73545-ved-8877"; my $name = "Benalgin"; my $prescription = 'X'; my $price = 10.99; my $date; my $p = new Pattern 'code: 1223144-x-67 name: Aspirin prescription: [pr_flag] price: $5.90'; my $print_view = new Pattern q{ record date : 8/11/00 code: 1223144-x-67 name: Aspirin [pr_flag] prescription price: $5.90 }; $p->bind( '1223144-x-67' => \$code); $p->bind( Aspirin => \$name); $p->bind( '5.90' => \$price, '\d+(?:[,\.]\d{2})?'); $p->bind( pr_flag => \$prescription); $print_view->bind_like($p); $print_view->bind( '8/11/00' => \$date); my $q="$p"; # equivalent ot my $q=$p->to_string; $prescription=" "; my $r = 'code: 1223144x-69 name: Aspirin-Bayer prescription: [ ] price: $3.124'; for my $string ($r, $p, $q) { print $string, "\n"; if ($p->identify($string)) { print "Match on pattern \n"; $date = localtime; print $print_view."\n"; }else{ print "Not match on pattern \n\n" } }
This module is designed to deal whith non atomic data strings representations whith fixed number of items on it - such as addresses, business cards, product descriptors etc. It simplificies string creation based on preliminary defined human readable patterns ( templates ) and identifying that any given string has format, "described" in the same pattern.
if $string is supplied then it will be used as pattern, otherwise currently used pattern is returned
$string
binds var ref to specific part of pattern (tag). When string is created, all occuriences of that tag will be replaced with the value of $var. Regular expession $re is used to describe what the tag looks like, and increases the accuracy of identify method in ambiguous cases. Pattern is used to build one regular expession whith backreferences ( for each occurrence of tag ) , so brackets usage in $re must be only like this (?: ... ).
$var
$re
Creates string based on pattern and bindings. This method is invoked when object ref is evaluated in string context
Returns 1 if $string matches on pattern and all occurrences of given tag are equal, then sets vars to first occurrence of respective bounded tag. Otherwise returns 0 and nothig is changed.
Gets bindings from other object. May be useful for copy like operations avoiding boring declararions.
my $p1 = new String::Pattern .....; my $p2 = new String::Pattern .....; # a lot of bind ... $p1->bind....; ... $p1->bind....; $p2->bind_like($p1); # $p2 specific binds $p2->bind....; if ($p1->identify(...)) { print $p2 }
so $p2 have all of $p1 bindings plus one additional
Adding some useful warnings and erros about content of variables, and their ralations with tags in the pattern
Ivo Zdravkov, ivoz@starmail.com
perl (1), perlre (3)
To install Pattern, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Pattern
CPAN shell
perl -MCPAN -e shell install Pattern
For more information on module installation, please visit the detailed CPAN module installation guide.