ensure that utf8 Perl code magically called from a regex localizes $.
[p5sagit/p5-mst-13.2.git] / lib / assertions.pm
index 918808d..700abf4 100644 (file)
@@ -6,6 +6,7 @@ our $VERSION = '0.01';
 # use warnings;
 
 my $hint=0x01000000;
+my $seen_hint=0x02000000;
 
 sub syntax_error ($$) {
     my ($expr, $why)=@_;
@@ -13,6 +14,15 @@ sub syntax_error ($$) {
     Carp::croak("syntax error on assertion filter '$expr' ($why)");
 }
 
+sub my_warn ($) {
+    my $error=shift;
+    require warnings;
+    if (warnings::enabled('assertions')) {
+       require Carp;
+       Carp::carp($error);
+    }
+}
+
 sub calc_expr {
     my $expr=shift;
     my @tokens=split / \s*
@@ -30,6 +40,8 @@ sub calc_expr {
     my @op='start';
 
     for my $t (@tokens) {
+       next if (!defined $t or $t eq '');
+
        if ($t eq '(') {
            unshift @now, 1;
            unshift @op, 'start';
@@ -45,9 +57,6 @@ sub calc_expr {
                    and syntax_error $expr, 'consecutive operators';
                $op[0]='&&';
            }
-           elsif (!defined $t or $t eq '') {
-               # warn "empty token";
-           }
            else {
                if ($t eq ')') {
                    @now==1 and
@@ -59,6 +68,9 @@ sub calc_expr {
                    shift @op;
                }
                elsif ($t eq '_') {
+                   unless ($^H & $seen_hint) {
+                       my_warn "assertion status '_' referenced but not previously defined";
+                   }
                    $t=($^H & $hint) ? 1 : 0;
                }
                elsif ($t ne '0' and $t ne '1') {
@@ -98,16 +110,14 @@ sub import {
        unless (calc_expr $expr) {
            # print STDERR "assertions deactived";
            $^H &= ~$hint;
+           $^H |= $seen_hint;
            return;
        }
     }
     # print STDERR "assertions actived";
-    $^H |= $hint;
+    $^H |= $hint|$seen_hint;
 }
 
-
-
-
 sub unimport {
     $^H &= ~$hint;
 }
@@ -118,7 +128,7 @@ __END__
 
 =head1 NAME
 
-assertions - selects assertions
+assertions - select assertions in blocks of code
 
 =head1 SYNOPSIS
 
@@ -129,12 +139,12 @@ assertions - selects assertions
 
   {
       use assertions qw( foo bar );
-      assert { print "asserting 'foo' & 'bar'\n" };
+      assert { print "asserting 'foo' and 'bar'\n" };
   }
 
   {
       use assertions qw( bar );
-      assert { print "asserting 'bar'\n" };
+      assert { print "asserting only 'bar'\n" };
   }
 
   {
@@ -144,24 +154,23 @@ assertions - selects assertions
 
   assert { print "asserting 'foo' again\n" };
 
-
-=head1 ABSTRACT
-
-C<assertions> pragma selects the tags used to control assertion
-execution.
-
 =head1 DESCRIPTION
 
+The C<assertions> pragma specifies the tags used to enable and disable
+the execution of assertion subroutines.
 
+An assertion subroutine is declared with the C<:assertion> attribute.
+This subroutine is not normally executed : it's optimized away by perl
+at compile-time.
 
-
-=head2 EXPORT
-
-None by default.
+The C<assertion> pragma associates to its lexical scope one or several
+assertion tags. Then, to activate the execution of the assertions
+subroutines in this scope, these tags must be given to perl via the
+B<-A> command-line option.
 
 =head1 SEE ALSO
 
-
+L<perlrun>.
 
 =head1 AUTHOR
 
@@ -175,3 +184,5 @@ This library is free software; you can redistribute it and/or modify
 it under the same terms as Perl itself.
 
 =cut
+
+TODO : Some more docs are to be added about assertion expressions.