1

I am having a Perl and a CGI file through which I want to fetch data from database. I've a UI where I am trying to use AJAX call which will hit the perl (.pl) or (.cgi) file and get the response in JSON. I've checked the perl/cgi file by running through command prompt and it works fine. This is how I am running my code in command prompt:

D:\>PerlExecutables\strawberry_32\perl\bin\perl.exe C:\Users\UserXYZ\Desktop\PerlExamples\test.cgi

The reason is I cannot do any kind of installation on my machine and also I don't want to run it through server like Apache or IIS. How can this be achieved? Is there any way to make the script work in AJAX by passing the perl.exe path for execution or Any other alternatives?

Thanks!

5
  • 4
    If you can run Perl and deploy a CGI script, you can also deploy a Perl program that acts as a web server on localhost. An HTTP server will definitely need to be involved, the browser will not allow you to run programs directly. Commented Jul 18, 2017 at 11:32
  • 1
    You might want to check perl based web server, mojolicious.org/perldoc/Mojo/Server/CGI Commented Jul 18, 2017 at 11:47
  • @Thilo I found metacpan.org/pod/HTTP::Daemon but everytime the URL keeps on changing if you can just run the example provided Commented Jul 18, 2017 at 11:56
  • @Ashraf.Shk786: You can specify a port number in the constructor. See the example on the page you linked to. Commented Jul 18, 2017 at 12:02
  • I am trying with use HTTP::Daemon for server and as soon as I run the file server.pl from cmd something happens that make command prompt non-editable which I probably think is a server start. I've also printed the url and port number which are used to call that server. But, when I try to open browser and run the URL with port number I am getting Error 403 : The website declined to show this webpage. Am I missing something? Commented Jul 19, 2017 at 5:30

1 Answer 1

3

One way to do this is to use Plack::App::CGIBin. It allows you to mount CGI scripts as apps with the PSGI/Plack protocol.

use Plack::App::CGIBin;
use Plack::Builder;

my $app = Plack::App::CGIBin->new(root => "/path/to/cgi-bin")->to_app;
builder {
    mount "/cgi-bin" => $app;
};

Save that as myapp.psgi (or whatever your stuff is called) and run it like this:

$ plackup myapp.psgi

By default it will open up a server on port 3000 on localhost. You will need to be able to install Perl modules. Since you have Strawberry Perl that shouldn't be a problem. In the worst case, just use a local::lib.

You will also need to be able to open a port for listening. If you cannot there is no other solution than to get an admin to install you an actual full-scale web server.


The PSGI protocol and the Plack tools are a simple, easy to use replacement for CGI. They allow you to be very flexible while making it easy to have persistently running large applications.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.