in reply to ActivePERL is the devil?
I think this is what you are trying to do:
Others have pointed out some of the problems with this script, system("cd d:") doesn't effect the rest of the progrem, etc. Others remain unnoted so far (recursive function call to answer(), use of & on function calls).
Here's my UNTESTED rough cut at a rewrite. Here's what I already know is wrong with it. I haven't enabled taint checking for the file names. I don't untaint the file names. There is almost certainly a better way to run those perl scripts against the target file. The subroutines need block comments describing what they do. Script paths are hardcoded in the body of the script and not in variables or constants at the top of the file. The temporary and base paths are specified in list form, which isn't too familiar to many people, I should really use File::Spec to parse normal paths. Finally it is UNTESTED code that features the dangerous unlink command. Despite all this, it may be a useful starting point for you.
use strict; use warnings; use diagnostics; use File::Copy; use File::Spec; use Cwd; use Shell 'perl'; my @TEMP_PATH = ( 'D:', 'perl' ); my @BASE_PATH = ( 'D:', 'tmp' ); my $base_path = File::Spec->catdir( @BASE_PATH ); my $file = get_file_to_process( $base_path ); my $orig_file = File::Spec->catfile( @BASE_PATH, $file); my $temp_file = File::Spec->catfile( @TEMP_PATH, $file); process_file( $orig_file, $temp_file ); if ( keep_changes() ) { # Copy the editted file over the original copy( $temp_file, $orig_file ); } # Cleanup unlink $temp_file; # ------------------------------------ sub print_listing { my $dir_path = shift; my $curdir = cwd; # Change scripts working directory to TEMP_PATH chdir( $dir_path ); # Get a list of *.txt files using glob. # You could also use opendir to do this. while ( my $filename = glob('*.txt') ) { print "$filename\n"; } chdir( $curdir ); } sub get_file_to_process { my $base_path = shift; print_listing( $base_path ); print "Enter File Name to be converted.\n"; print "File should be in this directory.\n"; my $file = <STDIN>; chomp $file; # Test for existence die "File $file does not exist\n" unless -e $file; } sub process_file { my $orig_file = shift; my $temp_file = shift; # Copy the file into a temporary location. copy( $orig_file, $temp_file ) or die "Unable to create temp file $temp_file - $!\n"; # Here we edit the temporary file # instead of using it as a backup. print "Staring Edits...1."; perl "/perl/macc1", $temp_file; sleep 2; print ",13.\n"; perl "/perl/macc13", $temp_file; print "Edits complete.\n"; } sub keep_changes { print "Do you wish to keep the edits?\n"; print "Enter Yes or No.\n"; my $response = <STDIN>; chomp $response; while (1) { if ( $response =~ /^y(es)?$/i ) { return 1; } elsif ( $response =~ /^no?$/i ) { return; } else { print <<'END_MSG'; ===================================== You Have Entered and Incorrect Option Valid Options are [Y/N] ===================================== END_MSG } } }
TGI says moo
|
|---|