| 1 | package OpenInteract::Config::PerlFile; | ||
| 2 | |||
| 3 | # $Id: PerlFile.pm,v 1.6 2002/04/17 12:44:29 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Data::Dumper qw( Dumper ); | ||
| 7 | use OpenInteract::Config qw( _w DEBUG ); | ||
| 8 | |||
| 9 | @OpenInteract::Config::PerlFile::ISA = qw( OpenInteract::Config ); | ||
| 10 | $OpenInteract::Config::PerlFile::VERSION = sprintf("%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/); | ||
| 11 | |||
| 12 | sub valid_keys { | ||
| 13 | my ( $self ) = @_; | ||
| 14 | return grep ! /^_/, keys %{ $self }; | ||
| 15 | } | ||
| 16 | |||
| 17 | |||
| 18 | sub read_config { | ||
| 19 | my ( $class, $filename ) = @_; | ||
| 20 | unless ( -f $filename ) { | ||
| 21 | my $msg = 'Cannot read configuration file!'; | ||
| 22 | my $system_msg = "No valid filename ($filename) for reading configuration information!"; | ||
| 23 | OpenInteract::Error->set({ user_msg => $msg, | ||
| 24 | type => 'config', | ||
| 25 | system_msg => $system_msg, | ||
| 26 | method => 'read_config', | ||
| 27 | extra => { filename => $filename } }); | ||
| 28 | die $msg; | ||
| 29 | } | ||
| 30 | DEBUG && _w( 1, "Reading configuration from: ($filename)" ); | ||
| 31 | eval { open( CONF, "$filename" ) || die $! }; | ||
| 32 | if ( $@ ) { | ||
| 33 | my $msg = 'Cannot read configuration file!'; | ||
| 34 | OpenInteract::Error->set({ user_msg => $msg, | ||
| 35 | type => 'config', | ||
| 36 | system_msg => "Error trying to open $filename: $@", | ||
| 37 | method => 'read_config', | ||
| 38 | extra => { filename => $filename } }); | ||
| 39 | die $msg; | ||
| 40 | } | ||
| 41 | local $/ = undef; | ||
| 42 | my $config = <CONF>; | ||
| 43 | close( CONF ); | ||
| 44 | my ( $data ); | ||
| 45 | eval $config; | ||
| 46 | if ( $@ ) { | ||
| 47 | my $msg = 'Cannot read configuration file!'; | ||
| 48 | OpenInteract::Error->set({ user_msg => $msg, | ||
| 49 | type => 'config', | ||
| 50 | system_msg => "Error trying to eval $filename: $@", | ||
| 51 | method => 'read_config', | ||
| 52 | extra => { config => $config } }); | ||
| 53 | die $msg; | ||
| 54 | } | ||
| 55 | DEBUG && _w( 1, "Structure of config:\n", Dumper( $data ) ); | ||
| 56 | return $data; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | sub save_config { | ||
| 62 | my ( $self, $filename ) = @_; | ||
| 63 | $filename ||= join( '/', $self->get_dir( 'config' ), $self->{config_file} ); | ||
| 64 | unless ( -f $filename ) { | ||
| 65 | my $msg = 'Cannot read configuration file!'; | ||
| 66 | my $system_msg = "No valid filename ($filename) for saving configuration information!"; | ||
| 67 | OpenInteract::Error->set({ user_msg => $msg, | ||
| 68 | type => 'config', | ||
| 69 | system_msg => $system_msg, | ||
| 70 | method => 'save_config', | ||
| 71 | extra => { filename => $filename } }); | ||
| 72 | die $msg; | ||
| 73 | } | ||
| 74 | |||
| 75 | DEBUG && _w( 1, "Trying to save configuration to: ($filename)" ); | ||
| 76 | eval { open( CONF, "> $filename" ) || die $! }; | ||
| 77 | if ( $@ ) { | ||
| 78 | my $msg = 'Cannot read configuration file!'; | ||
| 79 | OpenInteract::Error->set({ user_msg => $msg, | ||
| 80 | type => 'config', | ||
| 81 | system_msg => "Error trying to write $filename: $@", | ||
| 82 | method => 'save_config', | ||
| 83 | extra => { filename => $filename } }); | ||
| 84 | die $msg; | ||
| 85 | } | ||
| 86 | my %data = %{ $self }; | ||
| 87 | my $config = Data::Dumper->Dump( [ \%data ], [ 'data' ] ); | ||
| 88 | print CONF $config; | ||
| 89 | close( CONF ); | ||
| 90 | return $self; | ||
| 91 | } | ||
| 92 | |||
| 93 | 1; | ||
| 94 | |||
| 95 | __END__ |