federico-perlmonks has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone. I am trying to write a HTML page and I want a Perl script to be executed (and its output printed on the page) when clicking on a link. Both the page and the script shell stay on my computer, not on a server.
My problem is that when I write the path of the script in the search bar (or when I click the link to the script), the browser asks me if I want to open the file or save it... Instead I want it to be executed. How can I do? Is there any documentation I can read?

Replies are listed 'Best First'.
Re: Execute a Perl script in a browser
by choroba (Cardinal) on Apr 20, 2016 at 21:38 UTC
    You can run the web server locally on your computer. For example, see HTTP::Server::Simple.

    To try a more modern way, have a look at Dancer:

    perl -MDancer -e 'get "/" => sub { "Hello world" }; dance' >> Dancer 1.3202 server 15071 listening on http://0.0.0.0:3000 == Entering the development dance floor ...

    Then just point the browser to the indicated address.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      I took a look at dancer and HTTP::Server::Simple. Tell me if I am wrong, but I think when I use Dancer I still have to launch the script from the command line and then use the browser to connect to the address told by Dancer, right?
      HTTP::Server::Simple may be too complex to me... but if I will not find a simpler solution, I will try it.

        Yes, you have to start the server. But you only need to start it once, you can then query it many times. You might need to restart it when the code changes, though.
        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Execute a Perl script in a browser
by Marshall (Canon) on Apr 21, 2016 at 00:41 UTC
    "browser asks me if I want to open the file or save it... Instead I want it to be executed." As an experiment, I pointed my Firefox browser at a Perl script. There is no "execute" option, but under "open", one of my options under Windows is "Perl Command Line Interpreter (default)" amoungst Notepad and other "open options" for this xxx.pl file. A command line window pops up, script runs and then command line window disappears. Of course my script doesn't generate any HTML or launch a webpage, but I suppose that is possible. Anyway if you "open" the file with Perl, it will be executed.

    I don't know that this tid-bit of info helps you. I think something more sophisticated as choroba suggests is what you need? Oh, there is also a way to control Firefox from Perl, WWW-Mechanize-Firefox. That might have some relevance?

      I managed to run the script doing what you said, but it opened another window, while I was hoping that it acted more like some links you find in the web pages: you click, the program is executed and the server sends you another page, with the output (for example, when you register to a forum...). I'm sorry I did not explain this better in the post.

      The option to mechanize firefox is interesting, I'm sure I will find something useful.

Re: Execute a Perl script in a browser
by FreeBeerReekingMonk (Deacon) on Apr 21, 2016 at 08:03 UTC
    NEW RANTING:

    1. have a link in the browser webpage that points to a pl file.
    2. asociate your pl extension with a program you will write (and set the "always run with this program" so it doesnt pop up anymore)
    3. clicking on this link creates a detached running of your program, with the pl as parameter (the same way a .doc file opens word)
    4. the program runs perl, output into a temporal file with html extension, then runs firefox again with that page... which should open in a new tab/window

    Ok, you will need to make a cmd program, then use bat2exe to make it an exe...

    OLD RANTING:

    Although in the past you could run arbitrary code in Firefox without a plug-in... it was deemed unsave and has been removed. There are some plug-ins that do that for you, but I can not find any at the moment. (and the output was passed as pure text, not re-rendered as html, so you still needed some jquery magic with a refresh to reload that as html)

    If you still want to pursue this (without writing a webserver): Flash allows running arbitrary code, but support of it is iffy (and once you know what you can do with it, you never want flash active again when browsing other peoples webpages ;) ). Execute your perl output to a file, and load the result into your page.

    And there this

    https://en.wikipedia.org/wiki/TiddlyWiki

    You can modify the plug-in to run your code, but you will need to modify the java code for it. As of now, it only read/saves.

    disclaimer: THIS OPENS BACKDOORS INTO YOUR MACHINE THAT ANYMONE CAN USE!

    So the best thing you can do:

    Create a CMD/BAT script that you click yourself. And it does execute perl to a static page, then call firefox.exe and gives it that file. If firefox is already open, it will display it in a new tab.

      I managed to run the script and open it in a new window, but it was not what I had in mind. You are probably right: the best solution, or at least the easiest, is to manually launch the program and then pass its output to firefox.

        Out of curiosity, since you aren't running a web server and all of this is local, why is there a requirement to display this in Firefox in the first place?

        If the base requirement is to have a GUI, then there are lots of ways to do that which do not involve the browser at all! I've made some pretty fancy GUI's with Tk, but there are other options. I'm just wondering if we may all be pounding away at the wrong "solution"? If the real question is "hey, I want a GUI", then the discussion goes into a whole different direction and probably should be a new thread.

        As another server option, one project that I'm working on (not done yet), is to get a Raspberry Pi 3 up and working with an Apache server for my "testing play box". I should be able to do this for less than $100 + brain sweat. This thing actually has enough horsepower to be a full Unix server the size of my palm.

        Update: see XY Problem

Re: Execute a Perl script in a browser
by gnosti (Chaplain) on Apr 22, 2016 at 23:05 UTC
    Hi,

    If the link-clicking part is problematic, perhaps it may be enough to run the script that generates the HTML, have the output go to a file, then give the filename as an argument to the browser e.g. firefox.

    In that case, you could wrap the shell script like this:

    #!/bin/sh generate-html.pl > output.html firefox output.html

    So, you'd run your wrapper script, instead of clicking a link, for each iteration.

    cheers