system() commands

  • Thread starter Kim Gardiner CS2003
  • Start date
K

Kim Gardiner CS2003

Hi,
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!!

thanks.
 
P

Paul Lalli

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top