#!perl # # The copyright notice and plain old documentation (POD) # are at the end of this file. # package File::Maker; use strict; use warnings; use warnings::register; use vars qw($VERSION $DATE); $VERSION = '0.05'; $DATE = '2004/05/13'; use vars qw(@ISA @EXPORT_OK); use Exporter; @ISA = qw(Exporter); @EXPORT_OK = qw(load_db); use File::Package; use Tie::Layers 0.02; use Tie::Form; use Data::Startup; use Cwd; use File::Spec; ####### # # Handle the options # sub new { return undef unless @_; my ($class, @options) = @_; $class = ref($class) if ref($class); my $self = bless {}, $class; $self->{options} = Data::Startup->new(@options); $self; } ###### # # make_targets # sub make_targets { my ($self, $targets_h, @targets) = @_; $self->{options} = pop @targets if ref($targets[-1]) eq 'HASH'; my $formDB_pm = $self->{options}->{pm}; ######## # Determine the formDB program module to load # unless( $formDB_pm ) { warn "No formDB program module.\n"; return undef; } if( $self->{options}->{verbose} ) { print "~~~~~\nGenerating $formDB_pm " . (join ' ',@targets) . "\n"; } unless($self->{FormDB_PM} && $self->{FormDB_PM} eq $formDB_pm) { $self = $self->File::Maker::load_db($formDB_pm); unless(ref($self)) { warn "Cannot load $formDB_pm\n\t$self"; return undef; } } @targets = ('all') unless( @targets ); my $restore_dir = cwd(); my $success = 1; if( @targets ) { my ($macro, $value, $target); foreach $target (@targets) { if( $target =~ /=/ ) { ($macro,$value) = split /=/, $target; $self->{FormDB}->{$macro} = $value; next; } $self->{target} = $target; chdir $restore_dir; my $expanded_target = $targets_h->{$target}; unless( $expanded_target ) { $expanded_target = $targets_h->{__no_target__}; unless( $expanded_target ) { warn "No expanded_target.\n"; $success = undef; last; } } my (@args,$method,$result); foreach $target (@{$expanded_target}) { $method = $target; @args = (); if(ref($target) eq 'ARRAY') { ($method, @args) = @$target; } $result = $self->$method( @args ); if(ref($result) ) { $self = $result; } elsif ( !defined($result) ) { $success = undef; warn( "Target $method failed.\n" ); last; } } last unless( $success); } } chdir $restore_dir; print "\nFinished processing.\n" if $self->{options}->{verbose}; $success; } ##### # Use a Form type database file for Variables # sub load_db { my $self = ref($_[0]) ? shift : {}; my ($formDB_pm) = @_; ####### # Add any extra directories to the include path # my @restore_inc = @INC; unshift @INC, @{$self->{Load_INC}} if( $self->{Load_INC} ); ##### # load the FormDB program module # # Always look in the current directory first # unshift @INC, File::Spec->curdir(); my $error; $formDB_pm = ref($formDB_pm) if ref($formDB_pm); if($error = File::Package->load_package($formDB_pm)) { @INC = @restore_inc; return $error ; } #### # # no strict; my $data_handle = \*{"$formDB_pm" . '::DATA'}; use strict; my $position = tell($data_handle ); my $fh = $data_handle; my @data = <$fh>; seek($data_handle ,$position,0); tie *FORM, 'Tie::Form'; open FORM, '<', $data_handle ; my @fields =