0

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.

DavidO
  • 13,812
  • 3
  • 38
  • 66
Digital Brent
  • 1,275
  • 7
  • 19
  • 47
  • Of course, if you use a host then getting perl on there might be more work but: http://www.thesitewizard.com/archive/addcgitoapache.shtml – TheZ Aug 13 '12 at 20:59

2 Answers2

1

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.

mob
  • 117,087
  • 18
  • 149
  • 283
  • Thanks! This still took me a while, but I finally figured it out thanks to your help. The link you posted "To use a cgi script on dreamhost" in combination with this site: http://www.javascriptkit.com/howto/adashimar/index.shtml and this one: http://www.stadtaus.com/en/tutorials/chmod-ftp-file-permissions.php was very helpful. I was able to figure it out from there. Thanks for the help @mob! – Digital Brent Aug 14 '12 at 21:53
  • FYI for anyone facing a similar problem or stuck in the same place, I didn't have to change the file extension to cgi. I used the folder method with the .htaccess file in it that will run all the files in that folder as cgi scripts (see the dreamhost wiki link posted by @mob above). Once I did that I had to go in to filezilla, right click on the perl file and click "file permissions". Then give the file the 755 numeric value in the permissions form. Took me a while to figure it out because all the tuts out there assumed you know something and left a big piece of the puzzle out. – Digital Brent Aug 14 '12 at 21:59
0

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.

Community
  • 1
  • 1
h4ck3rm1k3
  • 2,060
  • 22
  • 33