File Coverage

blib/lib/Math/Symbolic/Custom/Pattern/Export.pm
Criterion Covered Total %
statement 29 33 87.8
branch 5 14 35.7
condition 2 6 33.3
subroutine 9 9 100.0
pod 2 2 100.0
total 47 64 73.4


line stmt bran cond sub pod time code
1             package Math::Symbolic::Custom::Pattern::Export;
2              
3 1     1   24 use 5.006001;
  1         4  
  1         57  
4 1     1   7 use strict;
  1         1  
  1         35  
5 1     1   6 use warnings;
  1         2  
  1         42  
6 1     1   6 use Carp qw/cluck confess/;
  1         2  
  1         67  
7              
8 1     1   7 use Math::Symbolic qw/parse_from_string/;
  1         2  
  1         53  
9 1     1   6 use Math::Symbolic::Custom::Base;
  1         3  
  1         50  
10 1     1   549 BEGIN {*import = \&Math::Symbolic::Custom::Base::aggregate_import}
11              
12             our $VERSION = '2.01';
13              
14             =encoding utf8
15              
16             =head1 NAME
17              
18             Math::Symbolic::Custom::Pattern::Export - Export method to MS::Custom
19              
20             =head1 SYNOPSIS
21              
22             use Math::Symbolic::Custom::Pattern;
23            
24             # later:
25             my $pattern = $tree->to_pattern();
26             # and even later:
27             $another_tree->is_of_form($pattern);
28              
29             =head1 DESCRIPTION
30              
31             This module is an extension to the Math::Symbolic module. A basic
32             familiarity with that module is required.
33              
34             Please have a look at the Math::Symbolic::Custom::Pattern module first.
35             This is an internal module only. It manages to add two new methods to all
36             Math::Symbolic objects: C and C. It uses the
37             Math::Symbolic::Custom mechanism for that.
38              
39             =head2 EXPORT
40              
41             In a way, this module exports the C and C methods to
42             Math::Symbolic::Base. Please look at L.
43              
44             =cut
45              
46             our $Aggregate_Export = [qw/is_of_form to_pattern/];
47              
48             =head2 Math::Symbolic method is_of_form
49              
50             This method can be called on any Math::Symbolic tree. First argument must be
51             a pattern. Returns true if the pattern matches the tree and false if not.
52             As with the C method on Math::Symbolic::Custom::Pattern objects,
53             the true value returned reflects the way the pattern matched. Please see
54             L for details.
55              
56             The pattern may either be a Math::Symbolic::Custom::Pattern object (fastest)
57             or a Math::Symbolic tree representing a pattern (decent speed, since only the
58             pattern object needs to be constructed) or a string to be parsed as a
59             Math::Symbolic tree (very slow since the string has to be parsed).
60              
61             For details on patterns, please refer to the documentation of
62             Math::Symbolic::Custom::Pattern.
63              
64             This method always throws fatal errors since returning a boolean is used for
65             valid, non-error return values. Therefore, if you plan to pass unvalidated
66             objects or strings to be parsed, consider wrapping calls to this method in
67             C blocks. (Note that C is the safer brother of
68             the much despised C. See L.)
69              
70             =cut
71              
72             sub is_of_form {
73 66     66 1 37857 my $self = shift;
74 66         129 my $proto = shift;
75              
76             # argument checking
77 66 50       385 confess("is_of_form() must be called on Math::Symbolic tree.")
78             if not ref($self) =~ /^Math::Symbolic/;
79 66 50 33     537 confess("is_of_form() requires a Math::Symbolic tree, a string to be parsed as a tree, or a Math::Symbolic::Custom::Pattern as first argument.")
80             if ref($proto) and not ref($proto) =~ /^Math::Symbolic/;
81            
82             # parse as tree
83 66 50       221 if (not ref($proto)) {
84 0         0 $proto = parse_from_string($proto);
85 0 0       0 confess("First argument to is_of_form() was treated as a string. That string could not be parsed as a Math::Symbolic tree.") if not defined $proto;
86             }
87              
88 66 50 33     383 if (
89             not UNIVERSAL::isa($proto, 'Math::Symbolic::Custom::Pattern')
90             and ref($proto) =~ /^Math::Symbolic/
91             ) {
92 0         0 $proto = Math::Symbolic::Custom::Pattern->new($proto);
93 0 0       0 confess("Could not generate pattern from Math::Symbolic tree.")
94             if not defined $proto;
95             }
96            
97 66         233 return $proto->match($self);
98             }
99              
100             =head2 Math::Symbolic method to_pattern
101              
102             Generates a Math::Symbolic::Custom::Pattern object from the Math::Symbolic
103             tree C is called on. The pattern can be used with the
104             C method or like any other Math::Symbolic::Custom::Pattern
105             object. (See that package for details on patterns.)
106              
107             =cut
108              
109             sub to_pattern {
110 66     66 1 2343150 my $self = shift;
111             # argument checking
112 66 50       486 confess("to_pattern() must be called on Math::Symbolic tree.")
113             if not ref($self) =~ /^Math::Symbolic/;
114 66         442 return Math::Symbolic::Custom::Pattern->new($self);
115             }
116              
117             1;
118             __END__