bit shift operation on the status variable for IPC::Run

79 views Asked by At

I recently came across the following code for one of the perl module

  my @cat = qw( cat );
  use IPC::Run qw( run timeout );

   eval { run \@cmd, '<', "in.txt", \&out, \&err or die "cat: $?" };
  #here why status variable is being done Binary Shift Right
  my $status = $? >> 8;

I am unable to make why binary shift has been made.

2

There are 2 answers

0
ikegami On BEST ANSWER

I am unable to make why binary shift has been made.

Because you don't get the information you want if you don't.

$? contains three pieces of information:

  • What signal killed the process ($? & 0x7F)
  • Was a core dump produced (($? >> 7) & 1)
  • Was error code was passed to exit ($? > 8)
0
Sobrique On

It's to allow for 'nesting' of return codes.

So you can tell the difference between cat generating an error code, and run generating an error code (or whatever perl call you're making).