Here is how you figure out how to de-obfuscate this subroutine.
Sorry for the length
First lets tidy up the code, and add useful comments.
sub foo {
[
(
# ($#{$_[1]})
$#{
$_[
! ( $| | $| )
# $OUTPUT_AUTOFLUSH === $|
# $| is usually 0
# ! ( $| | $| )
# ! ( 0 | 0 )
# ! ( 0 )
# 1
]
}
*
# @{$_[1]}
@{
$_[
!!$_ ^ !$_
# !! 1 ^ ! 1
# ! 0 ^ 0
# 1 ^ 0
# 1
# !! 0 ^ ! 0
# ! 1 ^ 1
# 0 ^ 1
# 1
]
}
)
?
# @{$_[1]}
@{
$_[
!$. . !!$.
# $INPUT_LINE_NUMBER === $.
# $. starts at 1
# !$. . !!$.
# ! 1 . !! 1
# 0 . ! 0
# 0 . 1
# 01
]
}
[
# $_[0]
$_[
# @LAST_MATCH_START - @LAST_MATCH_END
# 0
@- - @+
]
%
# @{$_[1]}
@{
$_[
$= =~ /(?=)/ / !$` #( fix highlighting )`/
# $= is usually 60
# /(?=)/ will match, returns 1
# $` will be ''
# 1 / ! ''
# 1 / ! 0
# 1 / 1
# 1
]
}
..
# $#{$_[1]}
$#{
$_[
$? ? !!$? : !$?
# $CHILD_ERROR === $?
# $? ? !!$? : !$?
# 0 ? !! 0 : ! 0
# 0 ? 0 : 1
# 1
# 1 ? !! 1 : ! 1
# 1 ? 1 : 0
# 1
]
}
,
# ( 0 )
(
$) ? !$) : !!$)
# $EFFECTIVE_GROUP_ID === $)
# $) ? !$) : !!$)
# 0 ? ! 0 : !! 0
# 0 ? 1 : 0
# 0
# 1 ? ! 1 : !! 1
# 1 ? 0 : 1
# 0
)
..
# $_[0]
$_[
$- - $- # 0
# $LAST_PAREN_MATCH = $-
# 1 - 1 == 0
# 5 - 5 == 0
]
%
# @{$_[1]}
@{
$_[
$] / $]
# $] === The version + patchlevel / 1000 of the Perl interpreter.
# 1 / 1 == 1
# 5 / 5 == 1
]
}
-
# ( 1 )
(
!!$+ + !$+
# !! 1 + ! 1
# ! 0 + 0
# 1 + 0
# 1
)
]
:
# @{$_[1]}
@{
$_[
!!$^^ ^ !$^^
# !! 1 ^ ! 1
# ! 0 ^ 0
# 1 ^ 0
# 1
# !! 0 ^ ! 0
# ! 1 ^ 1
# 0 ^ 1
# 1
]
}
]
}
Now lets remove some of the obfuscation.
sub foo{
[
(
$#{$_[1]} * @{$_[1]}
)
?
@{$_[1]}[
( $_[0] % @{$_[1]} ) .. $#{$_[1]}
,
0 .. ( $_[0] % @{$_[1]} - 1 )
]
:
@{$_[1]}
]
}
Now that we have some idea of what is going on, lets name the variables.
sub foo{
my( $item_0, $arr_1 ) = @_;
my $len_1 = @$arr_1;
[
# This essentially just checks that the length of $arr_1 is greater than 1
( ( $len_1 -1 ) * $len_1 )
# ( ( $len_1 -1 ) * $len_1 )
# ( ( 5 -1 ) * 5 )
# 4 * 5
# 20
# 20 ? 1 : 0 == 1
# ( ( $len_1 -1 ) * $len_1 )
# ( ( 2 -1 ) * 2 )
# 1 * 2
# 2
# 2 ? 1 : 0 == 1
# ( ( $len_1 -1 ) * $len_1 )
# ( ( 1 -1 ) * 1 )
# 0 * 1
# 0
# 0 ? 1 : 0 == 0
# ( ( $len_1 -1 ) * $len_1 )
# ( ( 0 -1 ) * 0 )
# -1 * 0
# 0
# 0 ? 1 : 0 == 0
?
@{$arr_1}[
( $item_0 % $len_1 ) .. ( $len_1 -1 ),
0 .. ( $item_0 % $len_1 - 1 )
]
:
# If we get here, @$arr_1 is either empty or has only one element
@$arr_1
]
}
Lets refactor the code to make it a little bit more readable.
sub foo{
my( $item_0, $arr_1 ) = @_;
my $len_1 = @$arr_1;
if( $len_1 > 1 ){
return [
@{$arr_1}[
( $item_0 % $len_1 ) .. ( $len_1 -1 ),
0 .. ( $item_0 % $len_1 - 1 )
]
];
}elsif( $len_1 ){
return [ @$arr_1 ];
}else{
return [];
}
}