9 my $seen_hint=0x02000000;
11 sub syntax_error ($$) {
14 Carp::croak("syntax error on assertion filter '$expr' ($why)");
20 if (warnings::enabled('assertions')) {
28 my @tokens=split / \s*
37 # print STDERR "tokens: -", join('-',@tokens), "-\n";
43 next if (!defined $t or $t eq '');
52 and syntax_error $expr, 'consecutive operators';
57 and syntax_error $expr, 'consecutive operators';
63 syntax_error $expr, 'unbalanced parens';
65 syntax_error $expr, "key missing after operator '$op[0]'";
71 unless ($^H & $seen_hint) {
72 my_warn "assertion status '_' referenced but not previously defined";
74 $t=($^H & $hint) ? 1 : 0;
76 elsif ($t ne '0' and $t ne '1') {
77 # print STDERR "'$t' resolved as ";
78 $t=grep ({ $t=~$_ } @{^ASSERTING}) ? 1 : 0;
79 # print STDERR "$t\n";
83 syntax_error $expr, 'operator expected';
85 if ($op[0] eq 'start') {
88 elsif ($op[0] eq '||') {
98 @now==1 or syntax_error $expr, 'unbalanced parens';
99 defined $op[0] and syntax_error $expr, "expression ends on operator '$op[0]'";
106 # print STDERR "\@_=", join("|", @_), "\n";
108 @_=(scalar(caller)) unless @_;
109 foreach my $expr (@_) {
110 unless (calc_expr $expr) {
111 # print STDERR "assertions deactived";
117 # print STDERR "assertions actived";
118 $^H |= $hint|$seen_hint;
131 assertions - select assertions in blocks of code
135 sub assert (&) : assertion { &{$_[0]}() }
137 use assertions 'foo';
138 assert { print "asserting 'foo'\n" };
141 use assertions qw( foo bar );
142 assert { print "asserting 'foo' and 'bar'\n" };
146 use assertions qw( bar );
147 assert { print "asserting only 'bar'\n" };
151 use assertions ' _ && bar ';
152 assert { print "asserting 'foo' && 'bar'\n" };
155 assert { print "asserting 'foo' again\n" };
159 The C<assertions> pragma specifies the tags used to enable and disable
160 the execution of assertion subroutines.
162 An assertion subroutine is declared with the C<:assertion> attribute.
163 This subroutine is not normally executed : it's optimized away by perl
166 The C<assertion> pragma associates to its lexical scope one or several
167 assertion tags. Then, to activate the execution of the assertions
168 subroutines in this scope, these tags must be given to perl via the
169 B<-A> command-line option.
177 Salvador FandiE<ntilde>o, E<lt>sfandino@yahoo.comE<gt>
179 =head1 COPYRIGHT AND LICENSE
181 Copyright 2002 by Salvador FandiE<ntilde>o
183 This library is free software; you can redistribute it and/or modify
184 it under the same terms as Perl itself.
188 TODO : Some more docs are to be added about assertion expressions.