0

I'm writing a Perl script to run an external program on every file in a directory. This program converts files from one format to another. Here's the deal...

When I run the program from the command line, everything works as it should:

computer.name % /path/program /inpath/input.in /outpath/output.out
converting: /inpath/input.in to /outpath/output.out

computer.name %

Here's the code I wrote to convert all files in a directory (listed in "file_list.txt"):

#!/usr/bin/perl -w

use warnings;
use diagnostics;
use FileHandle;
use File::Copy;

# Set simulation parameters and directories
@test_dates = ("20110414");
$listfile = "file_list.txt";
$execname = "/path/program";

foreach $date (@test_dates)
{
    # Set/make directories
    $obs_file_dir = "inpath";
    $pred_file_dir = "outpath";
    mkdir "$pred_file_dir", 0755 unless -d "$pred_file_dir";

    # Read input file names to array
    $obs_file_list = $obs_file_dir . $listfile;
    open(DIR, $obs_file_list) or die "Could not open file!";
    @obs_files = <DIR>;
    close(DIR);

    # Convert and save files
    foreach $file (@obs_files)
    {    
        $file =~ s/(\*)//g;
        $infile = $obs_file_dir . $file;
        $outfile = $pred_file_dir . $file;
        $outfile =~ s/in/out/g;
        print $infile . "\n";
        @arg_list = ($execname, $infile, $outfile);
        system(@arg_list);
    }
}

The output shows me the following error for every file in the list:

computer.name % perl_script_name.pl
/inpath/input.in
converting: /inpath/input.in to /outpath/output.out

unable to find /inpath/input.in
stat status=-1
error while processing the product

I verified every file is in the proper place and have no idea why I am getting this error. Why can't the files be found? When I manually pass the arguments using the command line, no problem. When I pass the arguments through a variable via a system call, they can't be found even though the path and file names are correct.

Your advice is greatly appreciated!

2
  • There's nothing obviously wrong.... are you positive that the command it is running is correct? I would output the result of join(" ", @arg_list) before you run system, and see if pasting that into your shell has the same effect. Commented Jul 6, 2011 at 10:47
  • Is /path/program a script? If so, that system call invoke your $SHELL to perform the command. If that is the case, does your .cshrc / .tcshrc / .bashrc make a new shell chdir to your home directory? Add a pwd command to /path/program so that you can see the current working directory. Commented Jul 6, 2011 at 12:57

1 Answer 1

1

Your list of files (@obs_files) comes from reading in a file via @obs_files = <DIR>;

When you do that, each element of array will be a line from a file (e.g. directory listing), with the line being terminated by a newline character.

Before using it, you need to remove the newline character via chomp($file).

Please note that s/(\*)//g; does NOT remove that trailing newline!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.