• C

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}
or
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?
LVL 1
feldmaniAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
Does it say $ in the error message?
brettmjohnsonCommented:
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.  
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

ozoCommented:
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

ozoCommented:
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.
Adam314Commented:
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?
martinCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
feldmaniAuthor Commented:
Thanks, martin.  I think that might work. I will try it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.