简体   繁体   中英

running a perl cgi script from a webpage (html)

I have been searching for a tutorial on how to run a perl program from an html webpage. I cannot find a tutorial or even a good starting point that explains clearly how to do this...

What I'm trying to do is use WWW::mechanize in perl to fill in some information for me on the back end of a wordpress site. Before I can do that I'd like to just see the retrieved html displayed in the browser like the actual website would be displayed in the browser. Here is my perl:

print "Content-type: text/html\n\n";
use CGI;
use WWW::Mechanize;
my $m = WWW::Mechanize->new();



use WWW::Mechanize;
$url = 'http://www.storagecolumbusohio.com/wp-admin';
$m->post($url);
$m->form_id('loginform');
$m->set_fields('log' => 'username', 'pwd' => 'password');
$page = $m->submit();
$m->add_handler("request_send",  sub { shift->dump; return });
$m->add_handler("response_done", sub { shift->dump; return });
print $page->decoded_content;

This code works from the command prompt (actually I'm on mac, so terminal). However I'd like it to work from a website when the user clicks on a link.

I have learned a few things but it's confusing for me since I'm a perl noob. It seems that there are two ways to go about doing this (and I could be wrong but this is what I've gathered from what iv'e read). One way people keep talking about is using some kind of "template method" such as embperl or modperl. The other is to run the perl program as a cgi script. From what I've read on various sites, it seems like cgi is the simplest and most common solution? In order to do that I'm told I need to change a few lines in the httpd.conf file. Where can I find that file to alter it? I know I'm on an apache server, but my site is hosted by dreamhost. Can I still access this file and if so how?

Any help would be greatly appreciated as you can probably tell I don't have a clue and am very confused.

To use a cgi script on dreamhost , it is sufficient to

  1. give the script a .cgi extension
  2. put the script somewhere visible to the webserver
  3. give the script the right permissions (at least 0755)

You may want to see if you can get a toy script, say,

#!/usr/bin/perl
print "Content-type: text/plain\n\nHello world\n";

working before you tackle debugging your larger script.

That said, something I don't see in your script is the header. I think you'll want to say something like

print "Content-type: text/html\n\n";

before your other print call.

I would suggest that you test your code first on your local server. I assume you are using windows or something similar with your questions, so use xamp http://www.apachefriends.org/en/xampp.html or wamp http://www.wampserver.com/en/ or get a real OS like http://www.debian.org (you can run it in a vm as well).

You should not print the content type like that, but use "print header", see this page: http://perldoc.perl.org/CGI.html#CREATING-A-STANDARD-HTTP-HEADER%3a

Make sure you have your apache server configured properly for perl, see also these commons problems: http://oreilly.com/openbook/cgi/ch12_01.html

Also see How can I send POST and GET data to a Perl CGI script via the command line? for testing on the command line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM