Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Re: How to execute C program and display output to webpage?

Reply
Thread Tools

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

 
 
John Bokma
Guest
Posts: n/a
 
      09-09-2003
Abby wrote:

> 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.

--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: http://johnbokma.com/ ICQ: 218175426
John web site hints: http://johnbokma.com/websitedesign/

 
Reply With Quote
 
 
 
 
Abby
Guest
Posts: n/a
 
      09-10-2003
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.



John Bokma <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Abby wrote:
>
> > 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.

 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      09-10-2003
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.

--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: http://johnbokma.com/ ICQ: 218175426
John web site hints: http://johnbokma.com/websitedesign/

 
Reply With Quote
 
Greg Schmidt
Guest
Posts: n/a
 
      09-10-2003
On 9 Sep 2003 19:42:43 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Abby) wrote:

>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.

--
Greg Schmidt ((E-Mail Removed))
Trawna Publications (http://www.trawna.com/)
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      09-10-2003
[Please don't top-post]
[Please trim your quotes to a reasonable minimum]

Abby wrote:
> [...] 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


 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      09-10-2003
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 .

--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: http://johnbokma.com/ ICQ: 218175426
John web site hints: http://johnbokma.com/websitedesign/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
execute command on remote machine and get output James_Dean Python 0 11-08-2007 10:49 AM
how to execute and capture .aspx output John Mott ASP .Net 2 01-11-2006 08:07 PM
execute commands and return output billiejoex Python 8 09-10-2005 05:06 PM
Can i execute aspx file in my asp pag with server.execute method(sorry) Savas Ates ASP General 1 08-17-2004 04:52 PM
Execute an ASP and output into a string Ed ASP General 2 08-11-2004 09:41 PM



Advertisments