http://qs321.pair.com?node_id=178019

Mahoota has asked for the wisdom of the Perl Monks concerning the following question:

I have created a perl program that asks for the name of an input filename; then an output file; and then reads in these filenames and opens the relevant files using STDIN (performs operations on the data from the infile and writes results to the outfile).

I now need a second program that will run this program (>perl progname), then enter the filenames when prompted at the command line.

I can't seem to get open() to do this, and dont know if there is a way of editing the first program to read in filenames directly from the executable line (>perl progname filename) (then I could simply use system() in the second program..

Any ideas greatfully recieved.

Replies are listed 'Best First'.
Re: Writing to command line when prompted
by Aristotle (Chancellor) on Jun 28, 2002 at 15:15 UTC
    Your question has mostly been answered, but I think a short abstract on all the available options is in order.
    1. Commandline parameters are available to the script via the @ARGV array. You can also modify this array at runtime to suit your needs, if desired.
    2. The Getopt:: modules are great help if you want to pass more parameters on the commandline than just filenames, @ARGV mangling can be very tiresome. These modules do the job for you with a lot of flexibly and robustness.
    3. If you use the plain diamond operator <>, Perl will read data from STDIN if no command line parameters have been passed, or use the scalars in @ARGV as the names of the files to read. It will read all of the files given there without you having to explicitly open/close any of them. Note that you can manually munge @ARGV and Perl will willing pick up the modified list of filenames.
    4. Unless you need to output to several files, I would suggest you print to STDOUT. To capture the output to a file, the shell offers redirection, so there is no extra work involved. But if you call that script from another one, its output will be much easier to work with if it prints to STDOUT instead of a file.
    Hope this helps. (Although I'm sure it's not entirely complete. :))

    Makeshifts last the longest.

Re: Writing to command line when prompted
by sschneid (Deacon) on Jun 28, 2002 at 14:42 UTC
    Options entered on the command line are stored in the @ARGV array, so you could hypothetically access the two filenames perl script.pl file_in file_out with $ARGV[0] and $ARGV[1]. If you are using more than just the two filenames (or plan on implementing switched options, etc) I would highly suggest taking a look at GetOpt::Long.

    scott.
Re: Writing to command line when prompted
by robobunny (Friar) on Jun 28, 2002 at 14:40 UTC
    since you created the first program, is there a reason you can't modify it to optionally take its arguments on the command line, and only prompt for them if they are not supplied in ARGV? then you can easily call it without doing any extra work, but you still get the prompt if you run it manually.
Re: Writing to command line when prompted
by amphiplex (Monk) on Jun 28, 2002 at 14:42 UTC
    I am not sure I completety understand what you are trying to do, but giving filenames on the commandline is quite easy. You could simply do:
    my ($filename_in, $filename_out) = @ARGV;
    which gives you the two filenames in $filename_in and $filename_out.

    ------ kurt
      Though I am sure that the better approach is to change the first script, here is a (very) simple solution to feed the two filenames to the script.

      I assume your first script looks something like this:
      use strict; my ($filename_in, $filename_out); while ($filename_in eq "") { print "IN file: "; $filename_in = <>; chomp $filename_in; } while ($filename_out eq "") { print "OUT file: "; $filename_out = <>; chomp $filename_out; } print "IN: [$filename_in]\n"; print "OUT: [$filename_out]\n";

      Then you could use this script to execute the first script and feed the two filenames to it:
      use strict; my $command = "./script1.pl"; my ($filename_in, $filename_out) = qw(infile outfile); system ("echo \"$filename_in\n$filename_out\n\" | $command");
      Assuming, of course, you are on a Un*x machine, echo is in your path,the first script is called "script1.pl", etc.

      ------ kurt