By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,662 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

Perl inline with PHP, a few ideas. Also: Multiline strings? Proc_open() tuts?

P: n/a
I like PHP for its excellent inline integration into standard HTML files,
but I like Perl for its quick-moving syntax and simpler data-processing. To
resolve this deep-seated inner turmoil (oh, the drama) I've been trying to
think of good ways to get Perl code to run inline in a PHP script. Here are
a few of my ideas. If anyone has any further ideas, resources, or knows of
someone else who's solved this already, please do tell...

1.) The simple route: Write the Perl script as a seperate file. Exec() perl
with it.

This would work, but it involves juggling seperate files.

2.) Write the Perl script into a huge Heredoc-formatted string. Proc_open()
perl and send the string to it for execution.

This was the first idea I'd come up with, and the most straightforward,
except for the fact that both Perl and PHP use $ as the variable
descriptor, and I don't know how to turn off variable expansion
(double-quotedness) in Heredoc formatted strings. Is there a way?

3.) Write the Perl script as a specially-marked /* */ comment. Use an
include()d function to read the current file, scan for the marked comments,
and proc_open() feed them to perl.

If I can't find a way to get Heredoc not to interpolate variables, this is
really the best way I can think of to work it. Since Perl only uses # for
comments, there's little chance of */ coming up in the inlined script.

This solution is better, but more complex. It's only dealing with comments,
not code, so there's less chance of tainted PHP input mucking up the works.
On the downside, though, it involves re-reading the PHP file again, as well
as making up and working with nonstandard notation for passing variables
and denoting code.
Related questions:

Are there any ways to denote a large, multiline string in PHP that don't
interpolate variables? Something similar to Perl's __END__ feature,
perhaps?

Also, does anyone know a good "primer" or tutorial that goes over the ins,
outs, and pitfalls of proc_open and piping i/o to processes? I read the
php.net manual, and I get the feeling I should learn a little bit more,
even about piping in general, before attempting to go live with any sort of
proc_open scripts. It doesn't sound all that difficult to bog down a server
with proc_open.

--
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <1n******************************@40tude.net>,
FLEB <so*********@mmers.and.evil.ones.will.bow-down-to.us> wrote:
I like PHP for its excellent inline integration into standard HTML files,
but I like Perl for its quick-moving syntax and simpler data-processing. To
resolve this deep-seated inner turmoil (oh, the drama) I've been trying to
think of good ways to get Perl code to run inline in a PHP script. Here are
a few of my ideas. If anyone has any further ideas, resources, or knows of
someone else who's solved this already, please do tell...

1.) The simple route: Write the Perl script as a seperate file. Exec() perl
with it.

This would work, but it involves juggling seperate files.

2.) Write the Perl script into a huge Heredoc-formatted string. Proc_open()
perl and send the string to it for execution.

This was the first idea I'd come up with, and the most straightforward,
except for the fact that both Perl and PHP use $ as the variable
descriptor, and I don't know how to turn off variable expansion
(double-quotedness) in Heredoc formatted strings. Is there a way?

3.) Write the Perl script as a specially-marked /* */ comment. Use an
include()d function to read the current file, scan for the marked comments,
and proc_open() feed them to perl.

If I can't find a way to get Heredoc not to interpolate variables, this is
really the best way I can think of to work it. Since Perl only uses # for
comments, there's little chance of */ coming up in the inlined script.

This solution is better, but more complex. It's only dealing with comments,
not code, so there's less chance of tainted PHP input mucking up the works.
On the downside, though, it involves re-reading the PHP file again, as well
as making up and working with nonstandard notation for passing variables
and denoting code.


What webserver are you running? Have you tried mod_perl? mod_perl enables
inline perl code in HTML document much like how PHP functions.

Difference is that PHP has a multitude of HTML/Web-specific functions that Perl
lack (or need to be added) which is why I have switched full-time to PHP from
being a SSI/CGI kind of guy in the past.

And yes, I miss $_, among a series of things, as well. :-D

--
Sandman[.net]
Jul 17 '05 #2

P: n/a
Regarding this well-known quote, often attributed to FLEB's famous "Wed, 23
Jun 2004 20:21:25 -0400" speech:
I like PHP for its excellent inline integration into standard HTML files,
but I like Perl for its quick-moving syntax and simpler data-processing. To
resolve this deep-seated inner turmoil (oh, the drama) I've been trying to
think of good ways to get Perl code to run inline in a PHP script. Here are
a few of my ideas. If anyone has any further ideas, resources, or knows of
someone else who's solved this already, please do tell...

--snip--
3.) Write the Perl script as a specially-marked /* */ comment. Use an
include()d function to read the current file, scan for the marked comments,
and proc_open() feed them to perl.

If I can't find a way to get Heredoc not to interpolate variables, this is
really the best way I can think of to work it. Since Perl only uses # for
comments, there's little chance of */ coming up in the inlined script.

This solution is better, but more complex. It's only dealing with comments,
not code, so there's less chance of tainted PHP input mucking up the works.
On the downside, though, it involves re-reading the PHP file again, as well
as making up and working with nonstandard notation for passing variables
and denoting code.


--snip--

Well, late last night I tried working this out. Here's what I came up with:
http://php.pixelsaredead.com/inline-perl/show-me.php

I'm probably going to completely overhaul everything except the basic idea.
I'm going to drop the interpreter="whatever" in favor of just reading a
shebang (#!) line from the comment. Right now the interpreter="" doesn't
actually specify anything (It just defaults to perl). It also needs a good
way to pass variables back and forth. Still, though, I like where this is
going.

--
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com
Jul 17 '05 #3

P: n/a
On Wed, 23 Jun 2004 20:21:25 -0400, FLEB
<so*********@mmers.and.evil.ones.will.bow-down-to.us> wrote:
I like PHP for its excellent inline integration into standard HTML files,
but I like Perl for its quick-moving syntax and simpler data-processing. To
resolve this deep-seated inner turmoil (oh, the drama) I've been trying to
think of good ways to get Perl code to run inline in a PHP script. Here are
a few of my ideas. If anyone has any further ideas, resources, or knows of
someone else who's solved this already, please do tell...

1.) The simple route: Write the Perl script as a seperate file. Exec() perl
with it.

2.) Write the Perl script into a huge Heredoc-formatted string. Proc_open()
perl and send the string to it for execution.

3.) Write the Perl script as a specially-marked /* */ comment. Use an
include()d function to read the current file, scan for the marked comments,
and proc_open() feed them to perl.


(4) The 'proper' way but also by far the most work - probably a ridiculous
amount - embed a Perl interpreter in PHP, interfacing the two with a PHP
extension module, to avoid the startup costs of spawning a new Perl interpreter
per request.

http://www.perldoc.com/perl5.8.0/pod/perlembed.html

Implmentation details left as an excercise for the reader ;-)

Then again, this is basically re-inventing mod_perl, except loading it from
PHP rather than directly from Apache.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #4

P: n/a
Regarding this well-known quote, often attributed to Andy Hassall's famous
"Thu, 24 Jun 2004 23:06:36 +0100" speech:
On Wed, 23 Jun 2004 20:21:25 -0400, FLEB
<so*********@mmers.and.evil.ones.will.bow-down-to.us> wrote:
I like PHP for its excellent inline integration into standard HTML files,
but I like Perl for its quick-moving syntax and simpler data-processing. To
resolve this deep-seated inner turmoil (oh, the drama) I've been trying to
think of good ways to get Perl code to run inline in a PHP script.

-- snip 3 ideas --
(4) The 'proper' way but also by far the most work - probably a ridiculous
amount - embed a Perl interpreter in PHP, interfacing the two with a PHP
extension module, to avoid the startup costs of spawning a new Perl interpreter
per request.

http://www.perldoc.com/perl5.8.0/pod/perlembed.html

Implmentation details left as an excercise for the reader ;-)

Then again, this is basically re-inventing mod_perl, except loading it from
PHP rather than directly from Apache.


Yeah... I'm nowhere *near* that hardcore :).

--
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.