Softpedia
 


LINUX CATEGORIES:



GLOBAL PAGES >>
NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
WEEK'S BEST
  • Linux Kernel 3.9.6 / 3....
  • Linux Kernel 3.0.82 LTS...
  • KDE Software Compilatio...
  • PulseAudio 4.0
  • Wireshark 1.10.0
  • NetworkManager 0.9.8.2
  • LibreOffice 3.6.6 / 4.0...
  • SystemRescueCd 3.7.0
  • Linux Kernel 3.10 RC6
  • Ubuntu Tweak 0.8.5
  • Home > Linux > Programming > Libraries

    Array::PatternMatcher 0.04

    Download button

    No screenshots available
    Downloads: 386  View global page NEW!  Tell us about an update
    User Rating:
    Rated by:
    NOT RATED
    0 user(s)
    Developer:

    License / Price:

    Last Updated:

    Category:
    Terrence M. Brannon | More programs
    Perl Artistic License / FREE
    July 12th, 2007, 12:05 GMT
    ROOT / Programming / Libraries

     Read user reviews (0)  Refer to a friend  Subscribe

    Array::PatternMatcher description

    Array::PatternMatcher is a pattern matching for arrays.

    Array::PatternMatcher is a pattern matching for arrays.

    SYNOPSIS

    This section inlines the entire test suite. Please excuse the ok()s.

    use Array::PatternMatcher;

    Matching logical variables to input stream

    # 1 - simple match of logical variable to input
    my $pattern = 'AGE' ;
    my $input = 969 ;
    my $result = pat_match ($pattern, $input, {} ) ;
    ok($result->{AGE}, 969) ;

    # 2 - if binding exists, it must equal the input
    $input = 12;
    my $new_result = pat_match ($pattern, $input, $result) ;
    ok(!defined($new_result)) ;

    # 3 - bind the pattern logical variables to the input list

    $pattern = [qw(X Y)] ;
    $input = [ 77, 45 ] ;
    my $result = pat_match ($pattern, $input, {} ) ;
    ok($result->{X}, 77) ;
    Matching segments (quantifying) portions of the input stream
    # 1
    {
    my $pattern = ['a', [qw(X *)], 'd'] ;
    my $input = ['a', 'b', 'c', 'd'] ;

    my $result = pat_match ($pattern, $input, {} ) ;
    ok ("@{$result->{X}}","b c") ;
    }

    # 2
    {

    my $pattern = ['a', [qw(X *)], [qw(Y *)], 'd'] ;
    my $input = ['a', 'b', 'c', 'd'] ;
    my $result = pat_match ($pattern, $input, {} ) ;
    ok ("@{$result->{Y}}","b c") ;

    }
    # 3
    {
    my $pattern = ['a', [qw(X +)], 'd'] ;
    my $input = ['a', 'b', 'c', 'd'] ;
    ok ("@{$result->{X}}","b c") ;
    }
    # 4
    {
    my $pattern = [ 'a', [qw(X ?)], 'c' ] ;
    my $input = [ 'a', 'b', 'c' ] ;
    my $result = pat_match ($pattern, $input, {} ) ;
    ok ("$result->{X}","b") ;
    }
    # 5
    {
    my $pattern = [ qw(X OP Y is Z),
    [
    sub { "($_->{X} $_->{OP} $_->{Y}) == $_->{Z}" },
    'IF?'
    ]
    ] ;
    my $input = [qw(3 + 4 is 7) ] ;
    my $result = pat_match ($pattern, $input, {} ) ;
    ok ($result) ;
    }
    Single-matching:
    Take a single input and a series of patterns and decide which pattern
    matches the input:

    # 1 - Here all input patterns must match the input

    {
    my @pattern ;
    push @pattern, [ qw(X Y) ] ;
    push @pattern, [ qw(22 Z ) ] ;
    push @pattern, [ qw(M 33) ] ;

    my $input = [ qw(22 33) ] ;

    my $meta_pattern = [ 'AND?', @pattern ] ;

    # if no bindings, add a binding between pattern and input
    my $result = pat_match ($meta_pattern, $input, {} ) ;
    ok ($result->{Z},33) ;
    }

    # 2 - Here, any one of the patterns must match the input

    {
    my @pattern ;
    push @pattern, [ qw(99 22) ] ;
    push @pattern, [ qw(33 22) ] ;
    push @pattern, [ qw(44 3) ] ;
    push @pattern, [ qw(22 Z) ] ;

    my $input = [ qw(22 33) ] ;

    my $meta_pattern = [ 'OR?', @pattern ] ;

    # if no bindings, add a binding between pattern and input
    my $result = pat_match ($meta_pattern, $input, {} ) ;
    ok ($result->{Z},33) ;
    }

    # 3 - Here, none of the patterns must match the input

    {
    my @pattern ;
    push @pattern, [ qw(99 22) ] ;
    push @pattern, [ qw(33 22) ] ;
    push @pattern, [ qw(44 3) ] ;
    push @pattern, [ qw(22 Z) ] ;

    my $input = [ qw(22 33) ] ;

    my $meta_pattern = [ 'NOT?', @pattern ] ;

    # if no bindings, add a binding between pattern and input
    my $result = pat_match ($meta_pattern, $input, {} ) ;
    ok (scalar keys %$result == 0) ;
    }

    # 4 - here the input must satisfy the predicate
    {
    sub numberp { $_[0] =~ /d+/ }

    my $pattern = [ qw(X age), [qw(IS? N), &numberp] ] ;
    my $input = [ qw(Mary age), 'thirty-four' ] ;

    # if no bindings, add a binding between pattern and input
    my $result = pat_match ($pattern, $input, {} ) ;
    ok (!defined($result));
    }

    # 5 - same thing, but this time a failing result --- ''
    # not undef because it is the return val of numberp
    {
    sub numberp { $_[0] =~ /d+/ }

    my $pattern = [ qw(X age), [qw(IS? N), &numberp] ] ;
    my $input = [ qw(Mary age), 34 ] ;
    my $result = pat_match ($pattern, $input, {} ) ;

    ok ($result->{N},34) ;
    }
    Segment-matching:
    Match a chunk of the input stream using *, +, ?

    # 1 - * is greedy in this case, but not with 2 consecutve * patterns
    {
    my $pattern = ['a', [qw(X *)], 'd'] ;
    my $input = ['a', 'b', 'c', 'd'] ;

    # if no bindings, add a binding between pattern and input
    my $result = pat_match ($pattern, $input, {} ) ;
    warn sprintf "X*RETVAL: %s", Data::Dumper::Dumper($result) ;
    ok ("@{$result->{X}}","b c") ;
    }
    # 2 - X* gets nothing, Y* gets all it can:
    {

    my $pattern = ['a', [qw(X *)], [qw(Y *)], 'd'] ;
    my $input = ['a', 'b', 'c', 'd'] ;

    # if no bindings, add a binding between pattern and input
    my $result = pat_match ($pattern, $input, {} ) ;
    warn sprintf "X*Y*RETVAL: %s", Data::Dumper::Dumper($result) ;
    ok ("@{$result->{Y}}","b c") ;

    }
    # 3 - samething , but require at least one match for X
    {
    my $pattern = ['a', [qw(X +)], 'd'] ;
    my $input = ['a', 'b', 'c', 'd'] ;

    my $result = pat_match ($pattern, $input, {} ) ;
    warn sprintf "RETVAL: @{$result->{X}}" ;
    ok ("@{$result->{X}}","b c") ;
    }
    # 4 - require 0 or 1 match for X
    {
    my $pattern = [ 'a', [qw(X ?)], 'c' ] ;
    my $input = [ 'a', 'b', 'c' ] ;


    my $result = pat_match ($pattern, $input, {} ) ;

    ok ("$result->{X}","b") ;
    }
    # 5 - evaluate a sub on the fly after match
    {
    my $pattern = [ qw(X OP Y is Z),
    [
    sub { "($_->{X} $_->{OP} $_->{Y}) == $_->{Z}" },
    'IF?'
    ]
    ] ;
    my $input = [qw(3 + 4 is 7) ] ;

    my $result = pat_match ($pattern, $input, {} ) ;

    ok ($result) ;
    }
    # --- 6 same thing, but fail
    {
    my $pattern = [ qw(X OP Y is Z),
    [
    sub { "($_->{X} $_->{OP} $_->{Y}) == $_->{Z}" },
    'IF?'
    ]
    ] ;
    my $input = [qw(3 + 4 is 8) ] ;

    my $result = pat_match ($pattern, $input, {} ) ;
    warn sprintf "IF_RETVAL2: *%s*", Data::Dumper::Dumper($result);
    ok ($result eq '') ;
    }

    Requirements:

    · Perl



    Product's homepage

    Requirements:

    · Perl

      


    TAGS:

    pattern matching | arrays patterns | Perl module | Array::PatternMatche | arrays | pattern

    Go to top

    WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

    SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM