| 1 |
#============================================================= -*-Perl-*- |
| 2 |
# |
| 3 |
# Template::Constants.pm |
| 4 |
# |
| 5 |
# DESCRIPTION |
| 6 |
# Definition of constants for the Template Toolkit. |
| 7 |
# |
| 8 |
# AUTHOR |
| 9 |
# Andy Wardley <[email protected]> |
| 10 |
# |
| 11 |
# COPYRIGHT |
| 12 |
# Copyright (C) 1996-2007 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 |
# Perl::MinimumVersion seems to think this is a Perl 5.008ism... |
| 26 |
# use base qw( Exporter ); |
| 27 |
use vars qw( @EXPORT_OK %EXPORT_TAGS ); |
| 28 |
use vars qw( $DEBUG_OPTIONS @STATUS @ERROR @CHOMP @DEBUG @ISA ); |
| 29 |
# ... so we'll do it the Old Skool way just to keep it quiet |
| 30 |
@ISA = qw( Exporter ); |
| 31 |
|
| 32 |
our $VERSION = 2.75; |
| 33 |
|
| 34 |
|
| 35 |
#======================================================================== |
| 36 |
# ----- EXPORTER ----- |
| 37 |
#======================================================================== |
| 38 |
|
| 39 |
# STATUS constants returned by directives |
| 40 |
use constant STATUS_OK => 0; # ok |
| 41 |
use constant STATUS_RETURN => 1; # ok, block ended by RETURN |
| 42 |
use constant STATUS_STOP => 2; # ok, stoppped by STOP |
| 43 |
use constant STATUS_DONE => 3; # ok, iterator done |
| 44 |
use constant STATUS_DECLINED => 4; # ok, declined to service request |
| 45 |
use constant STATUS_ERROR => 255; # error condition |
| 46 |
|
| 47 |
# ERROR constants for indicating exception types |
| 48 |
use constant ERROR_RETURN => 'return'; # return a status code |
| 49 |
use constant ERROR_FILE => 'file'; # file error: I/O, parse, recursion |
| 50 |
use constant ERROR_VIEW => 'view'; # view error |
| 51 |
use constant ERROR_UNDEF => 'undef'; # undefined variable value used |
| 52 |
use constant ERROR_PERL => 'perl'; # error in [% PERL %] block |
| 53 |
use constant ERROR_FILTER => 'filter'; # filter error |
| 54 |
use constant ERROR_PLUGIN => 'plugin'; # plugin error |
| 55 |
|
| 56 |
# CHOMP constants for PRE_CHOMP and POST_CHOMP |
| 57 |
use constant CHOMP_NONE => 0; # do not remove whitespace |
| 58 |
use constant CHOMP_ALL => 1; # remove whitespace up to newline |
| 59 |
use constant CHOMP_ONE => 1; # new name for CHOMP_ALL |
| 60 |
use constant CHOMP_COLLAPSE => 2; # collapse whitespace to a single space |
| 61 |
use constant CHOMP_GREEDY => 3; # remove all whitespace including newlines |
| 62 |
|
| 63 |
# DEBUG constants to enable various debugging options |
| 64 |
use constant DEBUG_OFF => 0; # do nothing |
| 65 |
use constant DEBUG_ON => 1; # basic debugging flag |
| 66 |
use constant DEBUG_UNDEF => 2; # throw undef on undefined variables |
| 67 |
use constant DEBUG_VARS => 4; # general variable debugging |
| 68 |
use constant DEBUG_DIRS => 8; # directive debugging |
| 69 |
use constant DEBUG_STASH => 16; # general stash debugging |
| 70 |
use constant DEBUG_CONTEXT => 32; # context debugging |
| 71 |
use constant DEBUG_PARSER => 64; # parser debugging |
| 72 |
use constant DEBUG_PROVIDER => 128; # provider debugging |
| 73 |
use constant DEBUG_PLUGINS => 256; # plugins debugging |
| 74 |
use constant DEBUG_FILTERS => 512; # filters debugging |
| 75 |
use constant DEBUG_SERVICE => 1024; # context debugging |
| 76 |
use constant DEBUG_ALL => 2047; # everything |
| 77 |
|
| 78 |
# extra debugging flags |
| 79 |
use constant DEBUG_CALLER => 4096; # add caller file/line |
| 80 |
use constant DEBUG_FLAGS => 4096; # bitmask to extraxt flags |
| 81 |
|
| 82 |
$DEBUG_OPTIONS = { |
| 83 |
&DEBUG_OFF => off => off => &DEBUG_OFF, |
| 84 |
&DEBUG_ON => on => on => &DEBUG_ON, |
| 85 |
&DEBUG_UNDEF => undef => undef => &DEBUG_UNDEF, |
| 86 |
&DEBUG_VARS => vars => vars => &DEBUG_VARS, |
| 87 |
&DEBUG_DIRS => dirs => dirs => &DEBUG_DIRS, |
| 88 |
&DEBUG_STASH => stash => stash => &DEBUG_STASH, |
| 89 |
&DEBUG_CONTEXT => context => context => &DEBUG_CONTEXT, |
| 90 |
&DEBUG_PARSER => parser => parser => &DEBUG_PARSER, |
| 91 |
&DEBUG_PROVIDER => provider => provider => &DEBUG_PROVIDER, |
| 92 |
&DEBUG_PLUGINS => plugins => plugins => &DEBUG_PLUGINS, |
| 93 |
&DEBUG_FILTERS => filters => filters => &DEBUG_FILTERS, |
| 94 |
&DEBUG_SERVICE => service => service => &DEBUG_SERVICE, |
| 95 |
&DEBUG_ALL => all => all => &DEBUG_ALL, |
| 96 |
&DEBUG_CALLER => caller => caller => &DEBUG_CALLER, |
| 97 |
}; |
| 98 |
|
| 99 |
@STATUS = qw( STATUS_OK STATUS_RETURN STATUS_STOP STATUS_DONE |
| 100 |
STATUS_DECLINED STATUS_ERROR ); |
| 101 |
@ERROR = qw( ERROR_FILE ERROR_VIEW ERROR_UNDEF ERROR_PERL |
| 102 |
ERROR_RETURN ERROR_FILTER ERROR_PLUGIN ); |
| 103 |
@CHOMP = qw( CHOMP_NONE CHOMP_ALL CHOMP_ONE CHOMP_COLLAPSE CHOMP_GREEDY ); |
| 104 |
@DEBUG = qw( DEBUG_OFF DEBUG_ON DEBUG_UNDEF DEBUG_VARS |
| 105 |
DEBUG_DIRS DEBUG_STASH DEBUG_CONTEXT DEBUG_PARSER |
| 106 |
DEBUG_PROVIDER DEBUG_PLUGINS DEBUG_FILTERS DEBUG_SERVICE |
| 107 |
DEBUG_ALL DEBUG_CALLER DEBUG_FLAGS ); |
| 108 |
|
| 109 |
@EXPORT_OK = ( @STATUS, @ERROR, @CHOMP, @DEBUG ); |
| 110 |
%EXPORT_TAGS = ( |
| 111 |
'all' => [ @EXPORT_OK ], |
| 112 |
'status' => [ @STATUS ], |
| 113 |
'error' => [ @ERROR ], |
| 114 |
'chomp' => [ @CHOMP ], |
| 115 |
'debug' => [ @DEBUG ], |
| 116 |
); |
| 117 |
|
| 118 |
|
| 119 |
sub debug_flags { |
| 120 |
my ($self, $debug) = @_; |
| 121 |
my (@flags, $flag, $value); |
| 122 |
$debug = $self unless defined($debug) || ref($self); |
| 123 |
|
| 124 |
if ($debug =~ /^\d+$/) { |
| 125 |
foreach $flag (@DEBUG) { |
| 126 |
next if $flag =~ /^DEBUG_(OFF|ALL|FLAGS)$/; |
| 127 |
|
| 128 |
# don't trash the original |
| 129 |
my $copy = $flag; |
| 130 |
$flag =~ s/^DEBUG_//; |
| 131 |
$flag = lc $flag; |
| 132 |
return $self->error("no value for flag: $flag") |
| 133 |
unless defined($value = $DEBUG_OPTIONS->{ $flag }); |
| 134 |
$flag = $value; |
| 135 |
|
| 136 |
if ($debug & $flag) { |
| 137 |
$value = $DEBUG_OPTIONS->{ $flag }; |
| 138 |
return $self->error("no value for flag: $flag") unless defined $value; |
| 139 |
push(@flags, $value); |
| 140 |
} |
| 141 |
} |
| 142 |
return wantarray ? @flags : join(', ', @flags); |
| 143 |
} |
| 144 |
else { |
| 145 |
@flags = split(/\W+/, $debug); |
| 146 |
$debug = 0; |
| 147 |
foreach $flag (@flags) { |
| 148 |
$value = $DEBUG_OPTIONS->{ $flag }; |
| 149 |
return $self->error("unknown debug flag: $flag") unless defined $value; |
| 150 |
$debug |= $value; |
| 151 |
} |
| 152 |
return $debug; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
1; |
| 158 |
|
| 159 |
__END__ |
| 160 |
|
| 161 |
=head1 NAME |
| 162 |
|
| 163 |
Template::Constants - Defines constants for the Template Toolkit |
| 164 |
|
| 165 |
=head1 SYNOPSIS |
| 166 |
|
| 167 |
use Template::Constants qw( :status :error :all ); |
| 168 |
|
| 169 |
=head1 DESCRIPTION |
| 170 |
|
| 171 |
The C<Template::Constants> modules defines, and optionally exports into the |
| 172 |
caller's namespace, a number of constants used by the L<Template> package. |
| 173 |
|
| 174 |
Constants may be used by specifying the C<Template::Constants> package |
| 175 |
explicitly: |
| 176 |
|
| 177 |
use Template::Constants; |
| 178 |
print Template::Constants::STATUS_DECLINED; |
| 179 |
|
| 180 |
Constants may be imported into the caller's namespace by naming them as |
| 181 |
options to the C<use Template::Constants> statement: |
| 182 |
|
| 183 |
use Template::Constants qw( STATUS_DECLINED ); |
| 184 |
print STATUS_DECLINED; |
| 185 |
|
| 186 |
Alternatively, one of the following tagset identifiers may be specified |
| 187 |
to import sets of constants: 'C<:status>', 'C<:error>', 'C<:all>'. |
| 188 |
|
| 189 |
use Template::Constants qw( :status ); |
| 190 |
print STATUS_DECLINED; |
| 191 |
|
| 192 |
Consult the documentation for the C<Exporter> module for more information |
| 193 |
on exporting variables. |
| 194 |
|
| 195 |
=head1 EXPORTABLE TAG SETS |
| 196 |
|
| 197 |
The following tag sets and associated constants are defined: |
| 198 |
|
| 199 |
:status |
| 200 |
STATUS_OK # no problem, continue |
| 201 |
STATUS_RETURN # ended current block then continue (ok) |
| 202 |
STATUS_STOP # controlled stop (ok) |
| 203 |
STATUS_DONE # iterator is all done (ok) |
| 204 |
STATUS_DECLINED # provider declined to service request (ok) |
| 205 |
STATUS_ERROR # general error condition (not ok) |
| 206 |
|
| 207 |
:error |
| 208 |
ERROR_RETURN # return a status code (e.g. 'stop') |
| 209 |
ERROR_FILE # file error: I/O, parse, recursion |
| 210 |
ERROR_UNDEF # undefined variable value used |
| 211 |
ERROR_PERL # error in [% PERL %] block |
| 212 |
ERROR_FILTER # filter error |
| 213 |
ERROR_PLUGIN # plugin error |
| 214 |
|
| 215 |
:chomp # for PRE_CHOMP and POST_CHOMP |
| 216 |
CHOMP_NONE # do not remove whitespace |
| 217 |
CHOMP_ONE # remove whitespace to newline |
| 218 |
CHOMP_ALL # old name for CHOMP_ONE (deprecated) |
| 219 |
CHOMP_COLLAPSE # collapse whitespace to a single space |
| 220 |
CHOMP_GREEDY # remove all whitespace including newlines |
| 221 |
|
| 222 |
:debug |
| 223 |
DEBUG_OFF # do nothing |
| 224 |
DEBUG_ON # basic debugging flag |
| 225 |
DEBUG_UNDEF # throw undef on undefined variables |
| 226 |
DEBUG_VARS # general variable debugging |
| 227 |
DEBUG_DIRS # directive debugging |
| 228 |
DEBUG_STASH # general stash debugging |
| 229 |
DEBUG_CONTEXT # context debugging |
| 230 |
DEBUG_PARSER # parser debugging |
| 231 |
DEBUG_PROVIDER # provider debugging |
| 232 |
DEBUG_PLUGINS # plugins debugging |
| 233 |
DEBUG_FILTERS # filters debugging |
| 234 |
DEBUG_SERVICE # context debugging |
| 235 |
DEBUG_ALL # everything |
| 236 |
DEBUG_CALLER # add caller file/line info |
| 237 |
DEBUG_FLAGS # bitmap used internally |
| 238 |
|
| 239 |
:all |
| 240 |
All the above constants. |
| 241 |
|
| 242 |
=head1 AUTHOR |
| 243 |
|
| 244 |
Andy Wardley E<lt>[email protected]<gt> L<http://wardley.org/> |
| 245 |
|
| 246 |
=head1 COPYRIGHT |
| 247 |
|
| 248 |
Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved. |
| 249 |
|
| 250 |
This module is free software; you can redistribute it and/or |
| 251 |
modify it under the same terms as Perl itself. |
| 252 |
|
| 253 |
=head1 SEE ALSO |
| 254 |
|
| 255 |
L<Template>, C<Exporter> |
| 256 |
|
| 257 |
=cut |
| 258 |
|
| 259 |
# Local Variables: |
| 260 |
# mode: perl |
| 261 |
# perl-indent-level: 4 |
| 262 |
# indent-tabs-mode: nil |
| 263 |
# End: |
| 264 |
# |
| 265 |
# vim: expandtab shiftwidth=4: |