Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

PERL-CGI on shared unix server

by rahu_6697 (Novice)
on Jun 22, 2018 at 05:43 UTC ( #1217158=perlquestion: print w/replies, xml ) Need Help??

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

Hello to all! I am new to perl-cgi, just trying to execute my first program and stored it as in my home directory. I am working on a shared unix based server so I don't have access to var/www/cgi-bin folder of my directory and there is not any webserver package installed. So how should i run this type of files. Please provide step by step procedure for running this file such that on browser Hello World! should display.

#!/usr/local/bin/perl print "Content-Type: text/html\n\n"; print "<html> <head>\n"; print "<title>Hello, world!</title>"; print "</head>\n"; print "<body>\n"; print "<h1>Hello, world!</h1>\n"; print "</body> </html>\n";

Replies are listed 'Best First'.
Re: PERL-CGI on shared unix server
by NetWallah (Canon) on Jun 22, 2018 at 05:55 UTC
    Try HTTP::Server::Simple.

    If you have used modules before this should be fairly straightforward.

                    Memory fault   --   brain fried

      Presently I'm printing all results data to an output.txt file but I also want to arrange all data in tabular format so as to open it on web-browser.Below is my PERL code from which I am printing all data in output file.

      printf OUT "%-15s %-13s %-16s %-13s\n",$fname,$drctotal,$metaltotal,$b +asetotal); for ($i=0;$i<5;$i++){ printf OUT "%-30s %-10s %-30s %-10s\n",$METAL[$i],$METALVAL[$i],$BASE[ +$i],$BASEVAL[$i]; }
      In HTML code will be like this:
      <table> <tr> <td>$fname</td> <td>$drctotal</td> <td>$metaltotal</td> <td>$basetotal</td> <td>$METAL[0] $METALVAL[0] </br> $METAL[1] $METALVAL[1] </br> $METAL[2] $METALVAL[2] </br> $METAL[3] $METALVAL[3] </br> $METAL[4] $METALVAL[4] </br> </td> <td>$BASE[0] $BASEVAL[0] </br> $BASE[1] $BASEVAL[1] </br> $BASE[2] $BASEVAL[2] </br> $BASE[3] $BASEVAL[3] </br> $BASE[4] $BASEVAL[4] </br> </td> </tr> </table>

      I want to put this HTML in my PERL script such that on executing script, output in the form of table get pops up on browser. I am new with PERL and any of the package can't be installed on my shared server linux. So please help me what would be the exact script to get the required output on browser.

Re: PERL-CGI on shared unix server
by Your Mother (Archbishop) on Jun 22, 2018 at 15:20 UTC

    Probably you are out of luck because few shared environments are going to let you run your own webserver or let you use any new ports which is necessary since 80 is taken. That said, this will work if your user account has permission. Plack is required for this solution. I rewrote your CGI with It's more reliable and once you're used to it, much easier to read.

    NetWallah's advice is also good, just plack is easier. See also HTTP::Server::Simple::CGI.

    Your, sorta, CGI

    #!/usr/bin/env perl # File called pm-1217158.cgi use strict; use warnings; use CGI ":standard"; # Import common functions. print header(), start_html("Hello, Nurse!"), h1("OHAI!"), end_html();

    Run a micro server

    cow@moo[323]~>plackup -L Shotgun -MPlack::App::WrapCGI \ -e 'Plack::App::WrapCGI->new( script => q{pm-1217158.cgi} )' HTTP::Server::PSGI: Accepting connections at http://0:5000/

    Then, if you were allowed to start it, you can visit your webhost at port 5000 to see your CGI. Just substitute the "0" in the URL above with the domain or IP.

Re: PERL-CGI on shared unix server
by taint (Chaplain) on Jun 22, 2018 at 19:51 UTC
    Greetings, rahu_6697, and welcome to Perl(monks). :-)

    Your question is somewhat vague, as to exact needs. But then again, Perl is new to you. So you get a pass this time. ;-)

    If I had to guess; I'd say you're looking to discover how to run/execute Perl scripts on your hosted web/internet account, and how to print (produce) Perl/CGI driven web pages. That's a pretty large topic to cover in a single reply on Perl Monks. But using your provided example in the OP; I might suggest using the feature/say . It's a bit like Heredoc ( Here Document ). Here's my version using Feature say:

    #!/usr/local/bin/perl -w use strict; use feature qw(say); print "content-type:text/html; charset=utf-8\n\n"; say qq~<!DOCTYPE html> <html lang="en"><head> <title>Hello, world!</title> </head><body> <h1>Hello, world!</h1> </body></html>~;
    As you can see, it's quite a bit simpler. While I'd have generally dumped all the commonly used stuff into a sub, of even made, or used an existing module that already provided that. All of that strays from your question. So I'll refrain from digression. :-)

    Secondly; you'll need to know where your hosing provider puts Perl for it's users. So you can use the right "shebang, or just "bang". That refers to the first line:#!/usr/local/bin/perl . Generally speaking, and as you noted you're on a *NIX platform. Your choices are either in /usr/bin/perl , or /usr/local/bin/perl. You can best determine that by way of which , or whereis . A couple of *NIX commands that tell you -- if you haven't already guessed; where stuff is. :-). So here's an example session:

    # which perl /usr/local/bin/perl
    # whereis perl perl: /usr/local/bin/perl /usr/local/lib/perl5/5.26/perl/man/man1/perl +.1.gz /usr/src/contrib/file/magic/Magdir/perl
    As you can see, the output is different; the command whereis being quite a bit more informative. :-)

    Lastly. You'll need/want to know where your hosting provider allows you to execute Perl scripts. This information should have been provided in the TOS (Terms of Service) you were required to agree to, when you signed up. Or it might also be in a FAQ (Frequently Asked Questions) they have on their site.

    Well, I think that probably covers it.


    Evil is good, for without it, Good would have no value
    λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1217158]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2023-03-26 19:02 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (63 votes). Check out past polls.