PDA

View Full Version : Executing a (CGI) perl program from another one


Ido Perelmutter
03-29-2003, 03:50 PM
Hi there!

I want to execute a (CGI) perl program from another (CGI) perl program and capture the output. To be more specific, my site's main page is actually a perl program, and I use another perl program for banner rotation. I wish to execute this program and print the output (Namely, the banners) from the "main page perl program".

How can I do this?!

Thanks. :)

YUPAPA
03-29-2003, 05:38 PM
HiHi~

You can make the banner script to just output the image of the banner.

And the for the main page script, just put something like

<A HREF="./$the_url"><IMG SRC="./the_banner_script.pl"></A>

It will display the banner image.

Ido Perelmutter
03-29-2003, 06:48 PM
Originally posted by YUPAPA
HiHi~

You can make the banner script to just output the image of the banner.

And the for the main page script, just put something like

<A HREF="./$the_url"><IMG SRC="./the_banner_script.pl"></A>

It will display the banner image.

thank you for trying to help, but unfortunately that doesn't work. I believe the reason is that the program prints an image but also a line of text under it...
Is there anyway to do it no matter what the other program prints? (Actually it does matter - HTML...).

Thanks again.

YUPAPA
03-29-2003, 09:18 PM
Originally posted by Ido Perelmutter
I believe the reason is that the program prints an image but also a line of text under it...

Sorry I don't quite understand... :o What I meant is to make two scripts. One to display the content of the page and the other one is just use for displaying the banner... this example, I use one script to display the page and the banner.



#!/usr/bin/perl
use CGI qw(:standard :form);
use strict;

my $query = new CGI;
my $action = $query->param('action');
my $banner = '/full/path/to/the/banner/image.gif';
my $link_to = 'http://www.mydomain.com';


print "Content-Type: text/html\n\n";

if($action eq 'print_banner') { &print_banner; }
else { &print_page; }


sub print_page {
print qq(
<HTML>
<HEAD><TITLE>My Page</TITLE></HEAD>
<BODY>
<A HREF="$link_to"><IMG SRC="script.pl?action=print_banner"></A>
</BODY>
</HTML>
);
}

sub print_banner {
open(BANNER,"<$banner");
print <BANNER>;
close(BANNER);
}

__END__


Sorry I spent only 1 min on coding, so it doesn't display random images, etc... I want to give you an idea. :o :)

Ido Perelmutter
03-30-2003, 06:12 PM
Yeah I know what you mean, but unfortunately I didn't write the banner script so I can't make it print only the banner....
I want my program to print what the other program prints. no matter what it is...

Thanks for trying to help though...

optimism_
04-01-2003, 11:10 PM
Soz, this is very quick and only meant to be inspiring so no code example:

How about running the banner script using backticks (`blah blah`) and piping the output to a tmp file using whatever random filename you deem necessary. then read the contents of the file into the primary script and printing them to the browser before closing and deleting the file.

Im sure there must be an easier way, especially if your site is php-enabled but hey - its a start :)

YUPAPA
04-02-2003, 04:35 AM
Another option would be use the LWP::UserAgent and HTTP::Request Module to get the url to your banner script and display the output.


my $ua = LWP::UserAgent->new;
my $request = HTTP::Request->new(GET => 'http://www.mydomain.com/banner_script.pl');
my $response = $ua->request($request);
print $response->as_string();

meow
04-02-2003, 05:09 AM
:rolleyes: :eek: Hey-hey! What are YOU doing here?

(Sorry, I realize this is OT :o )

YUPAPA
04-02-2003, 04:23 PM
I come here to code :p

Ido Perelmutter
04-02-2003, 07:55 PM
Originally posted by optimism_
Soz, this is very quick and only meant to be inspiring so no code example:

How about running the banner script using backticks (`blah blah`) and piping the output to a tmp file using whatever random filename you deem necessary. then read the contents of the file into the primary script and printing them to the browser before closing and deleting the file.

Im sure there must be an easier way, especially if your site is php-enabled but hey - its a start :)

I read about the backticks operator but didn't understand exactly how to use it.... thanks for your help, but some code would certainely be helpful... anyway, i'll try to get a little more info about backticks.

Originally posted by YUPAPA
Another option would be use the LWP::UserAgent and HTTP::Request Module to get the url to your banner script and display the output.



code:--------------------------------------------------------------------------------
my $ua = LWP::UserAgent->new;
my $request = HTTP::Request->new(GET => 'http://www.mydomain.com/banner_script.pl');
my $response = $ua->request($request);
print $response->as_string();
--------------------------------------------------------------------------------


I'll try that too.. Thanks!

optimism_
04-09-2003, 09:12 PM
backticks are just like some sort of system functions.


#using the backticks operators
$some_path = "C:\some_exe_file.exe";
`$some_path`; # will execute the file C:\some_exe_file.exe

`c:\temp\some_exe_file.exe`; #will execute some file in a sub folder called temp.


thats about it for backticks. As far as i know you cant capture the output but it will certainly execute a file - i use it to run Blat on my windows box for email in perl.

Ido Perelmutter
04-16-2003, 02:43 PM
Originally posted by YUPAPA
Another option would be use the LWP::UserAgent and HTTP::Request Module to get the url to your banner script and display the output.


my $ua = LWP::UserAgent->new;
my $request = HTTP::Request->new(GET => 'http://www.mydomain.com/banner_script.pl');
my $response = $ua->request($request);
print $response->as_string();



Hey thanks dude that really works.
Still a problem though... It prints this before it:
HTTP/1.1 200 OK Connection: close Date: Wed, 16 Apr 2003 12:41:46 GMT Server: Rapidsite/Apa/1.3.27 (Unix) FrontPage/5.0.2.2510 mod_ssl/2.8.12 OpenSSL/0.9.7a Content-Type: text/html Client-Date: Wed, 16 Apr 2003 12:41:46 GMT Client-Peer: 209.130.45.172:80

Is there anyway to prevent that from happenning?

Thanks man.

optimism_
04-16-2003, 06:38 PM
those are the headers sent by the server to the browser when the script is run. Im not entirely sure if you can prevent them from being sent, but you could perhaps try a regular expression replace to mask them from the output - eg

$output = preg_replace($reg_exp, "//", $input, 1);

if it prints out this:
HTTP/1.1 200 OK Connection: close Date: Wed, 16 Apr 2003 12:41:46 GMT Server: Rapidsite/Apa/1.3.27 (Unix) FrontPage/5.0.2.2510 mod_ssl/2.8.12 OpenSSL/0.9.7a Content-Type: text/html Client-Date: Wed, 16 Apr 2003 12:41:46 GMT Client-Peer: 209.130.45.172:80

the $reg_exp could be:
"/^HTTP/1\.1*Client-Peer: \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}[:\d*]/"

Now im not too good at regular expressions, so this may be erraneous - perhaps someone else can check it for me, but what it matches is:

"HTTP/1.1" at the beginning of the variable
any characters
"Client-Peer: " followed by an ip address and optionally a port.

the last parameter of the function means that only 1 occurrence of the string will be replaced so you dont accidentaly mask out something you do want.

hope this helps

YUPAPA
04-17-2003, 06:12 AM
Originally posted by Ido Perelmutter
Hey thanks dude that really works.
Still a problem though... It prints this before it:
HTTP/1.1 200 OK Connection: close Date: Wed, 16 Apr 2003 12:41:46 GMT Server: Rapidsite/Apa/1.3.27 (Unix) FrontPage/5.0.2.2510 mod_ssl/2.8.12 OpenSSL/0.9.7a Content-Type: text/html Client-Date: Wed, 16 Apr 2003 12:41:46 GMT Client-Peer: 209.130.45.172:80

Is there anyway to prevent that from happenning?

Thanks man.

I tried this and it works plus doesn't show the header...


#!/usr/bin/perl
use LWP::Simple;
use strict;

my $url = 'http://www.mydomain.com/banner_script.pl';

print "Content-Type: text/html\n\n";
print get($url);


I'll see if I can get rid of the header you mentioned... :o

Ido Perelmutter
04-17-2003, 01:53 PM
Originally posted by YUPAPA
I tried this and it works plus doesn't show the header...


#!/usr/bin/perl
use LWP::Simple;
use strict;

my $url = 'http://www.mydomain.com/banner_script.pl';

print "Content-Type: text/html\n\n";
print get($url);


I'll see if I can get rid of the header you mentioned... :o

Thanks dude, less code and better results :thumbsup:
It works perfect!

Originally posted by optimism
those are the headers sent by the server to the browser when the script is run. Im not entirely sure if you can prevent them from being sent, but you could perhaps try a regular expression replace to mask them from the output - eg

$output = preg_replace($reg_exp, "//", $input, 1);

if it prints out this:
HTTP/1.1 200 OK Connection: close Date: Wed, 16 Apr 2003 12:41:46 GMT Server: Rapidsite/Apa/1.3.27 (Unix) FrontPage/5.0.2.2510 mod_ssl/2.8.12 OpenSSL/0.9.7a Content-Type: text/html Client-Date: Wed, 16 Apr 2003 12:41:46 GMT Client-Peer: 209.130.45.172:80

the $reg_exp could be:
"/^HTTP/1\.1*Client-Peer: \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}[:\d*]/"

Now im not too good at regular expressions, so this may be erraneous - perhaps someone else can check it for me, but what it matches is:

"HTTP/1.1" at the beginning of the variable
any characters
"Client-Peer: " followed by an ip address and optionally a port.

the last parameter of the function means that only 1 occurrence of the string will be replaced so you dont accidentaly mask out something you do want.

hope this helps

Well YUPAPA gave me the answer to my problems, but thanks dude, I actually got a new idea (About something else) from your post. Thanks!

Ido Perelmutter
04-17-2003, 01:57 PM
(First of all, I've posted another reply in the previous page if you hadn't noticed)

Now, I got another one for you guys, if you're not already sick of me by now...

What if I wanna print a txt file which is on another server... I don't like to use <iframe> and I don't have php support for <?php include?>...

I don't suppose there's a way to do it but if there is I would really like to know.

Thanks for all your help.

YUPAPA
04-17-2003, 05:34 PM
Use the same code again but replace $url with the txt file?


#!/usr/bin/perl
use LWP::Simple;
use strict;

my $url = 'http://www.theotherdomain.com/my_text.txt';

print "Content-Type: text/html\n\n";
print get($url);

Ido Perelmutter
04-17-2003, 07:00 PM
Originally posted by YUPAPA
Use the same code again but replace $url with the txt file?


#!/usr/bin/perl
use LWP::Simple;
use strict;

my $url = 'http://www.theotherdomain.com/my_text.txt';

print "Content-Type: text/html\n\n";
print get($url);


Heh... I didn't notice that $url gets a url and not an absolute path.
Thanks again, you really helped me.

YUPAPA
04-17-2003, 07:05 PM
:o

chrisbragg
04-30-2003, 10:59 PM
Can't you just 'Require' that perl file in your other script.

Try inserting this:


require "file.pl";


where file.pl is the link to the file you are trying to access. It will act as if that file is inside the script where the 'require' is placed.

YUPAPA
05-01-2003, 01:44 AM
If you have two files, banner.pl and script.pl, it would work out like this.


# banner.pl
#!/usr/bin/perl
use strict;

print "Content-Type: text/html\n\n";
print qq(
<!-- This is the output from banner.pl -->
This would print out the banner
<!-- END -->
);



# script.pl
#!/usr/bin/perl
use strict;

require 'banner.pl';

print "Content-Type: text/html\n\n";
print qq(
<!-- This is the output from script.pl -->
<HTML>
<HEAD><TITLE>My Site</TITLE></HEAD>
<BODY>
My Content goes here
</BODY>
</HTML>
<!-- END -->
);


Whenever you execute script.pl from your browser, it will execute banner.pl and prints out the banner and header first first and then the script.pl which prints out the content. You will not be able to control the layout. You will have your banner displayed on top of each page like this:


<!-- This is the output from banner.pl -->
This would print out the banner
<!-- END -->
Content-Type: text/html
<!-- This is the output from script.pl -->
<HTML>
<HEAD><TITLE>My Site</TITLE></HEAD>
<BODY>
My Content goes here
</BODY>
</HTML>
<!-- END -->


The correct way to display the banner would be putting it inside the <BODY> </BODY> Tags.

This may work if you put require 'banner.pl' right between the body tag of your script.pl. You may end up having two headers again. One is printed by script.pl and another one printed by banner.pl. You may have to modify the script banner.pl to have it actually working the way you want. The only way I can think of would be to store the output of banner.pl to a variable (i.e $banner_code) and print it from script.pl. (i.e print $banner_code). :o That way, you will not have to do any modification to the banner script.