Calling Perl Methods/API from CPP

Problem : Create a single object of the perl(Moose) class defined below. Don't create the object more than once.
Using the object call the three methods with parameters defined in the class and display their output in a C++ program.
You could create an equavalent class in C++ and internally call the perl methods for processing part.
Objective of the problem is to use Perl methods/APIs in C++ program.
E.g. I should be able to do "my_cpp_obj.return_scalar("Random value")" in a c++ program.


Possible Hint:
1. Create a child process in which perl object of the perl class is created once.
2. Use a while loop in child process which will keep reading what is written on its input stream.
Capture the function name and arguments. Use "eval" to execute the function and write the output on output stream of child process.
3. Json or any other encoding could be used to send/receive arrays/hashes.
4. Maybe pipes could be used.



***********MethodClass.pm
package MethodClass;

use strict;
use warnings;

use Moose;

# Takes object of class and a scalar value as inputs.
sub return_scalar {
my ( $self, $arg ) = @_;
return "A Scalar Value";
};

# Takes object of class and an array as inputs.
sub return_array {
my ( $self, @arr ) = @_;
map { $_ = $_*2 } @arr;
return @arr;
}

# Takes object of class and a hash as inputs.
sub return_hash {
my ( $self, %hash ) = @_;
foreach my $key ( keys %hash ) {
$hash{$key} = $key . $hash{$key};
}
return %hash;
}

return 1;
Are you ASKING how to call Perl methods from C++ code?

Copy-'n'pasting your assignment without showing you have done some internet foo skills workout is not exactly cat-nip to people wanting to help any worthy grasshoppers.

https://warwick.ac.uk/fac/sci/moac/people/students/2007/nigel_dyer/phd/software/callingperlfromcpp/

A-maze-ing what the internet can scrape up if asked politely.

https://duckduckgo.com/?q=calling+perl+methods+from+C%2B%2B&t=ffsb&ia=web
Topic archived. No new replies allowed.