| 1 | #============================================================= -*-Perl-*- | ||
| 2 | # | ||
| 3 | # Template::Constants.pm | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # Definition of constants for the Template Toolkit. | ||
| 7 | # | ||
| 8 | # AUTHOR | ||
| 9 | # Andy Wardley <abw@wardley.org> | ||
| 10 | # | ||
| 11 | # COPYRIGHT | ||
| 12 | # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved. | ||
| 13 | # | ||
| 14 | # This module is free software; you can redistribute it and/or | ||
| 15 | # modify it under the same terms as Perl itself. | ||
| 16 | # | ||
| 17 | #============================================================================ | ||
| 18 | |||
| 19 | package Template::Constants; | ||
| 20 | |||
| 21 | require Exporter; | ||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | use Exporter; | ||
| 25 | |||
| 26 | use base qw( Exporter ); | ||
| 27 | |||
| 28 | our ( @EXPORT_OK, %EXPORT_TAGS ); | ||
| 29 | our ( $DEBUG_OPTIONS, @STATUS, @ERROR, @CHOMP, @DEBUG, @ISA ); | ||
| 30 | |||
| 31 | our $VERSION = '3.105'; | ||
| 32 | |||
| 33 | #======================================================================== | ||
| 34 | # ----- EXPORTER ----- | ||
| 35 | #======================================================================== | ||
| 36 | |||
| 37 | # STATUS constants returned by directives | ||
| 38 | use constant STATUS_OK => 0; # ok | ||
| 39 | use constant STATUS_RETURN => 1; # ok, block ended by RETURN | ||
| 40 | use constant STATUS_STOP => 2; # ok, stopped by STOP | ||
| 41 | use constant STATUS_DONE => 3; # ok, iterator done | ||
| 42 | use constant STATUS_DECLINED => 4; # ok, declined to service request | ||
| 43 | use constant STATUS_ERROR => 255; # error condition | ||
| 44 | |||
| 45 | # ERROR constants for indicating exception types | ||
| 46 | use constant ERROR_RETURN => 'return'; # return a status code | ||
| 47 | use constant ERROR_FILE => 'file'; # file error: I/O, parse, recursion | ||
| 48 | use constant ERROR_VIEW => 'view'; # view error | ||
| 49 | use constant ERROR_UNDEF => 'undef'; # undefined variable value used | ||
| 50 | use constant ERROR_PERL => 'perl'; # error in [% PERL %] block | ||
| 51 | use constant ERROR_FILTER => 'filter'; # filter error | ||
| 52 | use constant ERROR_PLUGIN => 'plugin'; # plugin error | ||
| 53 | |||
| 54 | # CHOMP constants for PRE_CHOMP and POST_CHOMP | ||
| 55 | use constant CHOMP_NONE => 0; # do not remove whitespace | ||
| 56 | use constant CHOMP_ALL => 1; # remove whitespace up to newline | ||
| 57 | use constant CHOMP_ONE => 1; # new name for CHOMP_ALL | ||
| 58 | use constant CHOMP_COLLAPSE => 2; # collapse whitespace to a single space | ||
| 59 | use constant CHOMP_GREEDY => 3; # remove all whitespace including newlines | ||
| 60 | |||
| 61 | # DEBUG constants to enable various debugging options | ||
| 62 | use constant DEBUG_OFF => 0; # do nothing | ||
| 63 | use constant DEBUG_ON => 1; # basic debugging flag | ||
| 64 | use constant DEBUG_UNDEF => 2; # throw undef on undefined variables | ||
| 65 | use constant DEBUG_VARS => 4; # general variable debugging | ||
| 66 | use constant DEBUG_DIRS => 8; # directive debugging | ||
| 67 | use constant DEBUG_STASH => 16; # general stash debugging | ||
| 68 | use constant DEBUG_CONTEXT => 32; # context debugging | ||
| 69 | use constant DEBUG_PARSER => 64; # parser debugging | ||
| 70 | use constant DEBUG_PROVIDER => 128; # provider debugging | ||
| 71 | use constant DEBUG_PLUGINS => 256; # plugins debugging | ||
| 72 | use constant DEBUG_FILTERS => 512; # filters debugging | ||
| 73 | use constant DEBUG_SERVICE => 1024; # context debugging | ||
| 74 | use constant DEBUG_ALL => 2047; # everything | ||
| 75 | |||
| 76 | # extra debugging flags | ||
| 77 | use constant DEBUG_CALLER => 4096; # add caller file/line | ||
| 78 | use constant DEBUG_FLAGS => 4096; # bitmask to extract flags | ||
| 79 | |||
| 80 | $DEBUG_OPTIONS = { | ||
| 81 | &DEBUG_OFF => off => off => &DEBUG_OFF, | ||
| 82 | &DEBUG_ON => on => on => &DEBUG_ON, | ||
| 83 | &DEBUG_UNDEF => undef => undef => &DEBUG_UNDEF, | ||
| 84 | &DEBUG_VARS => vars => vars => &DEBUG_VARS, | ||
| 85 | &DEBUG_DIRS => dirs => dirs => &DEBUG_DIRS, | ||
| 86 | &DEBUG_STASH => stash => stash => &DEBUG_STASH, | ||
| 87 | &DEBUG_CONTEXT => context => context => &DEBUG_CONTEXT, | ||
| 88 | &DEBUG_PARSER => parser => parser => &DEBUG_PARSER, | ||
| 89 | &DEBUG_PROVIDER => provider => provider => &DEBUG_PROVIDER, | ||
| 90 | &DEBUG_PLUGINS => plugins => plugins => &DEBUG_PLUGINS, | ||
| 91 | &DEBUG_FILTERS => filters => filters => &DEBUG_FILTERS, | ||
| 92 | &DEBUG_SERVICE => service => service => &DEBUG_SERVICE, | ||
| 93 | &DEBUG_ALL => all => all => &DEBUG_ALL, | ||
| 94 | &DEBUG_CALLER => caller => caller => &DEBUG_CALLER, | ||
| 95 | }; | ||
| 96 | |||
| 97 | @STATUS = qw( STATUS_OK STATUS_RETURN STATUS_STOP STATUS_DONE | ||
| 98 | STATUS_DECLINED STATUS_ERROR ); | ||
| 99 | @ERROR = qw( ERROR_FILE ERROR_VIEW ERROR_UNDEF ERROR_PERL | ||
| 100 | ERROR_RETURN ERROR_FILTER ERROR_PLUGIN ); | ||
| 101 | @CHOMP = qw( CHOMP_NONE CHOMP_ALL CHOMP_ONE CHOMP_COLLAPSE CHOMP_GREEDY ); | ||
| 102 | @DEBUG = qw( DEBUG_OFF DEBUG_ON DEBUG_UNDEF DEBUG_VARS | ||
| 103 | DEBUG_DIRS DEBUG_STASH DEBUG_CONTEXT DEBUG_PARSER | ||
| 104 | DEBUG_PROVIDER DEBUG_PLUGINS DEBUG_FILTERS DEBUG_SERVICE | ||
| 105 | DEBUG_ALL DEBUG_CALLER DEBUG_FLAGS ); | ||
| 106 | |||
| 107 | @EXPORT_OK = ( @STATUS, @ERROR, @CHOMP, @DEBUG ); | ||
| 108 | %EXPORT_TAGS = ( | ||
| 109 | 'all' => [ @EXPORT_OK ], | ||
| 110 | 'status' => [ @STATUS ], | ||
| 111 | 'error' => [ @ERROR ], | ||
| 112 | 'chomp' => [ @CHOMP ], | ||
| 113 | 'debug' => [ @DEBUG ], | ||
| 114 | ); | ||
| 115 | |||
| 116 | |||
| 117 | sub debug_flags { | ||
| 118 | my ($self, $debug) = @_; | ||
| 119 | my (@flags, $flag, $value); | ||
| 120 | $debug = $self unless defined($debug) || ref($self); | ||
| 121 | |||
| 122 | if ( $debug !~ tr{0-9}{}c) { | ||
| 123 | foreach $flag (@DEBUG) { | ||
| 124 | next if $flag eq 'DEBUG_OFF' || $flag eq 'DEBUG_ALL' || $flag eq 'DEBUG_FLAGS'; | ||
| 125 | |||
| 126 | # don't trash the original | ||
| 127 | substr($flag,0,6,'') if index($flag,'DEBUG_') == 0; | ||
| 128 | $flag = lc $flag; | ||
| 129 | return $self->error("no value for flag: $flag") | ||
| 130 | unless defined($value = $DEBUG_OPTIONS->{ $flag }); | ||
| 131 | $flag = $value; | ||
| 132 | |||
| 133 | if ($debug & $flag) { | ||
| 134 | $value = $DEBUG_OPTIONS->{ $flag }; | ||
| 135 | return $self->error("no value for flag: $flag") unless defined $value; | ||
| 136 | push(@flags, $value); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | return wantarray ? @flags : join(', ', @flags); | ||
| 140 | } | ||
| 141 | else { | ||
| 142 | @flags = split(/\W+/, $debug); | ||
| 143 | $debug = 0; | ||
| 144 | foreach $flag (@flags) { | ||
| 145 | $value = $DEBUG_OPTIONS->{ $flag }; | ||
| 146 | return $self->error("unknown debug flag: $flag") unless defined $value; | ||
| 147 | $debug |= $value; | ||
| 148 | } | ||
| 149 | return $debug; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | 1; | ||
| 155 | |||
| 156 | __END__ |