Maureen E Fischer wrote:

> code for me, but since this is considered its own "block", I cant't
> refer to the variables.  Should I be
> 
> going about this in a totally different way?

You want to create a class. OOP might sound scary, but its not.

# name this file DynamicConfig.pm and
# save it in ~/modules

package DynamicConfig;
use strict;

sub new {
  my($class) = shift();
  my($self) = bless( { }, $class );
  $self->_init( @_ );
  return($self);
}

sub _init {
  my($self, %params) = @_;

  $self->{defaultParam1} = 'Foo';
  $self->{defaultParam2} = 'Bar';
  $self->{defaultParam3} = 'Bazz';

  for my($key)( keys( %params ) ) {
    $self->{ $key } = $params{ $key };
  }

  return();
}

1;

## end module ##

You then use it like this. Call this program whatever you want:
#!/usr/bin/perl -w

use strict;

# this is the absolute path to the directory
# that DynamicConfig.pm is stored

use lib qw(/home/mydir/modules);
use DynamicConfig.pm;

# hard code defaults in the module.
# the arguments to new override defaults
# with matching keys, or adds parameters
# for keys that are not hard coded in the module

my($config) = DynamicConfig->new(
          defaultParam2 => 'Rab',
          anotherParam => '/etc/passwd'
);

# $config is an object that holds your configuration info.
# access them individually:

print $config->{defaultParam1}, "\n";
print $config->{anotherParam}, "\n";

# dump all the configuration info
# $config is just a hash reference

for my $key ( keys( %{$config} ) ) {

  print $key, ': ', $config->{ $key }, "\n";

}

__END__

[trwww@misa trwww]$ perl test.pl
Foo
/etc/passwd
defaultParam1: Foo
defaultParam2: Rab
defaultParam3: Bazz
defaultParam4: Bang
anotherParam: /etc/passwd

enjoy,

Todd W

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to