Re: How to execute C program and display output to webpage?

J

John Bokma

Abby said:
Hi,

I have no experience in Perl, but I need to write a Perl script.
What the script will do is .. it will pass some arguments, which I got
from text box in the form on webpage, to my c program (.exe file) as
arguments, run the program, then display the command-line output to
the web browser. Could anyone please give me an idea how to start,
please?

CGI.pm (getting parameters, creating headers).
-T option in the she-bang
-w option in the she-bang
use strict;

read on "system"

If you generate HTML using CGI.pm notice that recent versions default to
XHTML. Understand the impacts of this.

understand all the dangers of passing parameters from a form to an
executable.

What I recommend if:

a given parameter can only be a number between 1 and 100 *check* this.
Check it is a number *and* check if it falls in the range. Never ever
assume that your form gets valid values *nor* ever assume that client
side checks (JavaScript, maxlength etc) succeed (zie link in my sig).

If a parameter value falls out of a range, is not what you expect then
give an error. This is an exception. Something is *wrong*. Don't ever
try to fix it or fall back to defaults. If you get a silly value shout,
yell, complain. Don't silently fix & accept.

Be very very careful what you allow to be passed via a web form. All
your enemies are out there and able to call your script in every
possible bad way. Don't trust anything, ever. Even not the headers you
get (for example referer). Assume the worst.
 
A

Abby

Thanks a lot for your great tips. Just wonder about one thing. After I
use system("[program] [parameter]"), how can I get the output and
display all of them in webpage? Thank you so much.
 
J

John Bokma

Abby wrote:

please don't top post, or if you do, cut away everything below it if it
is no longer relevant.
Thanks a lot for your great tips. Just wonder about one thing. After I
use system("[program] [parameter]"), how can I get the output and
display all of them in webpage? Thank you so much.

if you want the output you can use `` (back ticks, not ') or open a pipe
to read from:

$output = `program parameter`; # one, multilined string
@output = `program parameter`; # one line per item

open(PROGRAM, "program parameter |" or die "Can't run program: $!";
while ....
:
:


Note: both call a shell to run the program. Understand the impact of this.
 
G

Greg Schmidt

Thanks a lot for your great tips. Just wonder about one thing. After I
use system("[program] [parameter]"), how can I get the output and
display all of them in webpage? Thank you so much.

If you want all of the output to be displayed exactly as-is in the
browser, you don't have to "get" the output with your Perl script at
all. Just let the C app print to it's standard output, the same as you
do with your Perl scripts, and the server will take care of getting it
to the browser for you. If you need to wrap something around the C
output, then print the first part with Perl, run the C app, and finish
up by printing the last part with Perl. Only if you want to somehow
change the output of the C app before sending it to the browser do you
need to be concerned with it in Perl.
 
J

Jürgen Exner

[Please don't top-post]
[Please trim your quotes to a reasonable minimum]
[...] After I
use system("[program] [parameter]"), how can I get the output

You don't. Capturing the output of an external program is not what system()
does.
For further details and what to do instead please see the third paragraph of
the documentation for system():

perldoc -f system
and display all of them in webpage?

Well, you could print them to a file, return them to a CGI server, ....

jue
 
J

John Bokma

Jürgen Exner wrote:

[output C program]
Well, you could print them to a file, return them to a CGI server, ....

rather not. Overhead and file locking / temp file creation with all the
fun associated with this :).
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top