Perl script calling another perl scripts which requests input from command line.

jakeyo
jakeyo used Ask the Experts™
on
Hi-
    I have a Perl script which I run from the command line. When running from the command line, the Perl script (processA2Mod) requests 5 pieces of information from the user via command line questions and user input.
    I'm now trying to call this script from another perl script. This second Perl script (Phase1) will call processA2Mod with answers to the user questions. How can I do this??
    Thanks in advance for any help on this matter.
Regards,
E.J. Yoerger (jakeyo)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Here's a little example for you.

test1.pl
#!/usr/bin/perl
#

my $val = qx{./test2.pl arg1 arg2 arg3 arg4 arg5};

print $val, "\n";

Open in new window


test2.pl
#!/usr/bin/perl
#
#
print "$ARGV[0],$ARGV[1],$ARGV[2],$ARGV[3],$ARGV[4]\n";

Open in new window

Author

Commented:
I'll try your solution and let you know if it works.
Regards,
E.J. Yoerger

Author

Commented:
I tried your code and I received the same error that I'm getting with my code:
use of uninitialized value in pattern match (m//) at ./processA2Mod line 246.

My code by the way is

$cmd="";
$cmd = `processA2Mod $raw $work $caldir $calfile $site_buoy`;
print $cmd;

For some reason, processA2Mod is not getting the correct value for the variables. I've doubled checked the values in the above arguments and they are correct. But it's not finding something, I don't know what.

Any further help would be appreciated. Thanks.
E.J.Yoerger
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

You don't need the first statement
$cmd = "";

Try commenting the third statement and see if the error goes away.  It could be that processA2Mod is not printing any output.

Author

Commented:
I still get the same error with print $cmd, "\n"; commented out. But I would still need the print  because when my program (processA2Mod) is run from the command line it requires 5 arguments. After these arguments are input it then creates several (6) files of output information and writes to the screen as well giving information of what files are being written as well as other sundry data.

Any other ideas??
Regards,
E.J. Yoerger
I understand you want to print.  I just wanted to see where the problem is.  So the problem is got to be in the second line:

$cmd = `processA2Mod $raw $work $caldir $calfile $site_buoy`;

I know you are saying that all the variables have no null values but it is complaining about null values in them.  You can remove one at a time (replacing by values) and see what you get.

Author

Commented:
I'll give it a try. Do values need to be designated with any particular type of punctuation like $cmd = `processA2Mod "value1" "value2" ....`; ?
Regards,
E.J. Yoerger

Author

Commented:
It seems like I moved the error to another part of the code. The new error is
"Use of unauthorized value in string eq at processA2Mod line 188, <STDIN> line 1.
 "                                 .....                                                                        .....    line 2.
etc.

This occurred when I passed the values using double quotes like so: "value".
I believe that the program processA2Mod is being run, but somehow it's not picking up the correct input values. These values would be passed normally on the command line if the program was run from the command line.

Any other ideas? Do you think another set of expert eyes would be helpful?
Regards,
E.J. Yoerger
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
what is the code in processA2Mod near lines 188 and 246?
Are you by any chance passing some special character in any of the variables?

Author

Commented:
No, I'm not passing any special characters.

I'll send you some piece of code, but first I want to run an idea by you.

When calling ...A2Mod from the command line, I execute "perl processA2Mod." Then,
the program gives me one prompt at a time from the keyboard with each answer (I believe) going into <STDIN>. It seems that I need to mimic this processes in the call to ...A2Mod from the Perl script calling it.

Therefore, I'm thinking that something like the following in the calling Perl script:
        $cmd = `perl processA2Mod`
          <STDIN> = "$arg1"
         <STDIN> = "$arg2"
              etc.
I'm not sure of the exact syntax, but I hope you get the idea. This to me mimics what is actually happening in the running the script stand alone.
Any ideas about how to accomplish this?? Thanks.
Regards,
E.J. Yoerger

Author

Commented:
When I looked at line 188, it is actually part of a sub program which is being used to input the data. When I redid my code input with
$cmd = `perl processA2Mod`
       whle (<>) {
                       print $raw;
                       print $work;
                       print $caldir;
                       print $calfile;
                       print $site_buoy;
                      };
I received an error at line 188. It apparently is not getting any data set into $ans below in line 188.

I hope this helps.
Regards,
E.J. Yoerger
Code given below from processA2Mod:

sub prompt_user (@) {
  my $have_readkey;
  #below, "use" fails in compilation without Term::ReadKey, so "require".
  if (eval {require Term::ReadKey; 1})
    {$have_readkey = 1;}
  else
    {$have_readkey = 0;}
  my ($request, $default, $setting) = @_;
  my $ans = '';
  my $def_text = $default ? " ($default)" : '';

  #if you don't put the following line in an eval, compilation will fail
  # on systems without Term::ReadKey  
  eval "ReadMode $setting;" if (defined $setting and $have_readkey);
  while ($ans eq '') {
    print STDOUT "Enter value for $request$def_text:\nentry> ";
    $ans = <STDIN>;
    chomp $ans;
    exit if $ans =~ m/quit|exit/i;
    $ans = $default unless $ans ne ''; <------ Line 188
  }
  eval "ReadMode 0;" if (defined $setting and $have_readkey);
  return $ans
}

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial