| 1 | # ************************************************************************* | ||
| 2 | # Copyright (c) 2014-2020, SUSE LLC | ||
| 3 | # | ||
| 4 | # All rights reserved. | ||
| 5 | # | ||
| 6 | # Redistribution and use in source and binary forms, with or without | ||
| 7 | # modification, are permitted provided that the following conditions are met: | ||
| 8 | # | ||
| 9 | # 1. Redistributions of source code must retain the above copyright notice, | ||
| 10 | # this list of conditions and the following disclaimer. | ||
| 11 | # | ||
| 12 | # 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | # notice, this list of conditions and the following disclaimer in the | ||
| 14 | # documentation and/or other materials provided with the distribution. | ||
| 15 | # | ||
| 16 | # 3. Neither the name of SUSE LLC nor the names of its contributors may be | ||
| 17 | # used to endorse or promote products derived from this software without | ||
| 18 | # specific prior written permission. | ||
| 19 | # | ||
| 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 21 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 24 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 25 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 26 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 27 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 28 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 29 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 30 | # POSSIBILITY OF SUCH DAMAGE. | ||
| 31 | # ************************************************************************* | ||
| 32 | |||
| 33 | package App::CELL::Guide; | ||
| 34 | |||
| 35 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 36 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 37 | 1 | ▶ | use 5.012; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | =head1 NAME | ||
| 42 | |||
| 43 | App::CELL::Guide - Introduction to App::CELL (POD-only module) | ||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | =head1 VERSION | ||
| 48 | |||
| 49 | Version 0.231 | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | 1 | our $VERSION = '0.231'; | |
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | =head1 SYNOPSIS | ||
| 58 | |||
| 59 | $ perldoc App::CELL::Guide | ||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | =head1 INTRODUCTION | ||
| 64 | |||
| 65 | L<App::CELL> is the Configuration, Error-handling, Localization, and Logging | ||
| 66 | (CELL) framework for applications written in Perl. In the L</GENERAL APPROACH> | ||
| 67 | section, this Guide describes the CELL approach to each of these four areas, | ||
| 68 | separately. Then, in the L</RATIONALE> section, it presents the author's reasons | ||
| 69 | for bundling them together. | ||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | =head1 HISTORY | ||
| 74 | |||
| 75 | The original L<App::CELL> was written by Nathan Cutler in 2013 and 2014, | ||
| 76 | initially as part of the L<App::Dochazka::REST> project. Later, with a view to | ||
| 77 | its generic nature, it was spun off into a separate project. | ||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | =head1 GENERAL APPROACH | ||
| 82 | |||
| 83 | This section presents CELL's approach to each of its four principal | ||
| 84 | functions: L</Configuration>, L</Error handling>, L<Localization>, and | ||
| 85 | L<Logging>. | ||
| 86 | |||
| 87 | |||
| 88 | =head2 Approach to configuration | ||
| 89 | |||
| 90 | CELL provides the application developer and site administrator with a | ||
| 91 | straightforward and powerful way to define configuration parameters as | ||
| 92 | needed by the application. If you are familiar with Request Tracker, you | ||
| 93 | will know that there is a directory (C</opt/...> by default) which contains | ||
| 94 | two files, called C<RT_Config.pm> and C<RT_SiteConfig.pm> -- as their names | ||
| 95 | would indicate, they are actually Perl modules. The former is provided by | ||
| 96 | the upstream developers and contains all of RT's configuration parameters | ||
| 97 | and their "factory default" settings. The content of the latter is entirely | ||
| 98 | up to the RT site administrator and contains only those parameters that | ||
| 99 | need to be different from the defaults. Parameter settings in | ||
| 100 | C<RT_SiteConfig.pm>, then, override the defaults set in C<RT_Config.pm>. | ||
| 101 | |||
| 102 | L<App::CELL> provides this same functionality in a drop-in Perl module, | ||
| 103 | with some subtle differences. While RT uses a syntax like this: | ||
| 104 | |||
| 105 | set( 'MY_PARAM', ...arguments...); | ||
| 106 | |||
| 107 | where C<...arguments...> is a list of scalar values (as with any Perl | ||
| 108 | subroutine), L<App::CELL> uses a slightly different format: | ||
| 109 | |||
| 110 | set( 'MY_PARAM', $scalar ); | ||
| 111 | |||
| 112 | where C<$scalar> can be any scalar value, i.e. including references. | ||
| 113 | |||
| 114 | (Another difference is that L<App::CELL> provides both immutable site | ||
| 115 | parameters _and_ mutable C<meta> configuration parameters, whereas RT's | ||
| 116 | meta parameters are only used by RT itself.) For more information on | ||
| 117 | configuration, see L</Configuration in depth>. | ||
| 118 | |||
| 119 | |||
| 120 | =head2 Error handling | ||
| 121 | |||
| 122 | To facilitate error handling and make the application's source code easier | ||
| 123 | to read and understand, or at least mitigate its impenetrability, CELL | ||
| 124 | provides the L<App::CELL::Status> module, which enables functions in the | ||
| 125 | application to return status objects if desired. | ||
| 126 | |||
| 127 | Status objects have the following principal attributes: C<level>, C<code>, | ||
| 128 | C<args>, and C<payload>, which are given by the programmer when the status | ||
| 129 | object is constructed, as well as attributes like C<text>, C<lang>, and | ||
| 130 | C<caller>, which are derived by CELL. In addition to the attributes, | ||
| 131 | C<Status.pm> also provides some useful methods for processing status | ||
| 132 | objects. | ||
| 133 | |||
| 134 | In order to signify an error, subroutine C<foo_dis> could for example do | ||
| 135 | this: | ||
| 136 | |||
| 137 | return $CELL->status_err( code => 'Gidget displacement %s out of range', | ||
| 138 | args => [ $displacement ], | ||
| 139 | ); | ||
| 140 | |||
| 141 | (Instead of having the error text in the C<code>, it could be placed in a | ||
| 142 | message file in the sitedir with a code like DISP_OUT_OF_RANGE.) | ||
| 143 | |||
| 144 | On success, C<foo_dis> could return an 'OK' status with the gidget | ||
| 145 | displacement value in the payload: | ||
| 146 | |||
| 147 | return $CELL->status_ok( payload => $displacement ); | ||
| 148 | |||
| 149 | The calling function could check the return value like this: | ||
| 150 | |||
| 151 | my $status = foo_dis(); | ||
| 152 | return $status if $status->not_ok; | ||
| 153 | my $displacement = $status->payload; | ||
| 154 | |||
| 155 | For details, see L<App::CELL::Status> and L<App::CELL::Message>. | ||
| 156 | |||
| 157 | CELL's error-handling logic is inspired by brian d foy's article "Return | ||
| 158 | error objects instead of throwing exceptions" | ||
| 159 | |||
| 160 | L<http://www.effectiveperlprogramming.com/2011/10/return-error-objects-instead-of-throwing-exceptions/> | ||
| 161 | |||
| 162 | |||
| 163 | =head2 Localization | ||
| 164 | |||
| 165 | This CELL component, called "Localization", gives the programmer a way to | ||
| 166 | encapsulate a "message" (in its simplest form, a string) within a message | ||
| 167 | object and then use that object in various ways. | ||
| 168 | |||
| 169 | So, provided the necessary message files have been loaded, the programmer | ||
| 170 | could do this: | ||
| 171 | |||
| 172 | my $message = $CELL->message( code => 'FOOBAR' ); | ||
| 173 | print $message->text, '\n'; # message FOOBAR in the default language | ||
| 174 | print $message->text( lang => 'de' ) # same message, in German | ||
| 175 | |||
| 176 | Messages are loaded when CELL is initialized, from files in the site | ||
| 177 | configuration directory. Each file contains messages in a particular | ||
| 178 | language. For example, the file C<Dochazka_Message_en.conf> contains | ||
| 179 | messages relating to the Dochazka application, in the English language. To | ||
| 180 | provide the same messages in German, the file would be copied to | ||
| 181 | C<Dochazka_Message_de.conf> and translated. | ||
| 182 | |||
| 183 | Since message objects are used by L<App::CELL::Status>, it is natural for | ||
| 184 | the programmer to put error messages, warnings, etc. in message files and | ||
| 185 | refer to them by their codes. | ||
| 186 | |||
| 187 | C<App::CELL::Message> could also be extended to provide methods for | ||
| 188 | encrypting messages and/or converting them into various target formats | ||
| 189 | (JSON, HTML, Morse code, etc.). | ||
| 190 | |||
| 191 | For details, see </Localization in depth> and <App::CELL::Message>. | ||
| 192 | |||
| 193 | |||
| 194 | =head2 Logging | ||
| 195 | |||
| 196 | For logging, CELL uses L<Log::Any> and optionally extends it by adding | ||
| 197 | the caller's filename and line number to each message logged. | ||
| 198 | |||
| 199 | Message and status objects have 'log' methods, of course, and by default | ||
| 200 | all statuses (except 'OK') are logged upon creation. | ||
| 201 | |||
| 202 | Here's how to set up (and do) logging in the application: | ||
| 203 | |||
| 204 | use App::CELL::Log qw( $log ); | ||
| 205 | $log->init( ident => 'AppFoo' ); | ||
| 206 | $log->debug( "Three lines into AppFoo" ); | ||
| 207 | |||
| 208 | L<App::CELL::Log> provides its own singleton, but since all method calls | ||
| 209 | are passed to L<Log::Any>, anyway, the L<App::CELL::Log> singleton behaves | ||
| 210 | just like its L<Log::Any> counterpart. This is useful, e.g., for testing | ||
| 211 | log messages: | ||
| 212 | |||
| 213 | use Log::Any::Test; | ||
| 214 | $log->contains_only_ok( "Three lines into AppFoo" ); | ||
| 215 | |||
| 216 | To actually see your log messages, you have to do something like this: | ||
| 217 | |||
| 218 | use Log::Any::Adapter ( 'File', $ENV{'HOME'} . '/CELLtest.log' ); | ||
| 219 | |||
| 220 | or, even simpler: | ||
| 221 | |||
| 222 | use Log::Any::Adapter ( 'Stderr' ); | ||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | =head1 DETAILED SPECIFICATIONS | ||
| 227 | |||
| 228 | =head2 Configuration | ||
| 229 | |||
| 230 | =head3 Three types of parameters | ||
| 231 | |||
| 232 | CELL recognizes three types of configuration parameters: C<meta>, C<core>, | ||
| 233 | and C<site>. These parameters and their values are loaded from files | ||
| 234 | prepared and placed in the sitedir in advance. | ||
| 235 | |||
| 236 | =head4 Meta parameters | ||
| 237 | |||
| 238 | Meta parameters are by definition mutable: the application can change a | ||
| 239 | meta parameter's value any number of times, and L<App::CELL> will not care. | ||
| 240 | Initial C<meta> param settings are placed in a file entitled | ||
| 241 | C<$str_MetaConfig.pm> (where C<$str> is a string free of underscore | ||
| 242 | characters) in the sitedir. For example, if the application name is FooApp, its | ||
| 243 | initial C<meta> parameter settings could be contained in a file called | ||
| 244 | C<FooApp_MetaConfig.pm>. At initialization time, L<App::CELL> looks in | ||
| 245 | the sitedir for files matching this description, and attempts to load them. | ||
| 246 | (See L</How configuration files are named>.) | ||
| 247 | |||
| 248 | =head4 Core parameters | ||
| 249 | |||
| 250 | As in Request Tracker, C<core> parameters have immutable values and are | ||
| 251 | intended to be used as "factory defaults", set by the developer, that the | ||
| 252 | site administrator can override by setting site parameters. If the | ||
| 253 | application is called FooApp, its core configuration settings could be | ||
| 254 | contained in a file called C<FooApp_Config.pm> located in the sitedir. | ||
| 255 | (See L</How configuration files are named> for details.) | ||
| 256 | |||
| 257 | =head4 Site parameters | ||
| 258 | |||
| 259 | Site parameters are kept separate from core parameters, but are closely | ||
| 260 | related to them. As far as the application is concerned, there are only | ||
| 261 | site parameters. How this works is best explained by two examples. | ||
| 262 | |||
| 263 | Let C<FOO> be an application that uses L<App::CELL>. | ||
| 264 | |||
| 265 | In the first example, core param C<FOO> is set to "Bar" and site param | ||
| 266 | C<FOO> is I<not> set at all. When the application calls C<< $site->FOO >> | ||
| 267 | the core parameter value "Bar" is returned. | ||
| 268 | |||
| 269 | In the second example, the core param C<FOO> is set to "Bar" and site | ||
| 270 | param C<FOO> is also set, but to a different value: "Whizzo". In this | ||
| 271 | scenario, when the application calls C<< $site->FOO >> the site parameter | ||
| 272 | ("Whizzo") value is returned. | ||
| 273 | |||
| 274 | This setup allows the site administrator to customize the application. | ||
| 275 | |||
| 276 | Site parameters are set in a file called C<$str_SiteConfig.pm>, where | ||
| 277 | C<$str> could be the appname. | ||
| 278 | |||
| 279 | =head4 Conclusion | ||
| 280 | |||
| 281 | How these three types of parameters are defined and used is up to the | ||
| 282 | application. As far as L<App::CELL> is concerned, they are all optional. | ||
| 283 | |||
| 284 | L<App::CELL> itself has its own internal meta, core, and site parameters, | ||
| 285 | but these are located elsewhere -- in the so-called "sharedir", a directory | ||
| 286 | that is internal to the L<App::CELL> distro/package. | ||
| 287 | |||
| 288 | All these internal parameters start with C<CELL_> and are stored in the | ||
| 289 | same namespaces as the application's parameters. That means the application | ||
| 290 | programmer should avoid using parameters starting with C<CELL_>. | ||
| 291 | |||
| 292 | =head3 Where configuration files are located | ||
| 293 | |||
| 294 | =head4 sitedir | ||
| 295 | |||
| 296 | Configuration parameters are placed in specially-named files within a | ||
| 297 | directory referred to by L<App::CELL> as the "site configuration | ||
| 298 | directory", or "sitedir". This directory is not a part of the L<App::CELL> | ||
| 299 | distribution and L<App::CELL> does not create it. Instead, the application | ||
| 300 | is expected to provide the full path to this directory to CELL's | ||
| 301 | initialization route, either via an argument to the function call or with | ||
| 302 | the help of an environment variable. CELL's initialization routine calls | ||
| 303 | L<App::CELL::Load::init> to do the actual work of walking the directory. | ||
| 304 | |||
| 305 | This "sitedir" (site configuration directory) is assumed to be the place | ||
| 306 | (or a place) where the application can store its configuration information | ||
| 307 | in the form of C<core>, C<site>, and C<meta> parameters. For | ||
| 308 | L</LOCALIZATION> purposes, C<message> codes and their corresponding texts | ||
| 309 | (in one or more languages) can be stored here as well, if desired. | ||
| 310 | |||
| 311 | =head4 sharedir | ||
| 312 | |||
| 313 | CELL itself has an analogous configuration directory, called the | ||
| 314 | "sharedir", where it's own internal configuration defaults are stored. | ||
| 315 | CELL's own core parameters can be overridden by the application's site | ||
| 316 | params, and in some cases this can even be desirable. For example, the | ||
| 317 | parameter C<CELL_DEBUG_MODE> can be overridden in the site configuration to | ||
| 318 | tell CELL to include debug-level messages in the log. | ||
| 319 | |||
| 320 | During initialization, CELL walks first the sharedir, and then | ||
| 321 | the sitedir, looking through those directories and all their | ||
| 322 | subdirectories for meta, core, site, and message configuration files. | ||
| 323 | |||
| 324 | The sharedir is part of the App::CELL distro and CELL's initialization | ||
| 325 | routine finds it via a call to the C<dist_dir> routine in the | ||
| 326 | L<File::ShareDir> module. | ||
| 327 | |||
| 328 | =head3 How the sitedir is specified | ||
| 329 | |||
| 330 | The sitedir must be created and populated with configuration files by the | ||
| 331 | application programmer. Typically, this directory would form part of the | ||
| 332 | application distro and the site administrator would be expected to make a | ||
| 333 | site configuration file for application-specific parameters. The | ||
| 334 | application developer and site administrator have flexibility in this | ||
| 335 | regard -- CELL's initialization routine, C<< $CELL->load >> will work | ||
| 336 | without a sitedir, with one sitedir, or even with multiple sitedirs. | ||
| 337 | |||
| 338 | =head4 No sitedir | ||
| 339 | |||
| 340 | It is possible, but probably not useful, to call C<< $CELL->load >> without | ||
| 341 | a sitedir parameter and without any sitedir specified in the environment. | ||
| 342 | In this case, CELL just loads the sharedir and returns OK. | ||
| 343 | |||
| 344 | =head4 One sitedir | ||
| 345 | |||
| 346 | If there is only one sitedir, there are three possible ways to specify it | ||
| 347 | to CELL's load routine: (1) a C<sitedir> parameter, (2) an | ||
| 348 | C<enviro> parameter, or (3) the hard-coded C<CELL_SITEDIR> environment | ||
| 349 | variable. | ||
| 350 | |||
| 351 | =head4 Multiple sitedirs | ||
| 352 | |||
| 353 | If the application needs to load configuration parameters from multiple | ||
| 354 | sitedirs, this can be accomplished simply by calling C<< $CELL->load >> | ||
| 355 | multiple times with different C<sitedir> arguments. | ||
| 356 | |||
| 357 | =head3 Sitedir search algorithm | ||
| 358 | |||
| 359 | Every time it is called, the load routine uses the following algorithm | ||
| 360 | to search for a/the sitedir: | ||
| 361 | |||
| 362 | =over | ||
| 363 | |||
| 364 | =item C<sitedir> parameter -- | ||
| 365 | a C<sitedir> parameter containing the full path to the sitedir can be | ||
| 366 | passed. If it is present, CELL will try it first. If needed for | ||
| 367 | portability, the path can be constructed using L<File::Spec> (e.g. the | ||
| 368 | C<catfile> method) or similar. It should be string containing the full path | ||
| 369 | to the directory. If the C<sitedir> argument points to a valid sitedir, it | ||
| 370 | is loaded and OK is returned. If a C<sitedir> argument is present but | ||
| 371 | invalid, an ERR status results. If no C<sitedir> argument was given, CELL | ||
| 372 | continues to the next step. | ||
| 373 | |||
| 374 | =item C<enviro> parameter -- | ||
| 375 | if no C<sitedir> parameter is given, C<< $CELL->load >> looks for a | ||
| 376 | parameter called C<enviro> which it interprets as the name of an | ||
| 377 | environment variable containing the sitedir path. If the C<enviro> | ||
| 378 | argument points to a valid sitedir, it is loaded and OK is returned. If an | ||
| 379 | C<enviro> argument is present but invalid, an ERR status results. If there | ||
| 380 | is no C<enviro> argument at all, CELL continues to the next step. | ||
| 381 | |||
| 382 | =item C<CELL_SITEDIR> environment variable -- | ||
| 383 | if no viable sitedir can be found by consulting the function call | ||
| 384 | parameters, CELL's load routine falls back to this hardcoded environment | ||
| 385 | variable. If the C<CELL_SITEDIR> environment variable exists and points to | ||
| 386 | a valid sitedir, it is loaded and OK is returned. If it exists but the | ||
| 387 | directory is invalid, an ERR status is returned. If the environment | ||
| 388 | variable doesn't exist, CELL writes a warning to the log (all attempts to | ||
| 389 | find the sitedir failed). The return status in this case can be either WARN | ||
| 390 | (if no sitedir was found in a previous call to the function) or OK if at | ||
| 391 | least one sitedir has been loaded. | ||
| 392 | |||
| 393 | =back | ||
| 394 | |||
| 395 | The C<load> routine is re-entrant: it can be called any number of times. | ||
| 396 | On first call, it will load CELL's own sharedir, as well as any sitedir | ||
| 397 | that can be found using the above algorithm. All further calls will just | ||
| 398 | run the sitedir search algorithm again. Each time it will find and load at | ||
| 399 | most one sitedir. CELL maintains a list of loaded sitedirs in | ||
| 400 | C<< $meta->CELL_META_SITEDIR_LIST >>. | ||
| 401 | |||
| 402 | For examples of how to call the C<load> routine, see L<App::CELL/SYNOPSIS>. | ||
| 403 | |||
| 404 | =head3 How configuration files are named | ||
| 405 | |||
| 406 | Once it finds a valid sitedir, CELL walks it (including I<all> its | ||
| 407 | subdirectories), assembling a list of filenames matching one four regular | ||
| 408 | expressions: | ||
| 409 | |||
| 410 | =over | ||
| 411 | |||
| 412 | =item C<^.+_MetaConfig.pm$> (meta) | ||
| 413 | |||
| 414 | =item C<^.+_Config.pm$> (core) | ||
| 415 | |||
| 416 | =item C<^.+_SiteConfig.pm$> (site) | ||
| 417 | |||
| 418 | =item C<^.+_Message(_[^_]+){0,1}.conf$> (message) | ||
| 419 | |||
| 420 | =back | ||
| 421 | |||
| 422 | Files with names that don't match any of the above regexes are ignored. | ||
| 423 | |||
| 424 | After the directory is walked, the files are loaded (i.e. parsed for config | ||
| 425 | params and messages). | ||
| 426 | |||
| 427 | The syntax of these files is simple and should be obvious from an | ||
| 428 | examination of CELL's own configuration files in the sharedir (C<config/> | ||
| 429 | in the distro). All four types of configuration file are there, | ||
| 430 | with comments. | ||
| 431 | |||
| 432 | Since the configuration files are Perl modules, Perl itself is leveraged | ||
| 433 | to parse them. Values can be any legal scalar value, so references to | ||
| 434 | arrays, hashes, or subroutines can be used, as well as simple numbers and | ||
| 435 | strings. For details, see L</SITE CONFIGURATION DIRECTORY>, | ||
| 436 | L<App::CELL::Config> and L<App::CELL::Load>. | ||
| 437 | |||
| 438 | Message file parsing is done by a parsing routine that resides in | ||
| 439 | L<App::CELL::Load>. For details on the syntax and how the parser works, see | ||
| 440 | L<LOCALIZATION>. | ||
| 441 | |||
| 442 | |||
| 443 | =head2 Configuration diagnostics | ||
| 444 | |||
| 445 | CELL provides several ways for the application to find out if the | ||
| 446 | configuration files were loaded properly. First of all, the load routine | ||
| 447 | (C<< $CELL->load >>) returns a status object: if the status is not OK, | ||
| 448 | something went wrong and the application should look at the status more | ||
| 449 | closely. | ||
| 450 | |||
| 451 | After program control returns from the load routine, the following methods | ||
| 452 | and attributes can be used to find out what happened: | ||
| 453 | |||
| 454 | =over | ||
| 455 | |||
| 456 | =item C<< $site->CELL_SHAREDIR_LOADED >> (boolean value) | ||
| 457 | |||
| 458 | =item C<< $site->CELL_SHAREDIR_FULLPATH >> (full path of CELL's sharedir) | ||
| 459 | |||
| 460 | =item C<< $meta->CELL_META_SITEDIR_LOADED >> (boolean value: true if at | ||
| 461 | least one sitedir has been loaded) | ||
| 462 | |||
| 463 | =item C<< $meta->CELL_META_SITEDIR_LIST >> (reference to a list of all sitedirs | ||
| 464 | that have been loaded -- full paths) | ||
| 465 | |||
| 466 | =back | ||
| 467 | |||
| 468 | =head3 Verbose and debug mode | ||
| 469 | |||
| 470 | The load routine takes two options to increase its verbosity. The first | ||
| 471 | option, C<verbose>, can be passed like this: | ||
| 472 | |||
| 473 | my $status = $CELL->load( verbose => 1 ); | ||
| 474 | |||
| 475 | It causes the load routine to write additional information to the log. | ||
| 476 | Since even this can easily be too much, the default value for C<verbose> is | ||
| 477 | zero (terse logging). | ||
| 478 | |||
| 479 | The load routine also has a C<debug> mode which should be activated in | ||
| 480 | combination with C<verbose>. Debug mode is actually a function of the CELL | ||
| 481 | logger, and is activated like this: | ||
| 482 | |||
| 483 | $log->init( debug_mode => 1 ); | ||
| 484 | |||
| 485 | Ordinarily the logger suppresses all log messages below C<info> level | ||
| 486 | (i.e., C<debug> and C<trace>). When C<debug_mode> is activated, all | ||
| 487 | messages are logged, regardless of level. | ||
| 488 | |||
| 489 | |||
| 490 | =head2 Error handling | ||
| 491 | |||
| 492 | =head3 STATUS OBJECTS | ||
| 493 | |||
| 494 | The most frequent case will be a status code of "OK" with no message (shown | ||
| 495 | here with optional "payload", which is whatever the function is supposed to | ||
| 496 | return on success: | ||
| 497 | |||
| 498 | # all green | ||
| 499 | return App::CELL::Status->new( level => 'OK', | ||
| 500 | payload => $my_return_value, | ||
| 501 | ); | ||
| 502 | |||
| 503 | To ensure this is as simple as possible in cases when no return value | ||
| 504 | (other than the simple fact of an OK status) is needed, we provide a | ||
| 505 | special constructor method: | ||
| 506 | |||
| 507 | # all green | ||
| 508 | return App::CELL::Status->ok; | ||
| 509 | |||
| 510 | In most other cases, we will want the status message to be linked to the | ||
| 511 | filename and line number where the C<new> method was called. If so, we call | ||
| 512 | the method like this: | ||
| 513 | |||
| 514 | # relative to me | ||
| 515 | App::CELL::Status->new( level => 'ERR', | ||
| 516 | code => 'CODE1', | ||
| 517 | args => [ 'foo', 'bar' ], | ||
| 518 | ); | ||
| 519 | |||
| 520 | It is also possible to report the caller's filename and line number: | ||
| 521 | |||
| 522 | # relative to my caller | ||
| 523 | App::CELL::Status->new( level => 'ERR', | ||
| 524 | code => 'CODE1', | ||
| 525 | args => [ 'foo', 'bar' ], | ||
| 526 | caller => [ CORE::caller() ], | ||
| 527 | ); | ||
| 528 | |||
| 529 | It is also possible to pass a message object in lieu of C<code> and | ||
| 530 | C<msg_args> (this could be useful if we already have an appropriate message | ||
| 531 | on hand): | ||
| 532 | |||
| 533 | # with pre-existing message object | ||
| 534 | App::CELL::Status->new( level => 'ERR', | ||
| 535 | msg_obj => $my_msg; | ||
| 536 | ); | ||
| 537 | |||
| 538 | Permitted levels are listed in the C<@permitted_levels> package | ||
| 539 | variable in C<App::CELL::Log>. | ||
| 540 | |||
| 541 | |||
| 542 | =head2 Localization | ||
| 543 | |||
| 544 | =head3 Introduction | ||
| 545 | |||
| 546 | To an application programmer, localization may seem like a daunting | ||
| 547 | proposition, and All strings the application displays to users must be replaced | ||
| 548 | by variable names. Then you have to figure out where to put all the | ||
| 549 | strings, translate them into multiple languages, write a library (or find | ||
| 550 | an existing one) to display the right string in the right language at the | ||
| 551 | right time and place. What is more, the application must be configurable, | ||
| 552 | so the language can be changed by the user or the site administrator. | ||
| 553 | |||
| 554 | All of this is a lot of work, particularly for already existing, | ||
| 555 | non-localized applications, but even for new applications designed from the | ||
| 556 | start to be localizable. | ||
| 557 | |||
| 558 | App::CELL's objective is to provide a simple, straightforward way | ||
| 559 | to write and maintain localizable applications in Perl. Notice the key word | ||
| 560 | "localizable" -- the application may not, and most likely will not, be | ||
| 561 | localized in the initial stages of development, but that is the time when | ||
| 562 | localization-related design decisions need to be made. App::CELL tries to | ||
| 563 | take some of the guesswork out of those decisions. | ||
| 564 | |||
| 565 | Later, when it really is time for the application to be translated | ||
| 566 | into one or more additional languages, this becomes a relatively simple | ||
| 567 | matter of translating a bunch of text strings that are grouped together in | ||
| 568 | one or more configuration files with syntax so trivial that no technical | ||
| 569 | expertise is needed to work with them. (Often, the person translating the | ||
| 570 | application is not herself technically inclined.) | ||
| 571 | |||
| 572 | =head2 Localization with App::CELL | ||
| 573 | |||
| 574 | All strings that may potentially need be localized (even if we don't have | ||
| 575 | them translated into other languages yet) are placed in message files under | ||
| 576 | the site configuration directory. In order to be found and parsed by | ||
| 577 | App::CELL, message files must meet some basic conditions: | ||
| 578 | |||
| 579 | =over | ||
| 580 | |||
| 581 | =item 1. file name format: C<AppName_Message_lang.conf> | ||
| 582 | |||
| 583 | =item 2. file location: anywhere under the site configuration directory | ||
| 584 | |||
| 585 | =item 3. file contents: must be parsable | ||
| 586 | |||
| 587 | =back | ||
| 588 | |||
| 589 | =head3 Format of message file names | ||
| 590 | |||
| 591 | At initialization time, App::CELL walks the site configuration directory | ||
| 592 | tree looking for filenames that meet certain regular expressions. The | ||
| 593 | regular expression for message files is: | ||
| 594 | |||
| 595 | ^.+_Message(_[^_]+){0,1}.conf$ | ||
| 596 | |||
| 597 | In less-precise human terms, this means that the initialization routine | ||
| 598 | looks for filenames consisting of at least three, but possibly four, | ||
| 599 | components: | ||
| 600 | |||
| 601 | =over | ||
| 602 | |||
| 603 | =item 1. the application name (this can be anything) | ||
| 604 | |||
| 605 | =item 2. followed by C<_Message> | ||
| 606 | |||
| 607 | =item 3. optionally followed by C<_languagetag> where "languagetag" is a | ||
| 608 | language tag (see L</..link..> for details) | ||
| 609 | |||
| 610 | =item 4. ending in C<.conf> | ||
| 611 | |||
| 612 | =back | ||
| 613 | |||
| 614 | Examples: | ||
| 615 | |||
| 616 | CELL_Message.conf | ||
| 617 | CELL_Message_en.conf | ||
| 618 | CELL_Message_cs-CZ.conf | ||
| 619 | DifferentApplication_Message.conf | ||
| 620 | |||
| 621 | |||
| 622 | =head3 Location of message files | ||
| 623 | |||
| 624 | As noted above, message files will be found as long as they are readable | ||
| 625 | and located anywhere under the base site configuration directory. For | ||
| 626 | details on how this base site configuration directory is searched for and | ||
| 627 | determined, see L</..link..>. | ||
| 628 | |||
| 629 | |||
| 630 | =head3 How message files are parsed | ||
| 631 | |||
| 632 | Message files are parsed line-by-line. The parser routine is | ||
| 633 | C<parse_message_file> in the C<CELL::Load> module. Lines beginning with a | ||
| 634 | hash sign ('#') are ignored. The remaining lines are divided into | ||
| 635 | "stanzas", which must be separated by one or more blank lines. | ||
| 636 | |||
| 637 | Stanzas are interpreted as follows: the first line of the stanza should | ||
| 638 | contain a message code, which is simply a string. Any legal Perl scalar | ||
| 639 | value can be used, as long as it doesn't contain white space. CELL itself | ||
| 640 | uses ALL_CAPS strings starting with C<CELL_>. | ||
| 641 | |||
| 642 | The remaining lines of the stanza are assumed to be the message text. Two | ||
| 643 | caveats here: | ||
| 644 | |||
| 645 | =over | ||
| 646 | |||
| 647 | =item 1. In the configuration file, message text strings can be written on | ||
| 648 | multiple lines | ||
| 649 | |||
| 650 | =item 2. However, this is intended purely as a convenience for the | ||
| 651 | application programmer. When C<parse_message_file> encounters multiple | ||
| 652 | lines of text, it simply concatenated them together to form a single, long | ||
| 653 | string. | ||
| 654 | |||
| 655 | =back | ||
| 656 | |||
| 657 | For details, see the C<parse_message_file> function in C<App::CELL::Load>, | ||
| 658 | as well as App::CELL's own message file(s) in C<config/CELL> directory of | ||
| 659 | the App::CELL distro. | ||
| 660 | |||
| 661 | =head2 How the language is determined | ||
| 662 | |||
| 663 | Internally, each message text string is stored along with a language tag, which | ||
| 664 | defines which language the message text is written in. The language tag is | ||
| 665 | derived from the filename using a regular expression like this one: | ||
| 666 | |||
| 667 | _Message_([^_]+).conf$ | ||
| 668 | |||
| 669 | (The part in parentheses signifies the part between C<_Message_> and | ||
| 670 | C<.conf> -- this is stored in the C<language> attribute of the message | ||
| 671 | object.) | ||
| 672 | |||
| 673 | No sanity checks are conducted on the language tag. Whatever string | ||
| 674 | the regular expression produces becomes the language tag for all | ||
| 675 | messages in that file. If no language tag is found, CELL first looks for a | ||
| 676 | config parameter called C<CELL_DEFAULT_LANGUAGE> and, failing that, the | ||
| 677 | hard-coded fallback value is C<en>. | ||
| 678 | |||
| 679 | I'll repeat that, since it's important: CELL assumes that the message | ||
| 680 | file names contain the relevant language tag. If the message | ||
| 681 | file name is C<MyApp_Message_foo-bar.conf>, then CELL will tag all | ||
| 682 | messages in that file as being in the C<foo-bar> language. Message files | ||
| 683 | can also be named like this: C<MyApp_Message.conf>, i.e. without a language | ||
| 684 | tag. In this case, CELL will attempt to determine the default language from | ||
| 685 | a site configuration parameter (C<CELL_DEFAULT_LANGUAGE>). If this | ||
| 686 | parameter is not set, then CELL will give up and assume that all message | ||
| 687 | text strings are in English (language tag C<en> -- CELL's | ||
| 688 | author's native tongue). | ||
| 689 | |||
| 690 | =head2 Language tags in general | ||
| 691 | |||
| 692 | See the W3C's "Language tags in HTML and XML" white paper for a | ||
| 693 | detailed explanation of language tags: | ||
| 694 | |||
| 695 | L<http://www.w3.org/International/articles/language-tags/> | ||
| 696 | |||
| 697 | And see here for list of all language tags: | ||
| 698 | |||
| 699 | L<http://www.langtag.net/registries/lsr-language.txt> | ||
| 700 | |||
| 701 | Note that you should use hyphens, and not underscores, to separate | ||
| 702 | components within the language tag, i.e.: | ||
| 703 | |||
| 704 | MyApp_Message_cs-CZ.conf # correct | ||
| 705 | MyApp_Message_cs_CZ.conf # WRONG!! | ||
| 706 | |||
| 707 | Non-ASCII characters in config/message file names: may or may not work. | ||
| 708 | Better to avoid them. | ||
| 709 | |||
| 710 | =head2 Normal usage | ||
| 711 | |||
| 712 | In normal usage, the programmer adds messages to the respective | ||
| 713 | message files. After CELL initialization, these messages (or, more | ||
| 714 | precisely, message code-language pairs) will be available to the | ||
| 715 | programmer to use, either directly via CELL::Message->new or | ||
| 716 | indirectly as status codes. | ||
| 717 | |||
| 718 | If a message code has text strings in multiple languages, these language | ||
| 719 | variants can be obtained by specifying the C<lang> parameter to | ||
| 720 | CELL::Message->new. If the C<lang> parameter is not specified, CELL will | ||
| 721 | always try to use the default language (C<CELL_DEF_LANG> or English if | ||
| 722 | that parameter has not been set). | ||
| 723 | |||
| 724 | |||
| 725 | =head2 Logging | ||
| 726 | |||
| 727 | CELL's logging facility is based on L<Log::Any>. In practice, this means | ||
| 728 | that L<App::CELL::Log> is simply a wrapper around this useful module. To | ||
| 729 | use it, one imports the L<Log::Any> singleton via L<App::CELL> like | ||
| 730 | this: | ||
| 731 | |||
| 732 | use App::CELL qw( $log ); | ||
| 733 | |||
| 734 | Since this I<is> the L<Log::Any> singleton, all L<Log::Any> methods can be | ||
| 735 | used with it. CELL provides some conveniences, but they are optional. | ||
| 736 | Actually, if the developer does not intend to use any of CELL's | ||
| 737 | conveniences, there is no reason to import it through L<App::CELL> at all | ||
| 738 | and one can use L<Log::Any> directly. In this case, CELL's log messages | ||
| 739 | will go to the same log as the application's provided the L<Log::Any> | ||
| 740 | category is the same as the CELL C<appname>. | ||
| 741 | |||
| 742 | See L</Verbose and debug mode> for a description of how to increase logging | ||
| 743 | verbosity of the load routine. | ||
| 744 | |||
| 745 | |||
| 746 | =head1 CAVEATS | ||
| 747 | |||
| 748 | |||
| 749 | =head2 Internal parameters | ||
| 750 | |||
| 751 | L<App::CELL> stores its own parameters (mostly meta and core, but also one | ||
| 752 | site param) in a separate directory, but when loaded they end up in the | ||
| 753 | same namespaces as the application's meta, core, and site parameters. | ||
| 754 | The names of these internal parameters are always prefixed with C<CELL_>. | ||
| 755 | |||
| 756 | Therefore, the application programmer should avoid using parameters | ||
| 757 | starting with C<CELL_>. | ||
| 758 | |||
| 759 | |||
| 760 | =head2 Mutable and immutable parameters | ||
| 761 | |||
| 762 | It is important to realize that, although core parameters can be overridden | ||
| 763 | by site parameters, internally the values of both are immutable. Although | ||
| 764 | it is possible to change them by cheating, the 'set' method of C<$core> and | ||
| 765 | C<$site> will refuse to change the value of an existing core/site parameter. | ||
| 766 | |||
| 767 | Therefore, use C<$meta> to store mutable values. | ||
| 768 | |||
| 769 | |||
| 770 | =head2 Taint mode | ||
| 771 | |||
| 772 | Since it imports configuration data at runtime from files supplied by the | ||
| 773 | user, L<App::CELL> should not be run under taint mode. The C<< load >> | ||
| 774 | routine checks this and will refuse to do anything if running with C<-T>. | ||
| 775 | |||
| 776 | To recapitulate: don't run L<App::CELL> in taint mode. | ||
| 777 | |||
| 778 | |||
| 779 | =head2 Installation issues with CELL internal sharedir | ||
| 780 | |||
| 781 | The easiest way to install L<App::CELL> is to use a package manager (e.g. | ||
| 782 | C<zypper>). Another way to install directly from CPAN using, e.g., | ||
| 783 | C<cpanm>). The former way installs to the C<vendor_perl> tree, while the | ||
| 784 | latter installs to the C<site_perl> tree. | ||
| 785 | |||
| 786 | If you install two different versions of L<App::CELL>, one via package | ||
| 787 | manager and another directly from CPAN, a conflict can arise, and it may be | ||
| 788 | necessary to examine CELL's log to determine which one is being used. | ||
| 789 | |||
| 790 | Even after running, e.g., C<cpanm -U App::CELL>, to uninstall from | ||
| 791 | C<site_perl>, I found that CELL's internal sharedir remained intact in the | ||
| 792 | C<site_perl> tree and had to be wiped manually. | ||
| 793 | |||
| 794 | As long as you always install either one way or other other (i.e. package | ||
| 795 | manager or direct from CPAN), you won't get bitten by this. | ||
| 796 | |||
| 797 | |||
| 798 | |||
| 799 | =head1 COMPONENTS | ||
| 800 | |||
| 801 | |||
| 802 | =head2 L<App::CELL> | ||
| 803 | |||
| 804 | This top-level module exports a singleton, C<$CELL>, which is all the | ||
| 805 | application programmer needs to gain access to the CELL's key functions. | ||
| 806 | |||
| 807 | |||
| 808 | =head2 C<App::CELL::Config> | ||
| 809 | |||
| 810 | This module provides CELL's Configuration functionality. | ||
| 811 | |||
| 812 | |||
| 813 | =head2 C<App::CELL::Guide> | ||
| 814 | |||
| 815 | This guide. | ||
| 816 | |||
| 817 | |||
| 818 | =head2 C<App::CELL::Load> | ||
| 819 | |||
| 820 | This module hides all the complexity of loading messages and config params | ||
| 821 | from files in two directories: (1) the App::CELL distro sharedir containing | ||
| 822 | App::CELL's own configuration, and (2) the site configuration directory, if | ||
| 823 | present. | ||
| 824 | |||
| 825 | |||
| 826 | =head2 C<App::CELL::Log> | ||
| 827 | |||
| 828 | Logging is accomplished by using and extending L<Log::Any>. | ||
| 829 | |||
| 830 | |||
| 831 | =head2 C<App::CELL::Message> | ||
| 832 | |||
| 833 | Localization is on the wish-list of many software projects. With CELL, | ||
| 834 | the programmer can easily design and write my application to be localizable | ||
| 835 | from the very beginning, without having to invest much effort. | ||
| 836 | |||
| 837 | |||
| 838 | =head2 C<App::CELL::Status> | ||
| 839 | |||
| 840 | Provides CELL's error-handling functionality. Since status objects inherit | ||
| 841 | from message objects, the application programmer can instruct CELL to | ||
| 842 | generate localized status messages (errors, warnings, notices) if desired. | ||
| 843 | |||
| 844 | |||
| 845 | =head2 C<App::CELL::Test> | ||
| 846 | |||
| 847 | Some routines used by CELL's test suite. | ||
| 848 | |||
| 849 | |||
| 850 | =head2 C<App::CELL::Util> | ||
| 851 | |||
| 852 | Some generalized utility routines. | ||
| 853 | |||
| 854 | |||
| 855 | |||
| 856 | =head1 RATIONALE | ||
| 857 | |||
| 858 | In the author's experience, applications written for "users" (however that | ||
| 859 | term may be defined) frequently need to: | ||
| 860 | |||
| 861 | =over | ||
| 862 | |||
| 863 | =item 1. be configurable by the user or site administrator | ||
| 864 | |||
| 865 | =item 2. handle errors robustly, without hangs and crashes | ||
| 866 | |||
| 867 | =item 3. potentially display messages in various languages | ||
| 868 | |||
| 869 | =item 4. log various types of messages to syslog | ||
| 870 | |||
| 871 | =back | ||
| 872 | |||
| 873 | Since these basic functions seem to work well together, CELL is designed to | ||
| 874 | provide them in an integrated, well-documented, straightforward, and | ||
| 875 | reusable package. | ||
| 876 | |||
| 877 | =cut | ||
| 878 | |||
| 879 | 1 | 1; | |
| 880 | |||