Maybe factor out the common $front in each return statement to make one return statement:
sub insert_after_first {
return if @_ < 3;
my ( $elem, $new, $front ) = splice @_, 0, 3;
return ( $front,
$front eq $elem
? ($new, @_ )
: insert_after_first( $elem, $new, @_ )
);
}
Update: Oo! In fact, you don't do anything with $new, except stick it back on the front of @_, so don't take it off:
sub insert_after_first {
return if @_ < 3;
my ( $elem, $front ) = (splice(@_, 0, 1), splice(@_, 1, 1));
return ( $front,
$front eq $elem
? @_
: insert_after_first( $elem, @_ )
);
}
Then, to change Lispish elegance to Perlish elegance (or madness), make your argument list the way you want it for the recursion, and use & without parentheses to recurse:
sub insert_after_first {
return if @_ < 3;
my $front = splice(@_, 2, 1);
return ( $front,
$front eq $_[0]
? @_[1..$#_]
: &insert_after_first
);
}
Caution: Contents may have been coded under pressure.
|