source: trunk/core/lib/Assert.pm @ 1340

Revision 1340, 1.9 KB checked in by OlivierRaginel, 4 years ago (diff)

Item27: Fix svn properties on all .pm, .tmpl and .txt files

  • Property svn:keywords set to Revision Date
Line 
1package Assert;
2use base 'Exporter';
3require 5.006;
4
5# Derived from Carp::Assert
6# Copyright 2004 Crawford Currie
7# Copyright 2002 by Michael G Schwern <schwern@pobox.com
8# Slightly simplified derived version of Assert
9# Differences are:
10#  1. ASSERT instead of assert
11#  2. has to be _explicitly enabled_ using the $ENV{ASSERT}
12#  3. should and shouldnt have been removed
13#  4. Added UNTAINTED
14#
15# Usage is as for Carp::Assert except that you have to explicitly
16# enable asserts using the environment variable ENV{FOSWIKI_ASSERTS}
17# (or ENV{TWIKI_ASSERTS})
18# add ENV{FOSWIKI_ASSERTS} = 1; to you bin/setlib.cfg or bin/LocalLib.cfg
19
20use strict;
21
22use vars qw(@ISA $VERSION %EXPORT_TAGS);
23
24BEGIN {
25    $VERSION = '0.01';
26
27    $EXPORT_TAGS{NDEBUG} = [qw(ASSERT UNTAINTED DEBUG)];
28    $EXPORT_TAGS{DEBUG}  = $EXPORT_TAGS{NDEBUG};
29    Exporter::export_tags(qw(NDEBUG DEBUG));
30}
31
32# constant.pm, alas, adds too much load time (yes, I benchmarked it)
33sub ASSERTS_ON()  { 1 }    # CONSTANT
34sub ASSERTS_OFF() { 0 }    # CONSTANT
35
36# Export the proper DEBUG flag if FOSWIKI_ASSERTS is set,
37# otherwise export noop versions of our routines
38sub noop { }
39
40sub import {
41    no warnings 'redefine';
42    no strict 'refs';
43    if ( $ENV{FOSWIKI_ASSERTS} || $ENV{TWIKI_ASSERTS} ) {
44        *DEBUG = *ASSERTS_ON;
45        Assert->export_to_level( 1, @_ );
46    }
47    else {
48        my $caller = caller;
49        *{ $caller . '::ASSERT' }    = \&noop;
50        *{ $caller . '::UNTAINTED' } = \&ASSERTS_OFF;
51        *{ $caller . '::DEBUG' }     = \&ASSERTS_OFF;
52    }
53    use strict 'refs';
54    use warnings 'redefine';
55}
56
57sub ASSERT ($;$) {
58    unless ( $_[0] ) {
59        require Carp;
60        my $msg = 'Assertion';
61        $msg .= " ($_[1])" if defined $_[1];
62        $msg .= " failed!\n";
63        Carp::confess($msg);
64    }
65    return undef;
66}
67
68sub UNTAINTED($) {
69    local ( @_, $@, $^W ) = @_;
70    my $x;
71    return eval { $x = $_[0], kill 0; 1 };
72}
73
741;
Note: See TracBrowser for help on using the repository browser.