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

I am trying to send two parameters from one perl script to another. When the child finishes, I want to capture the output. Here is line

open(LDBHIER, "ldbtest.pl -e aa100001 -t 123|") || die "dead";

the idea is that ldbtest.pl receives the two values and sticks them in $opt_e, and $opt_t )using the Getopt::Std module. When that program finishes, I believe that the output is put in the LDBHIER file-handle but I get an error message

Broken Pipe sh: -t: not found

I was able to get this to work when I had one parameter and I am able to run

ldbtest.pl -e aa100001 -t 123

from the unix prompt, but I am afraid that I am new at this and what I have read about forks, et al has just confused me a bit.

Replies are listed 'Best First'.
Re: passing values to another perl program
by Malkavian (Friar) on Jul 09, 2001 at 21:01 UTC
    Just a thought here..
    In your code, you're pulling part of the command line from a file line by line
    It's quite likely that this line contains the newline string, which, when inserted before the -t causes the shell to believe that you're trying to type two seperate lines (thus it's actually trying to do a :

    sh ldbtest.pl -e aa100001<newline>
    sh -t 123

    Try using a chomp on the value you interpolate into the piped open, or backticks, and see if that helps any.

    Cheers,

    Malk

      ahhhh the chomp worked! i guess i should have paraphrased the advice of my latin teacher when practicing translating...."when it doubt, chomp" that you monks!

      ..and thank you for the advice on Open3(), i was curious about how that would work...

Re: passing values to another perl program
by PrakashK (Pilgrim) on Jul 09, 2001 at 20:36 UTC
    It appears that the script ldbtest.pl is being run by your shell instead of by the perl interpreter.

    Do you have the shebang line (#!/usr/bin/perl -w or something like it) as the first line of your script? Also check the permissions on this script. I assume they are ok, considering you are able to run it from command line, but doesn't hurt to check.

    /prakash

Re: passing values to another perl program
by particle (Vicar) on Jul 09, 2001 at 20:35 UTC
    this may be overkill, but i found a way, with tye's help, to get STDERR, STDOUT, and the exit value back from an external program. it avoids the piped open, and uses IPC::Open3 instead. you may find this link useful.

    ~Particle

Re: passing values to another perl program
by arturo (Vicar) on Jul 09, 2001 at 20:08 UTC

    If you want to gather another program's output, use the built-in backticks:

    my $output = `ldbtest.pl -e aa100001 -t 123`;

    But be *VERY CAREFUL* and run under taint mode if the use r is going to be allowed to pass parameters to the other script.

    HTH perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); $rose = "smells sweet to degree $n"; *other_name = *rose; print "$other_name\n"' </code>

      I used the line from above and the line that returned to me is...

      sh: -t: not found

      below are the two programs, stripped down, that I am trying to run. The first one calls the second, which should print out the values. At this point all that I am trying to accomplish is seeing that the values get over.

      Here are the two programs: #!/use/bin/perl -w open(EXT,"< LDB_EXTID.lst") || die "something"; while (<EXT>) { push (@externaluids, $_) || die "dead again"; } close (EXT) foreach $externaluid (@externaluids) { my $output = `ldbtest.pl -e $externaluid -t 123`; #for my testing purposes $counter++; exit if $counter > 10; } exit #program ldbtest is simply... #!/use/bin/perl -w use Getopt::Std; getopt ('et'); print $opt_e print $opt_t exit

        Lemme reiterate Malkavian's suggestion in Re: passing values to another perl program; as we can now tell from your posted code, you're snagging individual lines from the one file, and interpolating those values into your backticks; but as he suggested might be the problem, you've got newlines in there and so the -t is seen as the beginning of a new command. Here's one way to rewrite your code:

        open (EXT, "LDB_EXTID.lst") or die "something"; my @uids = <EXT>; # read in each line as a member of the array close EXT; # here's the key line ! chomp @uids; # remove any trailing newlines from every member of @uids foreach my $uid (@uids) { my $output = `ldbtest.pl -e $uid -t 123`; last if ++$counter > 10; } # stuff you'd do after the loop, if anything

        perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
      Hi, I have a scenario, which I’m working on.. Perl script 1 abc.pl--- will have two parameters, $paramter1 $paramter2 It will call another perl script 2—xyz.pl, passing the two parameters, The perl script2, will be a tcp client requesting for a service with a java server. Once the service is done, I need to return a success/error code of 0 or 1 to th main perl script abc.pl Could u please explain how to do it.. I’m a newbie.. (abc.pl) #!/usr/local/bin/perl # # composite series of images over a background image # print "\nThis is from abc.pl"; $path = "/usr/bin"; $hostName ="127.0.0.1"; $cmd = "perl xyz..pl $hostName $path"; system($cmd)== 0 or die "system $cmd failed: " ,$? >> 8; if ($? == -1) { print "\n failed to execute: $!\n"; } elsif ($? & 127) { printf "\n child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; } else { printf "\nchild exited with value %d\n", $? >> 8 ; } And I need to write xyz.pl I’m able to get param from abc.pl, But how to send back the error/ success code #!/usr/local/bin/perl # # composite series of images over a background image # use IO::Socket::INET; print "\n This is in xyz.pl"; if ($#ARGV != 1) { print "usage: e2.pl hostname , pathofResults \n"; exit(2); } $hostName = $ARGV[0]; $path = $ARGV1; #is this possible??? $msg = $hostName+$path ; # Create a new socket $MySocket=new IO::Socket::INET->new(PeerPort=>5000,Proto=>'tcp',PeerAddr=>’127.0.0.1’); # Send messages to server $MySocket->send($msg); $MySocket->recv($text,1024); print "\nReceived message : ", $text,"\n"; exit (2);