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

Hi everyone
In hope that someone might know this, i am using WWW::Curl and when setting one of the options, CURLOPT_KEYPASSWD, it expects to as stated in the manual the following : "Pass a pointer to a zero terminated string as parameter". I am assuming that since a Data pointer to a string is the equivalent of passing a string, that i need to pass a 0 terminated string as a parameter, or a pointer to one. Any ideas as to how i could create such a string?
Thank you
  • Comment on Converting a string to a C like pointer to a 0-terminated string

Replies are listed 'Best First'.
Re: Converting a string to a C like pointer to a 0-terminated string
by Corion (Patriarch) on Aug 29, 2008 at 10:45 UTC

    Perl strings are always written to memory with an additional \0 behind the end of the string, to make passing memory buffers around to C functions easier. If WWW::Curl does not provide a function that takes a string as a Perl scalar (which I would expect), then you can always create the parameters via pack and then pass the results in:

    my $passwd_buffer_pointer = pack 'p', $passwd; $curl->option($passwd_buffer_pointer+0);

    Of course, you now have the responsibilities that come with dealing with C buffers - especially, you have to find out whether WWW::Curl (or curl) makes a copy of your password or just stores the pointer and expects the memory to be still around when it makes up its mind about accessing it. So likely, you'll have to keep a reference to $passwd alive and never resize it or otherwise change it.

      Hey there, thank you for your reply. It seems you were right, WWW::Curl seems to have a problem, which is probably with libcurl itself. The option CURLOPT_KEYPASSWD does not seem to work and will give an error in Perl if you try to pass anything to it saying the argument CURLOPT_KEYPASSWD isn't numeric, which from what it seems like means that in this case KEYPASSWD is not defined. Using CURLOPT_SSLKEYPASSWD solves the problem and works fine. Hope this will help other people avoid the same issues in the future.
      Thank you
Re: Converting a string to a C like pointer to a 0-terminated string
by pjotrik (Friar) on Aug 29, 2008 at 14:05 UTC
    That sounds really strange, I believe that "Pass a pointer to a zero terminated string as parameter" only applies to the C interface. In Perl examples to libcurl, I don't see strings sent to $curl->setopt() treated in any special way. So my guess would be to simply use $curl->setopt(CURLOPT_KEYPASSWD, 'password');