<<

NAME

Web::Definitions - defines constants in use throughout the Web::Chain project.

SYNOPSIS

   use Web::Definitions qw($DF_DESTINATION_RULE);

   unless( $name =~ qr/$DF_DESTINATION_RULE/ ) {
     die "$name is not a well-formed DF node name";
   }

DESCRIPTION

This is a configuration file for the Web::Chain project (which is used for DF work).

The "constants" defined here are now in the form of upper-case variables (see STYLE discussion below).

EXPORT

This module uses a system that automatically adds candidates for export to the EXPORT_OK list. This system exports:

  1. all constants
  2. all *_RULE (or *_rule) variables (declared with 'our')
  3. all UPPERCASE variables (declared with 'our')

The :all tag can be used to import all exports from this file:

  use Web::Definitions qw(:all);

though this is not recommended.

It's better to just import the ones you need, chosen from the following:

DISCUSSION

STYLE

(1) Avoid creating a lot of built-up definitions to be exported. E.g. something like this isn't a good idea:

    our $DF_TOPNODE = $DF_LOC . '/' . $DF_TOPNODE_NAME . '.html'; 

Because code that uses this $DF_TOPNODE can't be tested very well. It will always access the live location $DF_LOC. Better to do this sort of build-up in the code that uses these definitions, where $DF_LOC can be replaced by a debug location on the fly.

(2) Use names like "*_pat" for strings that contain regular expressions, and "*_RULE" for actual regexp objects created with qr{}. Export all "*_RULE"s automatically.

(3) Actually, this module automatically exports all variables with names all in uppercase. Using uppercase variables as (pseudo) constants lessens the chance of collision with other variables. And it makes the exports visually resemble constants even though they're not.

CONSTANT IRRITATION

Perl constants have little going for them: their only real advantage is the compiler can optimize them to inlines.

Well, there's also the fact that they're constant and putting a value into that kind of straight-jacket might help prevent a shoot-yourself-in-the-foot problem, but unlike most such things in perl, there is no easy way to escape the straight-jacket later if you really need to. There is no: {no strict 'constants' ... }

It's an odd thought, but it isn't really unusual to want to temporarily change a "constant" e.g. over-ride a project-level $DEBUG flag, setting it temporarily just for the current module.

Also on the negative side, constants don't really interpolate, not even if you try fugly tricks like:

   print "My constant: &CONSTANT() \n";

Though, if you want to get even fuglier, you can do this:

   print "My constant: @{[ CONSTANT ]} \n";

Why use perl at all if you're going to, uh, constantly do things like this:

   print "My constant: " . CONSTANT . "\n";

So: I've stopped using (and exporting) constants.

SEE ALSO

Project Documentation

AUTHOR

Joseph Brenner, <doom@kzsu.stanford.edu>

COPYRIGHT AND LICENSE

Copyright (C) 2004 by Joseph Brenner

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.2 or, at your option, any later version of Perl 5 you may have available.

BUGS

None reported... yet.

<<