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

Dear monks, Thank you all! My codes would run under unix command line. For example, the following segments say "HELLO WOULD!". #!/usr/bin/perl &run; exit(0); sub run { system("gcc code.cpp"); system("a.out/>result.txt"); open(F,"result.txt"); while(<F>) { print $_,"\n"; } close(F); } ##################################### But the system call never respondes to online inputs. I would like you monks to focus on this call. To Roy, "a.out/" is good for my machine. littlefishr
  • Comment on How to use system command in perl script?

Replies are listed 'Best First'.
Re: How to use system command in perl script?
by saskaqueer (Friar) on Jun 17, 2004 at 08:34 UTC

    You have not really provided enough detailed information for anyone to be of much help. I assume that you take the input from the user and put it in file.c. If so, are you sure your file.c file is being created and filled properly? Did you add error checking to the system() command and then check the error log for any useful error information? If the system() command is successful, did you check to see if a.out exists (that should be the file created by gcc, IIRC).

    When posting to the forum, you need to provide more information about what you have tried, and most importantly, what isn't working. Saying 'but it never works' doesn't tell us nearly enough to help out. We are not psychics :)

      Thanks a lot! Here is the original code:
      #!/usr/bin/perl use strict; use CGI; print "Content-type: text/plain\n\n"; my $q=new CGI; my $codes=$q->param('c_code'); my $file = "code.cpp"; open(FH, ">$file"); ### Must chmod Before writing, system("chmod 0777 code.cpp"); ### Can't create and change mode??? +? print FH "$codes"; close(FH); &run; exit(0); sub run { system("gcc code.cpp"); system("a.out/>result.txt"); open(F,"result.txt"); while(<F>) { print $_,"\n"; } close(F); }

      Edited by Chady -- added code tags.

Re: How to use system command in perl script?
by Happy-the-monk (Canon) on Jun 17, 2004 at 08:47 UTC

    Concerning the system command: The environment of the webserver usually is different from that of the ordinary user:
    Try executing your Perl programme in the shell of the webserver's user on the target machine and see what happens.
    That's closer to what's supposedly to happen, when you execute the code via the webpage.

    Now, not concerning the system command, but compiling and running snippets of C code in Perl, you should take a look at Inline::C.

    Cheers, Sören

Re:
by thor (Priest) on Jun 17, 2004 at 12:08 UTC
    A couple of questions. First why do you "have to" chmod the file to be able to have anyone do anything to it before you do anything. Presumably if your CGI can open the file for write, it can write to it without chmoding it. Secondly, any reason that you're not using the perl built-in chmod function?

    thor

Re: How to use system command in perl script?
by stalkeyefly (Novice) on Jun 17, 2004 at 10:20 UTC
    Hello

    You need to use <tags> to formatt the questions son that they can be read - the code tag is espically imp as i learbt from omitting to use it.

    Also I'm new to this (PERL CGI etc), so may have the wrong end of the stick but doubt that you should be giving a file chmod 777 as it is a real network security hazard - i think 755 is used more and is much safer.

    Cheers Stalky
Re: How to use system command in perl script?
by wufnik (Friar) on Jun 17, 2004 at 13:26 UTC
    imho unless you are explicitly wanting the exit status of gcc, and your prog, better to use backquotes in the following manner, to capture the output:
    my $compileresult = `gcc code.cpp`; my $aoutresult = `./a.out`; #... parse aoutresult
    ...wufnik

    -- in the world of the mules there are no rules --
Re: How to use system command in perl script?
by iburrell (Chaplain) on Jun 17, 2004 at 16:03 UTC
    BTW, Perl has a chmod() function that does the same thing as the chmod command. It doesn't have the nifty features, like recursion or symbolic modes, but you aren't using those. It is simpler to call the the function when you can use it. Also, it makes checking for errors easier.
Re: How to use system command in perl script?
by Roy Johnson (Monsignor) on Jun 17, 2004 at 16:32 UTC
    Rewrite run:
    sub run { my $cmd_out; $cmd_out = `gcc code.cpp`; $? and warn $cmd_out; print `a.out`; # Note that you had a slash in your call. $? and warn "a.out had error"; }

    We're not really tightening our belts, it just feels that way because we're getting fatter.