• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 809
  • Last Modified:

how to use system function in perl

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
rakeshreddy42
Asked:
rakeshreddy42
  • 2
1 Solution
 
kawasCommented:
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
 
danstukenCommented:

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
 
kawasCommented:
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
 
ozoCommented:
"\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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now