# Calling a c program using perl script

**URL:** <https://community.unix.com/t/calling-a-c-program-using-perl-script/266141>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [May 20, 2010, 1:58pm UTC](https://community.unix.com/t/calling-a-c-program-using-perl-script/266141 "2010-05-20T13:58:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gkbond](https://community.unix.com/letter_avatar/gkbond/32/5_5575768a8748004e209b776fc1b2916d.png) [@gkbond](https://community.unix.com/u/gkbond)\
**Post date:** [May 20, 2010, 1:58pm UTC](https://community.unix.com/t/calling-a-c-program-using-perl-script/266141/1 "2010-05-20T13:58:46Z")

</div>

On bash I run precompiled c Program as follows:

./create\_cust 1 10000 US S \> us\_cust.csv

create\_cust is a c program and requires 4 parameters.  
I am redirecting the output of this program to csv file

I need to run this same program in perl

I am aware of exec command though not sure it will work.

Can anybody will direct me to a code which is calling a parameterised c program through perl and how to redirect its output to csv file???

---

<div class="post-metadata">

**Author:** ![pseudocoder](https://community.unix.com/user_avatar/community.unix.com/pseudocoder/32/666_2.png) [@pseudocoder](https://community.unix.com/u/pseudocoder)\
**Post date:** [May 20, 2010, 2:12pm UTC](https://community.unix.com/t/calling-a-c-program-using-perl-script/266141/2 "2010-05-20T14:12:34Z")

</div>

> [@gkbond](#):
>
> I am aware of exec command though not sure it will work.

Why not simply try and see what happens 😕  
I'd first try it on the command line, if it works then put it in the script.

```plaintext
perl -e 'exec("create_cust 1 10000 US S > us_cust.csv");'

```

or

```plaintext
perl -e 'system("create_cust 1 10000 US S > us_cust.csv");'

```

Inside the perl script, something like following should also work:

```plaintext
my $par1=1;
my $par2=10000;
my $par3='US';
my $par4='S';
system("create_cust $par1 $par2 $par3 $par4 > us_cust.csv");

```

---

<div class="post-metadata">

**Author:** ![gkbond](https://community.unix.com/letter_avatar/gkbond/32/5_5575768a8748004e209b776fc1b2916d.png) [@gkbond](https://community.unix.com/u/gkbond)\
**Post date:** [May 20, 2010, 2:16pm UTC](https://community.unix.com/t/calling-a-c-program-using-perl-script/266141/3 "2010-05-20T14:16:25Z")

</div>

It gives error that create\_cust is not internal / external command or oparable program or batch file

---

<div class="post-metadata">

**Author:** ![pseudocoder](https://community.unix.com/user_avatar/community.unix.com/pseudocoder/32/666_2.png) [@pseudocoder](https://community.unix.com/u/pseudocoder)\
**Post date:** [May 20, 2010, 2:20pm UTC](https://community.unix.com/t/calling-a-c-program-using-perl-script/266141/4 "2010-05-20T14:20:44Z")

</div>

And

```nohighlight
perl -e 'exec("./create_cust 1 10000 US S > us_cust.csv");'

```

?

---

<div class="post-metadata">

**Author:** ![gkbond](https://community.unix.com/letter_avatar/gkbond/32/5_5575768a8748004e209b776fc1b2916d.png) [@gkbond](https://community.unix.com/u/gkbond)\
**Post date:** [May 20, 2010, 2:22pm UTC](https://community.unix.com/t/calling-a-c-program-using-perl-script/266141/5 "2010-05-20T14:22:54Z")

</div>

. not recognised as external or internal command

---

<div class="post-metadata">

**Author:** ![pseudocoder](https://community.unix.com/user_avatar/community.unix.com/pseudocoder/32/666_2.png) [@pseudocoder](https://community.unix.com/u/pseudocoder)\
**Post date:** [May 20, 2010, 2:34pm UTC](https://community.unix.com/t/calling-a-c-program-using-perl-script/266141/6 "2010-05-20T14:34:49Z")

</div>

Strange... I wonder if a simple

```nohighlight
perl -e 'exec("ls -l")'

```

will work? If this won't work too, than there might be something wrong with your Perl.  
However I have tried to run a c program before I posted here and it worked for me ☹

I assume you were in directory where your c program is stored when you issued the Perl command? Else one needs to specify the full path to that program.

---

<div class="post-metadata">

**Author:** ![gkbond](https://community.unix.com/letter_avatar/gkbond/32/5_5575768a8748004e209b776fc1b2916d.png) [@gkbond](https://community.unix.com/u/gkbond)\
**Post date:** [May 20, 2010, 2:57pm UTC](https://community.unix.com/t/calling-a-c-program-using-perl-script/266141/7 "2010-05-20T14:57:12Z")

</div>

I am using cygwin and have installed activestate for perl on windows.

I am going in bash of cygwin and going to directory where program executable is present and executing above command.

It gives error.

The other code which u gave for ls -l runs properly on cygwin

---

<div class="post-metadata">

**Author:** ![pseudocoder](https://community.unix.com/user_avatar/community.unix.com/pseudocoder/32/666_2.png) [@pseudocoder](https://community.unix.com/u/pseudocoder)\
**Post date:** [May 20, 2010, 3:24pm UTC](https://community.unix.com/t/calling-a-c-program-using-perl-script/266141/8 "2010-05-20T15:24:29Z")

</div>

All right, this is the last idea I have, namely to check if your program has to be in PATH in order to work:  
run "which ls" to see in which directory the ls command is present, the output will most likely be /bin/ls  
Now copy the create\_cust file in /bin and try to run (from any directory):

```nohighlight
perl -e 'exec("create_cust 1 10000 US S > us_cust.csv");'

```

If this works, delete the two files (create\_cust and us\_cust.csv) from /bin and add the actual directory, where the create\_custom file is located to the PATH variable.  
Hope I was clear?  
Let me know how it goes.
