Perl calling unix system commands

if ( system ("/bin/cat $File1 >> $File2") ) {
print("#WARNING RAISED : /bin/cat File1 >> File2  - FAILURE!\n"); } 

I came across this code, would appreciate if someone can tell me if my understanding is correct?

the perl code tell the system to cat file 1 into file 2, if command fails, print the warning statement?
I am a little confused by the test condition:
if ( system ("/bin/cat $File1 >> $File2") ) Is it testing for successful writing or unsuccessful writing ?

I think the code is correct. You actually tested it on your machine. Did you get the expected behaviour of having the error printed on error?

You do not have to be confused by the semantics of this usage. It appears bizarre, but it actually works that way because system() returns a value that depends on the return value of the command executed. If you read the perldoc for system() you will find that the actual return status is encoded in the least significant 8 bits, and higher bits will be set in the event of other errors. In general, most Unix shell commands have the convention of 0 return status being successful, while other values reflect error status. Therefore, the construct you quoted actually means to detect anything non-zero, that indicates error status. Of course it deviates from typical programming practice that zero is considered false, error, or anything like NULL.

Yup, i tested it.

So the if ( system ("/bin/cat $File1 >> $File2") ) Is testing for unsuccessful writing.

it sure does not follow the normal if else format.

Actually it does :slight_smile:
system returns a success/failure value which 'if' tests

As I said, it's just because of the peculiarity of system() and the shell return status. If you replace the command executed with one that returns a non-zero value to indicate success status for whatever reasons, then you will need to adjust the test to reflect that.