Can you move the editor invocation to a helper function? e.g.

    $preput = edit_command($term->GetHistory->[-2]);
    next;