Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Cannot find input arguments to program called with "system"

by pktrain (Acolyte)
on Jul 06, 2011 at 05:52 UTC ( #912913=perlquestion: print w/replies, xml ) Need Help??
pktrain has asked for the wisdom of the Perl Monks concerning the following question:


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: % /path/program /inpath/ /outpath/output.out converting: /inpath/ to /outpath/output.out %

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/nid/cdf/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: % /inpath/ converting: /inpath/ to /outpath/output.out unable to find /inpath/ 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. Your advice is greatly appreciated!

Replies are listed 'Best First'.
Re: Cannot find input arguments to program called with "system"
by philipbailey (Chaplain) on Jul 06, 2011 at 07:07 UTC

    You should chomp $file. It otherwise contains a newline character at the end.

      Thank you! Using chomp() was the solution...
Re: Cannot find input arguments to program called with "system"
by Khen1950fx (Canon) on Jul 06, 2011 at 07:07 UTC
    Your biggest problem was not declaring your lexical variables with 'my'. I fixed that and made a few changes.
    #!/usr/bin/perl use strict; use warnings; my(@test_dates) = '20110414'; my $listfile = '/root/Desktop/Log.txt'; foreach my $date(@test_dates) { my $obs_file_dir = 'inpath'; my $pred_file_dir = 'outpath'; mkdir "$pred_file_dir", 0755 unless -d "$pred_file_dir"; my $obs_file_list = $listfile; open( DIR, '<', $obs_file_list ) or die $!; my(@obs_files) = <DIR>; close(DIR); foreach my $file(@obs_files) { $file =~ s/(\*)//g; my $infile = $obs_file_dir . $file; my $outfile = $pred_file_dir . $file; $outfile =~ s/nid/cdf/g; print $infile . "\n"; } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://912913]
Approved by planetscape
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2018-05-21 17:43 GMT
Find Nodes?
    Voting Booth?