2013-04-15 2 views
2

은 여기 (아직 실제로 아직 키를 매핑하지 않고 현재 만 16 진수로보고 무엇을 인쇄하는) 내 프로그램 keymap있어 :펄 초기화되지 않은 경고

여기
#!/usr/bin/env perl 

use strict; 
use warnings; 

use Term::ReadKey; 
ReadMode 4; 
END { 
    ReadMode 0; # Reset tty mode before exiting 
} 

if ($ARGV[0] ~~ ["h", "-h", "--help", "help"]) { 
    print "Usage: (h|-h|--help|help)|(code_in codes_out [code_in codes_out]+)\nNote: output codes can be arbitrary length"; 
    exit; 
} 

$#ARGV % 2 or die "Even number of args required.\n"; 

$#ARGV >= 0 or warn "No args provided. Output should be identical to input.\n"; 

my $interactive = -t STDIN; 

my %mapping = @ARGV; 

{ 
    local $| = 1; 
    my $key; 
    while (ord(($key = ReadKey(0))) != 0) { 
     printf("saw \\x%02X\n",ord($key)); 
     if ($interactive and ord($key) == 4) { 
      last; 
     } 
    } 
} 

발생 내용은 다음과 같습니다

[email protected]:~/util 20:50:20 
❯ keymap a b 
saw \x61 
saw \x62 
saw \x04 

가 내 키보드 BCtrl 키 + D에 입력했다.

[email protected]:~/util 20:50:24 
❯ echo "^D^Da" | keymap 
No args provided. Output should be identical to input. 
saw \x04 
saw \x04 
saw \x61 
saw \x0A 
Use of uninitialized value $key in ord at /Users/slu/util/keymap line 30. 

나는 이것의 의미가 무엇인지 궁금합니다. 루프 상태를 "설정 중"으로 간주하지 않는 Perl의 경우입니까? $key? 여기에 경고를 표시하지 못하게 할 수있는 일이 있습니까? 나는 no warnings "uninitialized";에 대해 안다, 나는 그것을 원하지 않는다.

답변

3

while 루프의 조건식에 의해 발행 된 경고가 while 조건 바로 전에 평가 된 루프의 명령문에 잘못 할당 될 수있는 알려진 버그가 있습니다.

경고를 발행하는 코드는 실제로 while 루프 인 ord(($key = ReadKey(0))) != 0의 조건입니다.

ReadKey(0)undef을 반환하며 사용자는 ord 또는 그걸 얻으려고합니다.

while (1) { 
    my $key = ReadKey(0); 
    last if !defined($key) || ord($key) == 0; 

    printf("saw \\x%02X\n",ord($key)); 

    last if $interactive and ord($key) == 4; 
} 
+0

줄을 변경하여 'while (ord (($ key = ReadKey (0)))) { –

+0

아니요,'$ key'가 정의되어 있는지 먼저 확인한 후에 'ord'를 호출해야합니다. –

+0

아니요, 여전히 'ord (undef)'입니다. 답변을 추가 한 코드를 참조하십시오. – ikegami

관련 문제