Solved

how to use system function in perl

Posted on 2008-06-25
4
742 Views
Last Modified: 2012-06-27
Hello,

I wish to execute a c programm form a perl script.
say like this::I think did something wrong in the system function syntax.Please correct me.

$result = system("gcc file.cpp");
if($result == 0){
print "Cmpilation success\n";
chdir("C:\Dev-Cpp\bin\");
system("a.out");
}
elsif($result==1){
print "Compilation not success";
}

0
Comment
Question by:rakeshreddy42
  • 2
4 Comments
 
LVL 13

Expert Comment

by:kawas
ID: 21864871
maybe something like the following will work for you?


my $exit = system ("gcc", "file.cpp");

my $rez = _how_exit($exit);

print "Cmpilation success\n";

   $exit = system ("c:/Dev-Cpp/bin/a.out");

) if ( $rez == 0 );
 
 

sub _how_exit {

    my ($exit) = @_;

    my ($status) = $exit >> 8;

    my ($signal) = $exit & 255;

    return $status unless $signal;

    return $signal;

}

Open in new window

0
 
LVL 1

Expert Comment

by:danstuken
ID: 21864879

hi rakeshreddy,

Can you state where the problem actually lies?  From your message I'm guessing the call to system() isn't working.  You should note that system() doesn't necessarily return '1' on a failure but the result of the wait() system call (assuming such a call exists in windows).

You might find using the backticks operator (``) gives more useful information, e.g.:

print "Running....\n", `gcc file.ccp`, "\n";

You could try something like the following:


my @cmd = ("gcc" "file.cpp");

my $ret = system(@cmd);

if( $ret == 0 ){

   print "All good\n";

}else{

   print "Error with system(): ", $!, " returned : ", $ret, "\n";

   print "Exit code : ", $ret >> 8, "\n";

}

Open in new window

0
 
LVL 13

Expert Comment

by:kawas
ID: 21864902
sorry, premature submit!
my $exit = system ("gcc", "file.cpp");

my $rez = _how_exit($exit);

do {

   print "Cmpilation success\n";

   $exit = system ("c:/Dev-Cpp/bin/a.out");

} if ( $rez == 0 );
 

print "Compilation not success" if ( $rez != 0 );

 

sub _how_exit {

    my ($exit) = @_;

    my ($status) = $exit >> 8;

    my ($signal) = $exit & 255;

    return $status unless $signal;

    return $signal;

}

Open in new window

0
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
ID: 21867804
"\b" is the backspace character.
That's probably not the name of the directory
chdir("C:/Dev-Cpp/bin/");


$result==1 may be better as $result!=0
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

930 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now