Jump to content

PHP in Perl CGI script - how


upperbid

Recommended Posts

Well, you can try this:

<Location /index.html>
   AddType application/x-httpd-php .cgi
</Location>

But I don't think it will work.  It is likely that the server will only parse one language or the other... As a result, it will more than likely try to parse one as the other and give you a fatal error.

 

You really should try to convert the PHP portion of your code to Perl but I assume that you either don't know Perl or the PHP script is too large to convert.

 

Of course, it is possible to run the PHP script remotely and dump the data back into the Perl script but that requires the proper Perl code to be added to do so.  I don't know Perl well enough to tell you how to go about that.

 

Handy PHP

Link to comment
Share on other sites

The problem is getting two languages to be parsed in a single file.  Off the top of my head I don't think you can really do that, but, perhaps you can get around it by treating it in the same way you would ajax: call the script, the script does the processing and outputs the results as plain text, and on the other end, it is assigned to a var.  I don't know perl enough to be more specific with perl syntax, but if we were to look at it the other way around: getting a perl script to be included in a php script, it would go something like this:

 

Let's say you just want it to be executed and aren't expecting anything returned, or else a simple scalar value:

 

<?php
  $result = file_get_contents('somePerlScript.cgi');
?>

 

this executes the script and assigns the results to $result.  It is "executed" in the same way as if you were to go directly to it in your browser, so $result would contain the same stuff as a rightclick > view source from in your browser.  So if somePerlScript.cgi were to do db interaction or whatever and in the end, do like [pre]print 'kitty';[/pre] that would be assigned to $result.

 

If for instance I needed somePerlScript.cgi to set certain variables or whatever, I could have it output the actual code to set the variable, and eval the string, and it will parse the string as if it were php code.  I don't know if perl has an equivalent function, though...example:

 

somePerlScript.cgi

#!/usr/bin/perl
print "content-type: text/html \n\n";

print '$x = 123;';

 

script.php

<?php
  // somePerlScript.cgi is called and it outputs the following: $x = 123;
  $result = file_get_contents('somePerlScript.cgi');
  // so basically you have the equivalent of this: $result = '$x = 123';
  eval($result); // evaluate the string as if it were php code

  echo $x; // output: 123
?>

 

So that's basically what you could do, in concept. Just have to do it the other way around, where you are calling a php script in your perl script.

Link to comment
Share on other sites

So that's basically what you could do, in concept. Just have to do it the other way around, where you are calling a php script in your perl script.

 

All well and good excepting even your php is floored.

 

file_get_contents gets the contents of the file, not the result of a file thats been parsed by a server. Of course, providing the appropriate url wrappers are enabled you could pass file_get_contents a url however and this would get you the results of that scripts execution.

 

To the op, you'll likely want to look at perl's system or exec functions.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.