9 my $seen_hint=0x02000000;
11 sub _syntax_error ($$) {
14 Carp::croak("syntax error on assertion filter '$expr' ($why)");
19 if (warnings::enabled('assertions')) {
27 my @tokens=split / \s*
36 # print STDERR "tokens: -", join('-',@tokens), "-\n";
42 next if (!defined $t or $t eq '');
51 and _syntax_error $expr, 'consecutive operators';
56 and _syntax_error $expr, 'consecutive operators';
62 _syntax_error $expr, 'unbalanced parens';
64 _syntax_error $expr, "key missing after operator '$op[0]'";
70 unless ($^H & $seen_hint) {
71 _carp "assertion status '_' referenced but not previously defined";
73 $t=($^H & $hint) ? 1 : 0;
75 elsif ($t ne '0' and $t ne '1') {
76 $t = ( grep { ref $_ eq 'Regexp'
79 } @{^ASSERTING} ) ? 1 : 0;
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;
123 and _carp($_[0]."->unimport arguments are being ignored");
137 return $^H & $hint ? 1 : 0;
149 return $^H & $seen_hint ? 1 : 0;
159 assertions - select assertions in blocks of code
163 sub assert (&) : assertion { &{$_[0]}() }
165 use assertions 'foo';
166 assert { print "asserting 'foo'\n" };
169 use assertions qw( foo bar );
170 assert { print "asserting 'foo' and 'bar'\n" };
174 use assertions qw( bar );
175 assert { print "asserting only 'bar'\n" };
179 use assertions '_ && bar';
180 assert { print "asserting 'foo' && 'bar'\n" };
183 assert { print "asserting 'foo' again\n" };
187 The C<assertions> pragma specifies the tags used to enable and disable
188 the execution of assertion subroutines.
190 An assertion subroutine is declared with the C<:assertion> attribute.
191 This subroutine is not normally executed: it's optimized away by perl
194 The C<assertions> pragma associates to its lexical scope one or
195 several assertion tags. Then, to activate the execution of the
196 assertions subroutines in this scope, these tags must be given to perl
197 via the B<-A> command-line option. For instance, if...
199 use assertions 'foobar';
201 is used, assertions on the same lexical scope will only be executed
202 when perl is called as...
204 perl -A=foobar script.pl
206 Regular expressions can also be used within the -A
207 switch. For instance...
209 perl -A='foo.*' script.pl
211 will activate assertions tagged as C<foo>, C<foobar>, C<foofoo>, etc.
213 =head2 Selecting assertions
215 Selecting which tags are required to activate assertions inside a
216 lexical scope, is done with the arguments passed on the C<use
217 assertions> sentence.
219 If no arguments are given, the package name is used as the assertion tag:
225 use assertions __PACKAGE__;
227 When several tags are given, all of them have to be activated via the
228 C<-A> switch to activate assertion execution on that lexical scope,
231 use assertions qw(Foo Bar);
233 Constants C<1> and C<0> can be used to force unconditional activation
234 or deactivation respectively:
239 Operators C<&&> and C<||> and parenthesis C<(...)> can be used to
240 construct logical expressions:
242 use assertions 'foo && bar';
243 use assertions 'foo || bar';
244 use assertions 'foo && (bar || doz)';
246 (note that the logical operators and the parens have to be included
247 inside the quoted string).
249 Finally, the special tag C<_> refers to the current assertion
252 use assertions 'foo';
253 use assertions '_ && bar;
257 use assertions 'foo && bar';
259 =head2 Handling assertions your own way
261 The C<assertions> module also provides a set of low level functions to
262 allow for custom assertion handling modules.
264 Those functions are not exported and have to be fully qualified with
265 the package name when called, for instance:
268 assertions::enabled(1);
270 (note that C<assertions> is loaded with the C<require> keyword
271 to avoid calling C<assertions::import()>).
273 Those functions have to be called at compile time (they are
280 activates or deactivates assertion execution. For instance:
282 package assertions::always;
285 sub import { assertions::enabled(1) }
289 This function calls C<assertion::seen(1)> also (see below).
293 returns a true value when assertion execution is active.
297 A warning is generated when an assertion subroutine is found before
298 any assertion selection code. This function is used to just tell perl
299 that assertion selection code has been seen and that the warning is
300 not required for the currently compiling lexical scope.
304 returns true if any assertion selection module (or code) has been
305 called before on the currently compiling lexical scope.
311 Support for assertions is only available in perl from version 5.9. On
312 previous perl versions this module will do nothing, though it will not
315 L<assertions::compat> provides an alternative way to use assertions
316 compatible with lower versions of perl.
321 L<perlrun>, L<assertions::activate>, L<assertions::compat>.
325 Salvador FandiE<ntilde>o, E<lt>sfandino@yahoo.comE<gt>
327 =head1 COPYRIGHT AND LICENSE
329 Copyright 2002, 2005 by Salvador FandiE<ntilde>o
331 This library is free software; you can redistribute it and/or modify
332 it under the same terms as Perl itself.