Wednesday, September 25, 2013

Perl & Curl have my Head in a Whirl

I had recently written a PHP script to read a URL using cURL. Today, I thought I'd convert it into a Perl script. PHP has built in support for cURL, using functions like curl_init(), curl_exec() and curl_close(). In Perl, you have to install and use a CPAN module called WWW::Curl. At first glance, it seemed a simple task. But the problem is that on Windows, you can't simply download the module using the usual command "cpan WWW::Curl". You have to download the source file package and manually install the module. For this, you also need the source package of the cURL libraries. You can't just download and use the "curl" binary, the way you would if you just wanted to run the "curl" command from the DOS prompt. Anyway, here are the steps to follow...

* Ensure a C compiler is installed and available on the PATH. If not, get GCC by installing the Perl MinGW package:

ppm install MinGW

* Download the latest cURL libraries for mingw from the cURL website:

http://curl.haxx.se/download.html

* Unzip the folder somewhere (say C:\curl-7.32.0)

* Download the CPAN module WWW::CURL from CPAN:

http://search.cpan.org/~szbalint/WWW-Curl-4.15/

* Unizp the folder somewhere (say on your Desktop). Edit the Makefile.PL, making the following changes:

1. Comment the lines following the comment "This is a hack."

2. Change the "my @includes..." line to:

my @includes = qw(C://curl-7.32.0//include);

3. Change the "my ($cflags..." line to:

my ($cflags,$lflags, $ldflags) = ('-I"C://curl-7.32.0//include"','-L"C://curl-7.32.0//lib"','-lcurldll -lcurl');

 4. Change the "open(H_IN, "-|", "cpp"" line to:

my $cmd = "cpp $curl_h";
open(H_IN, $cmd) and $has_cpp++;

5. Change the "'LIBS'         => "$ldflags $lflags..." line to:

'LIBS'         => "$lflags $ldflags",        # e.g., '-lm'

* Note that this last change is very important, or else you'll spend 2 hours debugging a linker error - as I unfortunately did

* Run the command:

perl Makefile.PL

* Run the command:

dmake

* Run the command:

dmake install

* Try running your test Perl script. If you get an error, usually as a popup, that says "The procedure entry point ERR_remove_thread_state...", just add "C:\curl-7.32.0\bin" at the beginning of your PATH. This error occurs when your system picks up a wrong version of "libeay32.dll", probably from your installation of SSL VPN or some other application

* Another issue you could face is that your Perl script works fine from the command line but when you run it in Apache, you get an internal server error while loading the WWW::Curl Perl Module, which you can confirm by checking the Apache error log. This is because the "C:\curl-7.32.0\lib" folder is not in your PATH, and needs to be added

Now you're ready to go - write that Perl program to read a URL using cURL. Thanks to Niranjan Prithivraj, whose post was partially helpful during my installation. Hope this post would be useful to someone else, just like Niranjan's post helped me

No comments:

Post a Comment