cURL / Mailing Lists / curl-users / Single Mail

curl-users

Re: Help for novice PERL user

From: Cris Bailiff <c.bailiff+curl_at_awayweb.com>
Date: Tue, 24 Sep 2002 11:09:56 +1000

On Tue, 24 Sep 2002 07:22, Lane Parkes wrote:
> Hi,
>
> I’m a novice PERL user so I apologize if this is something simple. I’ve
> read through the newsgroups, done a GOOGLE search and got totally confused
> by the PERL documentation trying to implement this (I’ve spent days trying
> to get it working when I’m sure it should be something easy).

Unfortunately, using libcurl directly is not always that easy for a novice
programmer (in any language) - you sometimes have to know a bit about http
and what effect you are actually trying to achieve.

If you really haven't programmed in perl before, you're going to find using
Curl::easy a bit of a learning curve - you'll probably need to understand a
little bit about subroutine callbacks and arrays. If you're comfortable with
that, then you need to study the curl_easy_setopt manual page, which
describes all the options and settings you can use. There are many options,
which can be confusing, but fortunately, Daniel has just re-arranged that
page into a much more sensible order. Have a look at this page whilst reading
the test script examples (described below):

http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

(When the page says 'pass a long' or 'string', just use a perl variable with
a number or text in it.)

> I have a web form that allows uploading of a file when you pass it a couple
> of parameters and a file to upload.
>
> I can do this from the command line using the following example…
>
> Curl –u NAME:PASSWORD –F Action=Upload –F JobName=Myjob –F
> FileHandle=Myfile MyURL
>
> I’m now trying to do the same thing using cURL:EASY. Could anyone please
> post a simple example or put me on the right track for this?

There are a whole bunch of examples in the 't/' (test) directory of the
Curl::easy distribution, including a couple of different file uploads:

    http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/curl/perl/t/

Even Daniel managed to write a simple example :-) :

http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/curl/perl/examples/basicfirst.pl?rev=HEAD&content-type=text/vnd.viewcvs-markup

> I’m happy to post what I’ve done so far - but to be honest I’ve tried a
> bunch of things and nothing seems correct.

If you are trying to use a recent version of curl, you'll also find yourself
affected by the missing support for curl_forms (which make uploading and
posting easier) in Curl::easy. There's the start of code in the CVS, but it's
nowhere near complete, due to the wonderful flexibility of the curl_forms
system (thanks Daniel!). Without this, you have to pretty much create your
own HTTP content when doing an upload via POST (which isn't hard, but it's a
bit too complex to explain here!).

> I really would like to get it working with cURL:EASY as this seems to be
> the more robust solution compared to using the command line.

It's certainly more flexible, and easier to integrate with other
functionality, but it does give you a fairly 'raw' libcurl interface (which
is 'easy'ier for some than others!).

I don't want to put you off Curl::easy un-necessarily, but as a beginner in
perl, you might find using the standard perl LWP library a bit more
straightforwards ('perldoc LWP') (or, you might not!). Unless you need curls'
greatly enhanced performance and wide-range of features, you'll probably be
fine with LWP.

Someone on the libcurl list offered/suggested that they were working on a
libcurl-based backend for LWP, which would be ideal - a much more perl-like
interface, with libcurl doing the heavy lifting. I don't know if they used
Curl::easy as the glue (I hope so) between perl and libcurl, but I've long
intended there should be a 'wrapper' interface around Curl::easy to make it
easier to do easy things.

Cris

-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
Received on 2002-09-24