#949 [perl] std::string arg cannot be passed int, double

open
nobody
perl (97)
5
2008-09-24
2008-09-24
Justin Vallon
No

A function taking a std::string as an argument cannot be passed an integer in perl. Thus, for a function:

void f(const std::string& s);

The following behave differently:

f("1");
f(1)

This is counter to perl's automatic stringify.

A patch was submitted in Req #1798728, but it was rolled back because it was a little too aggressive (it turned everything into strings).

This patch only stringifies ints, unsigned ints, and doubles. I have also attached a patch to add a test for passing string,int,double into a string.

Index: Lib/perl5/perlstrings.swg

--- Lib/perl5/perlstrings.swg (revision 10879)
+++ Lib/perl5/perlstrings.swg (working copy)
@@ -6,7 +6,7 @@
SWIGINTERN int
SWIG_AsCharPtrAndSize(SV *obj, char** cptr, size_t* psize, int *alloc)
{
- if (SvPOK(obj)) {
+ if (SvPOK(obj) || SvIOK(obj) || SvUOK(obj) || SvNOK(obj)) {
STRLEN len = 0;
char *cstr = SvPV(obj, len);
size_t size = len + 1;

Discussion

  • Justin Vallon
    Justin Vallon
    2008-09-24

    Add some perl std::string tests

     
  • Justin Vallon
    Justin Vallon
    2008-09-24

    • summary: std::string arg cannot be passed int, double --> [perl] std::string arg cannot be passed int, double
     
  • Justin Vallon
    Justin Vallon
    2008-09-25

    Also allow int, unsigned int, double in string conversion