perl algorithm