Config-Vars

 view release on metacpan or  search on metacpan

Vars.pm  view on Meta::CPAN


 var $variable_name = $some_value;
 var @variable_name = @some_values;
 var %variable_name = %some_values;

This declares a variable.  The variable is initialized and its name is
appended to @EXPORT_OK.

=item ro

 ro $variable_name = $some_value;
 ro @variable_name = @some_values;
 ro %variable_name = %some_values;

This declares a readonly variable, if you have the Readonly module
installed.  If you don't, a warning will be printed to STDERR and the
module will continue as if you had used C<var>.

If you don't want the warning message to be displayed, specify "nowarn"
on the "use Config::Vars" line; see USE above.

=back

=head1 EXAMPLE

 # ==== CONFIG FILE MyConfig.pm ====
 use strict;
 package MyConfig;
 use Config::Vars;

 ro $db_user    = 'my_db_app';      # database application username
 ro $db_pw      = $database_user;   # password (often the same as username)
 ro $db_connect = 'dbi:mysql';      # database connect string
 var $dbh;                          # global database handle
 # ==== END CONFIG FILE ====

 # ==== SOME LIBRARY Lib.pm ====
 use strict;
 package Lib;
 use MyConfig qw($db_user $db_pw $db_connect $dbh);

 sub connect
 {
     $dbh = DBI->connect($db_connect, $db_user, $db_pw);
     die "Can't connect" unless $dbh;
 }
 # ==== END LIBRARY Lib.pm ====

 # ==== MAIN PROGRAM ====
 use strict;
 use MyConfig qw($dbh);    # Configuration variables for this app
 use Lib;         # Utility library for this app

 Lib::connect;    # Call utility function

 # Now $dbh is available.  Lib.pm and main share the same $dbh
 my $sth = $dbh->prepare(...)
 # ==== END MAIN PROGRAM ====


=head1 BUGS AND LIMITATIONS

=over 1

=item *

All errors are reported as being from the "use Config::Vars" line of
the calling module, instead of the line where the error actually
occurred.

=item *

'var' and 'ro' are only recognized if they are the first non-whitespace
characters on the line.  Thus the following won't work:

 var $foo=7; var $bar=8;

=item *

The first non-whitespace character after the variable name on a 'var'
or 'ro' line must be a semicolon or an equals sign.  So the following
would not be accepted:

 var %is_bird        # This comment messes things up.
     = ( vulture => 1, tiger => 0 );

=back

=head1 EXPORTS

The following symbols appear to be exported, but they're actually sort
of magical (because they're implemented via source filter).

 var
 ro

=head1 REQUIREMENTS

 Carp
 Filter::Simple
 Readonly      (if you want the 'ro' directive to work)

=head1 SEE ALSO

The Exporter module, especially the bits about @EXPORT_OK and @EXPORT.

The Camel book, esp. the bits about @ISA and C<use vars>.

The Reaodnly module.

=head1 AUTHOR / COPYRIGHT

Eric J. Roode, roode@cpan.org

Copyright (c) 2003 by Eric J. Roode. All Rights Reserved.  This module
is free software; you can redistribute it and/or modify it under the
same terms as Perl itself.

If you have suggestions for improvement, please drop me a line.  If
you make improvements to this software, I ask that you please send me
a copy of your changes. Thanks.



( run in 1.527 second using v1.01-cache-2.11-cpan-98e64b0badf )