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

I apologise to any puritan monks who may not appreciate this question.
I'm using Data::Dumper to store data structures (mainly hashes) which I then want to use in some C++ functions. What is the best way to reconstruct these structures in C++ (as I would with eval/do in perl).
Thanks for any help,
Ntav.
NAPH. not another perl hacker

Replies are listed 'Best First'.
Re: perl Data::Dumper'ed to C++
by wog (Curate) on Sep 06, 2001 at 03:57 UTC
    Your probably best off not using Data::Dumper, because to handle this in C++, you're going to need to write a parser/interperater for a signifigant subset of perl. (update: or, see below, you can put perl inside your C++ program.)

    You could also try Data::Denter which should be more easily parsed then Data::Dumper's output.

    You could also have your perl code write out the structures in a format designed specifically for the C++ program to easily read. How complex this syntax would be would depend on how complex the data you are reading is, though in many cases you could probably make it pretty simple (e.g. a flat file, key on one line, number of lines in value on the next. This is pretty easy to parse in C++.)

    Another option is too embed a perl interperator in your C++ program, or embed C++ in the perl program, and have your C++ use perl's API. But, this may very likely be inappropriate for your situation. update: for information on putting perl inside your C++ program see perlembed. For putting C++ in perl see perlxs.

      For putting C++ in perl see perlxs.

      Or check out Inline::CPP.

      bbfu
      Seasons don't fear The Reaper.
      Nor do the wind, the sun, and the rain.
      We can be like they are.

Re: perl Data::Dumper'ed to C++
by perrin (Chancellor) on Sep 06, 2001 at 07:26 UTC
    If you're just trying to exchange data between Perl and C++, use the marshalling code from SOAP or XML-RPC. It's already implemented in Perl and C++ and it uses XML for the interchange format.
Re: perl Data::Dumper'ed to C++
by clemburg (Curate) on Sep 06, 2001 at 12:44 UTC

    Another option would be to use any of the CORBA modules available (might be overkill, though). Also depends on the question if you want/need a type-safe or a type-less interface.

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com