• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

embedding Perl into C

I have an already written C program that also I have access to, is very spaghetti so we try not to work with it.  I need to make modifications to the output, so I wrote a Perl program to do it, but I think I will need to run the perl code from inside C.  
I found a way to do it in a book I have, but I don't know how to get the library directory needed since when I type
perl -MConfig -e "print $Config{archlib}
perl -Mconfig -e "print $Config{cc}
I get an  error saying can't located $Config in @INC.  
Any ideas on what to do for me?
1 Solution
Does it say $ in the error message?
Rather than try to embed perl into your C program, why not pipe the output of the C program to the Perl program at the command level?  This is typically how output post-processing is done.

myCprogram | perl -MConfig -e "print $Config{archlib}"

feldmaniAuthor Commented:
well, here is the problem. The C program creates two different files, a .wrk and a .txt.  
The file that the perl modifies is the .wrk since the .txt would be MUCH harder to modify.  I don't even know if unix would allow this to happen since it might still be locked for writing/reading.  Hopefully it's open for at least reading which then I could get it to work.  
Get Certified for a Job in Cybersecurity

Want an exciting career in an emerging field? Earn your MS in Cybersecurity and get certified in ethical hacking or computer forensic investigation. WGU’s MSCSIA degree program was designed to meet the most recent U.S. Department of Homeland Security (DHS) and NSA guidelines.  

Unix would probably allow it to be modified, however, if the C program has kept it open, the C program would overwrite the modified version when it does a close on the file.
Can you send .wrk to STDOUT so that the above pipe can work?
In what context did you type the comands which got an error?
You should probably be using ' instead of " so that the shell does not interpret the $Config,
but the error message you report is not the one I would have expected.
feldmaniAuthor Commented:
No,  the C program closes it, but I don't know if it instantly allocates the rights back to the program.  I MIGHT be able to send the .wrk to STDOUT, although I doubt it.  I will try it however.

I typed the commands above in the unix shell script...Not sure what you mean by context. I tried it with the ' marks, and it still didn't work.

The full error is more like this  (I don't want to put down the real directory since I don't know what it is):
Can't locate config.pm in @INC (@INC contains: /foo/perl5/foo/foo/fake_variables .).
BEGIN failed--compilation aborted

it should be Config not config
But why do you need Config?
feldmaniAuthor Commented:
Cause when I was reading how to embedd the perl into C code, the O'reilly book said I need to get those.  
I've never had to run anything inside C, so I don't know exactly how to do it.  I found several things in books and such on the topic, and they all said I would need this data before I can make perl work in C.
Could you have the perl program run after the c program is finished (and done making changes to the .wrk file).  Then have the perl program make the changes necessary?
When you open the .wrk file for writing, instead open a pipe to a perl script (using the C function popen). The perl script will read its STDIN and write the .wrk file.

See: http://www.opengroup.org/onlinepubs/007908799/xsh/popen.html

This way you won't need -MConfig.

feldmaniAuthor Commented:
Thanks, martin.  I think that might work. I will try it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

IT Degree with Certifications Included

Aspire to become a network administrator, network security analyst, or computer and information systems manager? Make the most of your experience as an IT professional by earning your B.S. in Network Operations and Security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now