Is there a possibility of calling the function main without the function _end_win being called? 
#!/usr/bin/env perl
use warnings;
use strict;
use 5.10.0;
use Term::ReadKey;
use constant {
    NEXT_getch      => -1,
    CONTROL_C       => 0x03,
    CONTROL_D       => 0x04,
    KEY_ENTER       => 0x0d,
    KEY_RIGHT       => 0x1b5b43,
    KEY_LEFT        => 0x1b5b44,
};
say main();
sub main {
    my $arg = {};
    $arg->{handle_out} = *STDOUT;
    _init_scr( $arg );
    while ( 1 ) {
        my $c = _getch( $arg );
        if ( ! defined $c ) {
            _end_win( $arg );
            warn "EOT";
            return;
        }
        next if $c == NEXT_getch;
        given ( $c ) {
            when ( $c >= 97 && $c <= 122 ) {
                print chr $c;
                $arg->{string} .= chr $c;
            }
            when ( $c == KEY_RIGHT ) {
                print '>';
                $arg->{string} .= '>';
            }
            when ( $c == KEY_LEFT ) {
                print '<';
                $arg->{string} .= '<';
            }
            when ( $c == CONTROL_D ) {
                _end_win( $arg );
                return;
            }
            when ( $c == CONTROL_C ) {
                _end_win( $arg );
                print STDERR "^C";
                kill( 'INT', $$ );
                return;
            }
            when ( $c == KEY_ENTER ) {
                _end_win( $arg );
                return $arg->{string};
            }
        }
    }
}
sub _init_scr {
    my ( $arg ) = @_;
    $arg->{old_handle} = select( $arg->{handle_out} );
    $arg->{backup_flush} = $|;
    $| = 1;
    Term::ReadKey::ReadMode 'ultra-raw';
}
sub _end_win {
    my ( $arg ) = @_;
    print "\n\r";
    Term::ReadKey::ReadMode 'restore';
    $| = $arg->{backup_flush};
    select( $arg->{old_handle} );
}
sub _getch {
    my ( $arg ) = @_;
    my $c1 = ReadKey 0;
    return if ! defined $c1;
    if ( $c1 eq "\e" ) {
        my $c2 = ReadKey 0.10;
        if ( ! defined $c2 ) { return NEXT_getch; }
        elsif ( $c2 eq 'C' ) { return KEY_RIGHT; }
        elsif ( $c2 eq 'D' ) { return KEY_LEFT; }
        elsif ( $c2 eq '[' ) {
            my $c3 = ReadKey 0;
            if ( $c3 eq 'C' ) { return KEY_RIGHT; }
            elsif ( $c3 eq 'D' ) { return KEY_LEFT; }
            else {
                return NEXT_getch;
            }
        }
        else {
            return NEXT_getch;
        }
    }
    else {
        return ord $c1;
    }
}
				
                        
To ensure that the terminal is reset when your program exits, put the reset code into an
ENDblock. For example, you could replace your_end_winsub with:(I removed the lines resetting
$|and theselected output filehandle since the process is exiting anyhow, so they're about to become irrelevant.)An
ENDblock will always run when the program terminates in a "normal" way, such as callingexitordieor hitting the end of the executable code. It does not fire when the process terminates due to receiving a signal; it looks like you're handling the ctrl-C character directly, but you may want to consider adding a%SIG{INT}handler as well, in case someone sends you akill -2.