Passing integer by reference in extension

Questions and problems about using polymake go here.
xammy
Posts: 9
Joined: 07 Mar 2011, 17:32

Passing integer by reference in extension

Postby xammy » 07 Mar 2011, 18:36

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

User avatar
gawrilow
Main Author
Posts: 423
Joined: 25 Dec 2010, 17:40

Re: Passing integer by reference in extension

Postby gawrilow » 07 Mar 2011, 18:59

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.

xammy
Posts: 9
Joined: 07 Mar 2011, 17:32

Re: Passing integer by reference in extension

Postby xammy » 08 Mar 2011, 14:40

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.

User avatar
gawrilow
Main Author
Posts: 423
Joined: 25 Dec 2010, 17:40

Re: Passing integer by reference in extension

Postby gawrilow » 08 Mar 2011, 15:25

Well, you can still have two functions, just give them different names...


Return to “Helpdesk”