system() commands

Discussion in 'Perl Misc' started by Kim Gardiner CS2003, Jan 30, 2007.

  1. 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.
     
    Kim Gardiner CS2003, Jan 30, 2007
    #1
    1. Advertising

  2. Kim Gardiner CS2003

    Paul Lalli Guest

    On Jan 30, 7:12 am, Kim Gardiner CS2003 <kgardine
    > 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
     
    Paul Lalli, Jan 30, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. caleb
    Replies:
    0
    Views:
    2,916
    caleb
    Jan 3, 2006
  2. Ben Pfaff

    Re: man pages for C commands (GCC commands)

    Ben Pfaff, Jun 24, 2003, in forum: C Programming
    Replies:
    4
    Views:
    4,009
    Thomas Stegen
    Jun 28, 2003
  3. jon c
    Replies:
    0
    Views:
    432
    jon c
    Feb 11, 2004
  4. Tim Stanka
    Replies:
    1
    Views:
    836
    Jeff Epler
    Aug 2, 2004
  5. twsnnva
    Replies:
    6
    Views:
    979
    Cliff Wells
    Sep 11, 2004
Loading...

Share This Page