ramigi has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
Is it possible to insert a perl program (not perlscript) inside an html page? (in the same way as javascript).

For example, store some text in a variable, rework it with regular expressions and print it in the same HTML page.

#!/usr/local/bin/perl my $ligne; my $corpus = "file.txt"; #some text in a variable open (CORPUS,"<$corpus") or die "erreur de texte d'origine"; while($ligne=<CORPUS>){ $ligne=~s/^\s*//; #if line begins with one or sev. spaces, repla +ce by nothing if($ligne=~m/$\./){ #if line ends with a dot, print "$1\<br\>"; #add <br> } print "$ligne"; }

Thank you,

RG

Replies are listed 'Best First'.
Re: perl inside HTML
by Corion (Patriarch) on Dec 16, 2005 at 09:46 UTC

    ActiveState has the PerlScript plugin, which acts as an IE browser plugin and which enables Internet Explorer to run Perl code where you would otherwise place JavaScript. This is Windows-only though.

    Maybe you can tell us the problem you want to solve by using this technique. Usually, it's better to use either server-side techniques like Template Toolkit or HTML::Template or to use JavaScript. JavaScript has native regular expressions that have about the same power as Perls nowadays.

Re: perl inside HTML
by reneeb (Chaplain) on Dec 16, 2005 at 10:37 UTC

      ... embperl? ... this code runs in the server side, like php, or a simple perl-cgi ... and ramigi whas asking for a 'client-side' perl interpreter included in the web browser; like javascript, vbscript (on the iexplorer) or others.

      I'm affraid that the solution is to install a plugin to support this web-browser-perlish comprehension(like the activeperl's one, mentioned by Corion) ...

      Maybe we must propose to the mozilla-firefox guys to give developpers a way to give this support ... it would be funny to program client-side perl scripts managing the obscure DOM as you want ...
      ... i imagine thinks like this:

      <script language='perl'> use Dumpvalue; my $dump = new Dumpvalue; $dump->dumpValue(\@document); #prints the DOM :-P </script>

      hahahaha

      Regards :-P

      turo

      perl -Te 'print map { chr((ord)-((10,20,2,7)[$i++])) } split //,"turo"'
        To expand on turo's post, HTML::Embperl is a server-side tool for mixing Perl and HTML, although it's more like PHP than JavaScript. See also the Embperl home page. I use it extensively and it makes transaction-oriented web systems really easy. I also wish there were a replacement for javascript, but, well, somebody's going to have to write one. :) I just embed JS calls and code in the Embperl header files and the main HTML file when I need to do browser-side validation and such.

        Don Wilde
        "There's more than one level to any answer."
Re: perl inside HTML
by eweaver (Sexton) on Dec 16, 2005 at 19:37 UTC

    So, summarizing the other replies: not really.

    The reason is that every modern browser includes a javascript interpreter built-in, but no browser (unless you use that unlikely plug-in mentioned above) includes a perl interpreter, and you need your code to be run client-side. javascript supports regular expressions so I suggest you just learn a little javascript.

    You will have trouble with the open() call, though. Where do you expect the file to be? On the client's machine? On the server?

      Dear all,

      Thanks for all of your replies. Basically, both html file and the text file can be on the client side for someone who creates a web site, or on a server when the client enters a text (a comment typically) in a form field that has to appear in a second "confirmation" page.

      I don't really know any javascript, and from what I have seen so far it is more complicated than perl...

      Concerning the famous IE plug-in, does anybody know where to get it?

      Many Thanks,

      Rami Gideon

A reply falls below the community's threshold of quality. You may see it by logging in.