3

I've to read and parse HTML file and populate a data structure (in C++). I'm planning to do the parsing using perl (so I can use some other perl modules.). My requirement is this.

  1. Get the file from gui (C++ code)
  2. Pass it to perl.
  3. Parse file on perl side (perl script using some other perl modules), populate the C++ structure
  4. Continue working on C++ side with the populated structure.

I'm reading about extending and embedding perl, but unable to figure out the correct procedure. Any help would be greatly appreciated.

4
  • 2
    Quick fix: have the Perl script output a simplified text format, read that through a pipe on the C++ side, build the structure. Commented Jun 11, 2011 at 19:34
  • This, I've thought of and decided against it. A system call to perl, and parsing on C++-side. The data structure (class) may contain pointers to other classes. so I want to avoid this. Commented Jun 11, 2011 at 19:46
  • have you tried serializing the data using Data::Dumper for example. You are going to have a real hard time passing Perl objects out to a C++ program intact. Commented Jun 11, 2011 at 20:05
  • I'd do what @larsmans suggests. Parse it in a separate Perl program, output it in some format C++ can understand (JSON, YAML, XML...) and let C++ turn that into a struct. Embedding Perl is fraught with peril and you won't be able to move Perl classes and objects easily into C++ structs. Commented Jun 12, 2011 at 3:36

3 Answers 3

4

In your reading did you find perlembed in Perl's documentation? That's the definitive resource for learning how to embed Perl in a C/C++ program. The author of the document was one of the original mod_perl developers, I believe.

I don't think that embedding Perl for a trivial task would be the easiest solution when compared to doing a system call to perl and parsing the result, but for more involved needs it's certainly a solution.

Sign up to request clarification or add additional context in comments.

1 Comment

You don't even have to do much parsing if you use pack to return bytes that can be slopped straight into a struct.
3

I've used swig to connect C++ and Python. The documentation says it works for Perl, also.

Comments

1

Yet another alternative is to have perl drive your C++ code. Write a function that has a perl-side implementation that calls a corresponding C-side implementation. Do man perlxs and perlxstut for more info.

Edit: Or read it online at http://perldoc.perl.org/perlxs.html and http://perldoc.perl.org/perlxstut.html.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.