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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FLEB

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

    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.ev il.ones.will.bo w-down-to.us
    (put "Hey!" in the Subject line for priority processing!)
    -- http://www.pixelsaredead.com
  • Sandman

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

    In article <1noaorjekvn3b$ .3ioirpc607ca$. dlg@40tude.net> ,
    FLEB <soon.the.sp@mm ers.and.evil.on es.will.bow-down-to.us> wrote:
    [color=blue]
    > 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.[/color]

    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]

    Comment

    • FLEB

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

      Regarding this well-known quote, often attributed to FLEB's famous "Wed, 23
      Jun 2004 20:21:25 -0400" speech:
      [color=blue]
      > 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...
      >[/color]

      --snip--
      [color=blue]
      > 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.
      >[/color]

      --snip--

      Well, late last night I tried working this out. Here's what I came up with:


      I'm probably going to completely overhaul everything except the basic idea.
      I'm going to drop the interpreter="wh atever" 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.ev il.ones.will.bo w-down-to.us
      (put "Hey!" in the Subject line for priority processing!)
      -- http://www.pixelsaredead.com

      Comment

      • Andy Hassall

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

        On Wed, 23 Jun 2004 20:21:25 -0400, FLEB
        <soon.the.sp@mm ers.and.evil.on es.will.bow-down-to.us> wrote:
        [color=blue]
        >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.[/color]

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



        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 <andy@andyh.co. uk> / Space: disk usage analysis tool
        http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

        Comment

        • FLEB

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

          Regarding this well-known quote, often attributed to Andy Hassall's famous
          "Thu, 24 Jun 2004 23:06:36 +0100" speech:
          [color=blue]
          > On Wed, 23 Jun 2004 20:21:25 -0400, FLEB
          > <soon.the.sp@mm ers.and.evil.on es.will.bow-down-to.us> wrote:
          >[color=green]
          >>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.[/color][/color]

          -- snip 3 ideas --
          [color=blue]
          > (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.[/color]

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

          --
          -- Rudy Fleminger
          -- sp@mmers.and.ev il.ones.will.bo w-down-to.us
          (put "Hey!" in the Subject line for priority processing!)
          -- http://www.pixelsaredead.com

          Comment

          Working...