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

Hy guys. I have a problem. I want to includ a perl file in html file with JavaScript, but don't work.
I use Apache Server.
I want to use JavaScript to includ , not SSI, and i want to help me, please.

Html file:
<html> <head> <title>Welcome</title> </head> <body> Acesta este pagina de index si mai jos se afla legatura facuta la test +.pl<br><br> <script src="http://localhost/cgi-bin/test.pl"></script> <br> </body></html>


Perl file:
#!perl print "Content-type: text/html\n\n"; print "Acesta este continutul fisierului";


Why don't work? :(
Thank you.

Replies are listed 'Best First'.
Re: Problem to include a perl file
by hacker (Priest) on Oct 10, 2003 at 13:17 UTC

    There is no spoon.

    Seriously though, JavaScript is parsed, not executed (similar to perl, but not). You have to provide a link to an actual file containing JavaScript commands that your browser's JavaScript parsing engine can understand and properly parse. Passing it a Perl script will do nothing, except cause the internal browser JavaScript parser to silently abort with an error (unless you have a browser with a JavaScript debugging console). The browser's internal JavaScript parsing engine doesn't understand how to properly parse perl constructs (thankfully), and leaves it alone.

    Also, even if that could work, your perl script is redundant, since you're already passing a Content-Type when your webserver sends the .html page to the client, and you then parse a perl script which passes it again, right in the middle of existing content.

    In short, use the Perl interpretor to interpret perl, and the JavaScript interpretor to interpret JavaScript.

    How about using this instead, less code, easier to maintain:

    #!/usr/bin/perl use strict; # ALWAYS use strict use CGI qw(p br); my $cgi = CGI->new(); print $cgi->header(); print $cgi->start_html(-title=>'Welcome'), "\n"; print p('Acesta este pagina de index si mai jos se afla legatura facuta la test.pl'); print p('Acesta este continutul fisierului'), br(); print $cgi->end_html();

    You could also do this all in one print statement, if you wish:

    #!/usr/bin/perl use strict; # ALWAYS use strict use CGI qw(p br); my $cgi = CGI->new(); print $cgi->header(), $cgi->start_html(-title=>'Welcome'), "\n", p('Acesta este pagina de index si mai jos se afla legatura facuta la test.pl'), p('Acesta este continutul fisierului'), br(). $cgi->end_html();

    TMTOWTDI

Re: Problem to includ a perl file
by alienhuman (Pilgrim) on Oct 10, 2003 at 13:30 UTC

    webstudioro,

    Hay varios razones porque su ejemplo no funciona. Primera es que seria una violacion de seguridad si el webserver no prohibe cualquiera pagina de html ejecutar una script en cualquier directorio--alguien se puede escribir una script y ejecutarlo sin tener los permisos.

    Segundo es que el html tag <script> necesita el atributo "type", y los valores permitidos no incluyen perl. Aqui esta detalles, pero en breve--<script> es solo para ordenar el cliente (typicamente un web browser) a ejecutar una script.

    Perdoname mi castellano--hace muchas meses que escribia en la idioma, y nunca de sujectos tecnicos (que Ud. ya sospecha, pienso!).

    Boy is my face red: I thought I saw Spanish in the OP and decided to try to show off. Teach me to write anything before coffee.

    There are several reasons why your example doesn't work. First is that it would be poor security if the webserver allowed any old html page to exectute any old script in any directory--anyone could write a script and execute it without having the permissions to do so.

    Second is that the html tag <script> requires the attribute "type" and the allowed values don't include perl. Here are the details, but in short--<script> is only for telling the client (typically a web browser) to execute code.

    My apologies for my Spanish--it's been months since I've written in that language, and never on technical matters (as you probably suspect!).

    Further attempt to atone for the above goof:

    The tag you're probably looking for is:
    <!--#exec cmd="command [arguments...]"-->
    Note that your server has to be configured to allow this sort of thing.

    Sweet jebus, I didn't notice he wants a JS solution. That's it, I'm going to go drink several cups of coffee and start over with PM today. Do me a favor and ignore this w/u.

    AH

    ----------
    Using perl 5.6.1 unless otherwise noted. Apache 1.3.27 unless otherwise noted. Redhat 7.1 unless otherwise noted.
Re: Problem to includ a perl file
by rdfield (Priest) on Oct 10, 2003 at 13:10 UTC
    Why don't work? :(
    because you don't have perl installed as a plug-in in the client's browser? I don't think that the problem that you are having is the same as the problem you think you are having. Please take a look at the Tutorials, the bit about "Network/CGI programming" would be the best place to start.

    rdfield

      The chap may just be trying to grab the results of a CGI script into his webpage and is using a very simple CGI to get it working. That would explain the Content-Type bit to cheer apache up.

      I think the problem is that the CGI is dying (check the logs or go to the page direct) and / or that the javascript is duff (probably the case) try grabbing a static page and get that working. In fact I'm sure it is the JavaScript.

      Client side perl scripts would be great but to the best of my knowledge the few implementaions are on remarkably few browsers.

      --tidiness is the memory loss of environmental mnemonics

        To clarify: I seriously doubt that the contents of %DOCUMENTROOT%/cgi-bin/index.pl is Javascript. I think that there is difference between how CGI works and how the OP thinks CGI works. Hence the link to tutorials.

        rdfield

Re: Problem to includ a perl file
by jeffa (Bishop) on Oct 10, 2003 at 19:56 UTC
    As BUU and the others have already stated ... this is working exactly as it should. If your test.pl file actually returns a plain text header and VALID JavaScript. You don't need to change your HTML, just the Perl script:
    #!/usr/bin/perl -T use strict; use warnings; use CGI qw(:standard); print header('text/plain'), 'window.document.title = "Acesta este continutul fisierului"' ;
    But what this buys you beats me ... seems very silly.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Problem to includ a perl file
by eric256 (Parson) on Oct 10, 2003 at 13:15 UTC

    It would be easier if you gave use some idea what didn't work about it. You should also remember that browsers don't usualy show the contents of script tag, so either the script (returned by the perl files output) needs to include the javascript to add content to the web page, or you need to use a different tag to include the output of the script. You could try using an IMG tag. I beleive those work even to include text in the way you are trying.

    Good luck, and if you want an example of the javascript needed check out DigitalWindmill (google for it). I know they used to use a script tag like that to let people include dynamic content.


    ___________
    Eric Hodges
Re: Problem to includ a perl file
by BUU (Prior) on Oct 10, 2003 at 16:12 UTC
    It is working. Everything is working perfectly in fact. The reason you don't see anything is A) <script></script> tags generally expect a 'text/javascript' header (or something like that) and B), last time I checked Acesta este continutul fisierului isn't valid javascript. So nothing happens.