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

Hi venerable monks
In my application I used SWIG to wrap c++ functions and call these functions in perl script.

One of these function is as follows
int sendMsg(const char* req, char* response,int respSize)

which I call from my script as
$c->sendMsg("hello",$response,$respsize);

Can anyone of you let me know:-
1)if this is correct way of calling with "$response" parameter which is char*.
2) if this is correct then, how to print in script? I tried print ($response) & it doesnt seem to work.

regards
Mike

Replies are listed 'Best First'.
Re: print char * in perl
by Joost (Canon) on Jun 13, 2006 at 13:08 UTC
    I don't know how SWIG handles this, or even if it handles "output parameters" correctly, but as a first shot, make sure $response is already a string of size $respsize before calling the function, and check the output of sendMessage (looks like a status code).

    my $response = " " x 100; # 100 spaces + 0 char my $respsize = length $response; # 100 my $status = $c->sendMsg("hello",$response,$respsize); print "status = $status, response is '$response'\n";
Re: print char * in perl
by syphilis (Archbishop) on Jun 13, 2006 at 13:03 UTC
    I don't know SWIG at all - perhaps there are others here that can offer advice. But on most of the forums (fora ?) that I peruse there's not much advice forthcoming wrt SWIG - though there are plenty that are willing and able to offer good advice wrt Inline::C and Inline::CPP.

    The function prototype would suggest that the task is trivial with Inline::C (or Inline::CPP) - so maybe try that path if you don't get a helpful answer to the question that you actually asked.

    My hunch is that something like '$ret = sendMsg("hello", $response, $respsize);' ought to work if $respsize is set to the appropriate value .... but I'm really going out on a limb ... and a very thin one at that ... in fact I can feel it breaking away as I'm typing ...

    Cheers,
    Rob