call c program from perl

Hi experts

i want to call a c program from my perl . i google about it but i couldnt find any good link.all i found where so complicated.  could you pls help me with any link or reference or sample code ? i need this for my co-op job

thanks a lot
Far22
Far22Asked:
Who is Participating?
 
ozoCommented:
find2perl . -name '*.004' -print
generates
#! /usr/local/bin/perl -w
    eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}'
        if 0; #$running_under_some_shell

use strict;
use File::Find ();

# Set the variable $File::Find::dont_use_nlink if you're using AFS,
# since AFS cheats.

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

sub wanted;



# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, '.');
exit;


sub wanted {
    /^.*\.004\z/s &&
    print("$name\n");
}



But
$extenion=".004";
@files=<*$extenion>;
is a simpler way to do it if you don't need to recurse into sub directories
If you're already read all the files into
@files=<*>;
then you can get just the *.004  files with
@files=grep{/\.004$/}@files;
0
 
ozoCommented:
perldoc -f system
perldoc -f qx
perldoc -f open

http://search.cpan.org/~ingy/Inline-0.44/C/C-Cookbook.pod
0
 
Adam314Commented:
Is your c program compiled?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Far22Author Commented:
thanks for quick response.
yes, c program is compiled.

0
 
Far22Author Commented:

actually i want to call the .exe file of my c code from the perl.
i read about the inline c program. i dont want to  put the "c code" in my  perl.

thanks
Far22
0
 
ozoCommented:
  $exit_status   = system("program.ext");
   $output_string = `program.exe`;
0
 
Kim RyanIT ConsultantCommented:
Is the PATH variable defined to include the location of your .exe? I f not you may need full path
# run an external command
system("/path/to/yourprogs/program.exe");
0
 
dbkrugerCommented:
Ozo gave you two answers.
To clarify, if you just need to run the code, and only need to see an integer return code (ie did it complete successfully or not) then:

$exit_status = system("program.exe");

If you need the output, you can alternatively run in back quotes:

$output_string = `program.exe`;

If you need to communicate bidirectionally, as I do, then an answer he gave to a question of mine some time ago is even better:


use IPC::Open2;

my $pid = open2(\*RDRFH, \*WTRFH, 'latlon2ij', 'gcmplt.cdf');
print WTRFH $y,$x;
my ($i, $j) = <RDRFH>;

In this one, you run the program "latlon2ij" passing gcmplt.cdf as an argument. You can print input to the program, and read output back from it.

So it's all from ozo originally, but I hope I have made your options clearer.
0
 
Far22Author Commented:
thanks a lot for your help.

here they have a general c code(.exe) which is used in shell program. this code get a file as input and clean up the file from unwanted character and out put to other file. in my perl code i looping through a directory get each file and run c code like: `myC_code.exe <file1>file2`
it works fine if i just do this in the loop but if i want to read file2 in each loop and do something with it , i noticed that in each loop after `myC_code.exe <file1>file2` file2 hasnt created yet .

any help be appreciated a lot

Far22
0
 
Adam314Commented:
The file2 should be created as soon as the myC_code.exe finishes.  Are you checking it after this program is finished?  Is the myC_code.exe executing like it is supposed to?
0
 
ozoCommented:
What is your loop?
0
 
Far22Author Commented:
thanks for your hint . it is working fine now.
0
 
Far22Author Commented:
Hi experts

i want to find files with name like:   *.004 under a folder. i read all files under the  folder but i want to grab files with *.$extenion names.
i looked at find2perl examples but i didnt understand how to use it. is there any simple way to do it i appreciate any help.

thanks
Far22
0
 
Adam314Commented:
@files=<*.004>;
0
 
Far22Author Commented:
thanks a lot for your help

Far22

0
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.

All Courses

From novice to tech pro — start learning today.