package Configuration; =head1 NAME Configuration -- An object that keeps track of the program configuration. =head1 SYNOPSIS my $configuration = Configuration->new; my $boolean = Configuration->save; Object hash entries: project_array array of strings =head1 DESCRIPTION This object keeps track of the program configuration. =head1 VERSION 0.001 (last update: 6/30/04) =head1 AUTHOR Chet Langin, clangin@siu.edu SIU Plant Biotechnology and Genomics Core-facility =head1 BUGS None known. =head1 SEE ALSO extropy ExtropyConstants ExtropyUtils Extropy::MenuMain =head1 COPYRIGHT Copyright 2004, Chet Langin, All Rights Reserved. This program is free software. You may copy or redistribute it under the same terms as Perl itself. =head1 METHODS The remainder of this document describes the methods available to the programmer. =cut # load the pragmas use warnings; use strict; # load other modules use ExtropyConstants; use ExtropyUtils; # package variables my $kbd_input_line = ""; # my $project_name = ""; # ******************************** new ****************************** =head2 new Configuration->new; Creates an instance of the Configuration object. =cut # --------------------------------------------------------------------- sub new { # instantiate the oject my $self = {}; bless $self; my $defined = TRUE; # see if a configuation file exists my $config_file_exists = TRUE; my $config_file_created = TRUE; open(CONF_FILE, "<", "extropy.cfg") or $config_file_exists = FALSE; if($config_file_exists) { my @project_array = (); # read the configuration file my @file_array = ; for(my $lcv = 0; $lcv < scalar(@file_array); $lcv++) { my $current_line = $file_array[$lcv]; next if($current_line =~ /^#/); chomp $current_line; next if($current_line =~ /^\s*$/); my @line_array = split /=/, $current_line; if($line_array[0] =~ /project/) { push(@{$self->{project_array}}, $line_array[1]); } # if } # for close CONF_FILE; } # if else { # config file does not exist # try to create a configuration file $self->{project_exists} = FALSE; @{$self->{project_array}} = []; message_start; message("The \"extropy.cfg\" configuration file could not be opened:"); message(" $!."); message("Creating an \"extropy.cfg\" file..."); open(CONF_FILE, ">", "extropy.cfg") or $config_file_created = FALSE; if($config_file_created) { message("\"extropy.cfg\" file created."); close CONF_FILE; } # if else { message("extropy.cfg file could not be created: $!"); message("extropy cannot continue without an extropy.cfg file."); message("extropy is terminating..."); blank_line; $defined = FALSE; } # else } # if # return values if($defined) { $self; } # if else { undef; } # else } # new() # ******************************** save ***************************** =head2 save Configuration->save; Saves the configuration to the extropy.cfg file. =cut # --------------------------------------------------------------------- sub save { my $self = shift; my $project = shift; my $error = FALSE; open(CFG_FILE, ">", "extropy.cfg") or $error = TRUE; if($error) { message("Could not save to extropy.cfg"); message("$!"); } # if else { print CFG_FILE "# extropy.cfg\n"; print CFG_FILE "# This is the configuration file for the program extropy.\n"; print CFG_FILE "# This file is created automatically by extropy.\n"; print CFG_FILE "# Any changes made directly to this file may be lost.\n"; my $date = localtime; print CFG_FILE "# Last update: $date\n\n"; foreach my $project (@{$self->{project_array}}) { print CFG_FILE "project=$project\n"; } # foreach message("The program configuration was saved to extropy.cfg"); } # else close CFG_FILE; $project->save; } # save 1