Re: system() commands



On Jan 30, 7:12 am, Kim Gardiner CS2003 <kgardine
+use...@xxxxxxxxxxxxxxxx> wrote:
I have been using perl to launch Prolog via the system("swipl");
command. This has worked fine so far. However, when I try to issue
commands to Prolog via Perl to read in files and perform queries etc
such as
system("consult(roy_uk).");

I get errors such as:
sh: -c: line 1: syntax error near unexpected token `roy_uk'
sh: -c: line 1: `consult(roy_uk)'

Ive only been teaching myself Perl for the last few weeks so I'm not
sure if I am even going about this is the correct way, but any help
would be much appreciated!!

You're confused about what system() does. It takes the string passed
as the argument, launches a brand-spanking new process, and executes
that command in that process. Two consecutive system() calls are
wholly unrelated. Your second call there is attempting to run the
command "consult(roy_uk)." as an actual command, just as if you'd
entered it at the command line. It is not sending that string to the
previously executed program that was run (and has already terminated)
by the first system() call.

If you want to open a pipe to a process so that you can feed it
commands, you probably want to use open() with a pipe:

open my $prog_h, '|-', "swipl" or die "Cannot start swipl: $!";
print $prog_h "consult(roy_uk).";

Of course, if you're going to want to both write to and read from this
process, this isn't going to work. You need bi-directional
communication, which is more advanced than the above. For the
details, have a read of:
perldoc IPC::Open2
If, after reading that, and making an attempt, your program does not
work as expected, please post a short-but-complete script that
demonstrates your error.

Please also read the Posting Guidelines that are posted here twice a
week.

Paul Lalli

.



Relevant Pages

  • Re: Wait for background processes to complete
    ... To be able to execute commands in the background and wait for their ... The documentation I am referring to is http://perldoc.perl.org/. ... You can run a command in the background with: ... There is a general problem with perl documentation: ...
    (comp.lang.perl.misc)
  • Re: Perl For Amateur Computer Programmers
    ... >professional computer programmers could use with the same ease as Basic. ... >Perl For Amateur Computer Programmers ... Also, taking into account that you're appealing to "scientists", it ... Also, as a side note, you seem to use the noun "command" in a naive ...
    (comp.lang.perl.misc)
  • Obtaining complete Unix command line that evoked script as string
    ... If there is a more appropriate list for this, let me know; the other perl lists I've seen seem to specialised for this. ... Note this is not just the arguments of the call to the script, but everything including pipes and redirects, etc., e.g. ... Ideally the perl interpreter would grab the complete command line as its evoked and I'd access this via a variable. ...
    (perl.beginners)
  • Thanks for the comments Jan. 25, 2006
    ... "shadow" operating system which is running behind Windows. ... Perl can then collect data from that program and do ... that by sending longer strings etc. to the Windows clipboard and then having ... SendKeysuse a Ctrl V command to paste the information to the text editor ...
    (comp.lang.perl.misc)
  • Re: debugger exiting
    ... strict and warnings pragmas. ... I think portraying Perl as a command-line tool limits it to fewer platforms than ... work only as a Unix shell command line. ...
    (perl.beginners)