Page 1 of 1

Passing integer by reference in extension

Posted: 07 Mar 2011, 18:36
by xammy
Hello,

I'm working on an extension and want to pass an integer argument by reference. I tried the following perl-glue code:

Code: Select all

UserFunction4perl("# Tests a given //matrix// for k-modularity, without certificates. It also computes k." "# @param Matrix<int> matrix" "# @param int& k" "# @return Bool", &is_k_modular_compute,"is_k_modular(Matrix<Integer>, $&)");
Neither "$" nor "$&" or anything else I tried has worked. Can you help me here?

The function signature is as follows:

Code: Select all

bool is_k_modular_compute(const Matrix <Integer>& matrix, int& ko)
Thank you!
Matthias Walter

Re: Passing integer by reference in extension

Posted: 07 Mar 2011, 18:59
by gawrilow
You can't pass perl primitive types to C++ code by reference, they are not stored in C++ representation. If you want to return several values from a function, please use std::pair or perl::ListReturn.

Alternatively, your function would look more adhering to perl habits if it would return k if the matrix is k-modular and undef otherwise. Again, perl::ListReturn will facilitate this. Just return an empty list in negative case.

Re: Passing integer by reference in extension

Posted: 08 Mar 2011, 14:40
by xammy
Thank you for the helpful reply! I managed to get it working with perl::ListReturn. The only drawback is that there was another version of the routine which did not compute k (to save running time) which I removed now. But I think it's okay as the running time gain is not a lot.

Re: Passing integer by reference in extension

Posted: 08 Mar 2011, 15:25
by gawrilow
Well, you can still have two functions, just give them different names...