[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

embedding Perl into C

Posted on 2006-04-06
10
Medium Priority
?
257 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:feldmani
10 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 16396742
Does it say $ in the error message?
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 16397968
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}"



0
 
LVL 1

Author Comment

by:feldmani
ID: 16405231
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.  
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 85

Expert Comment

by:ozo
ID: 16411996
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.
0
 
LVL 1

Author Comment

by:feldmani
ID: 16412333
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

0
 
LVL 85

Expert Comment

by:ozo
ID: 16413248
it should be Config not config
But why do you need Config?
0
 
LVL 1

Author Comment

by:feldmani
ID: 16416992
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.
0
 
LVL 39

Expert Comment

by:Adam314
ID: 16418158
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?
0
 
LVL 3

Accepted Solution

by:
martin earned 1500 total points
ID: 16418855
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.

0
 
LVL 1

Author Comment

by:feldmani
ID: 16419126
Thanks, martin.  I think that might work. I will try it.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question