를?
펄 트래핑 오류 eval
사용하여 다음 [블록] 형태에서
use strict;
use warnings;
use 5.016;
use Text::Iconv;
my $source_encoding = 'blabla';
my $result_encoding = 'utf-8';
my $converter = eval {
Text::Iconv->new(
$source_encoding,
$result_encoding
);
}; #Error message gets inserted into [email protected]
if (not $converter and [email protected] =~ /invalid argument/i) {
say "Either the '$source_encoding' encoding or the ",
"'$result_encoding' encoding\nis not available on this system.";
}
if ($converter) { #Can new() fail in other ways?
my $result = $converter->convert('€');
if (not $result) {
say "Some characters in '$source_encoding'\n",
"are invalid in '$result_encoding'.";
}
else {
say $result;
}
}
은, 블록 내의 코드 번만 파싱 - 동시에 자체는 파싱 된 평가를 둘러싼 코드 현재의 Perl 프로그램의 컨텍스트 내에서 실행됩니다. 이 형식은 일반적으로 첫 번째 (아래 참조)보다 효율적으로 예외를 트랩하는 데 사용되는 반면 컴파일 타임에 BLOCK 내의 코드를 확인하는 이점을 제공합니다.
http://perldoc.perl.org/functions/eval.html
또는 방법
변환하기 전에 코드 세트를 지원 확인하려면?
에 대한 iconv --list
을 그렇게 나쁘지 무엇입니까 (나는) "의 iconv --list"에 의해 OS에서 을 읽어 알고 있지만, 더 나은 솔루션 (희망이 존재해야합니다)?
use strict;
use warnings;
use 5.016;
use Text::Iconv;
my $source_encoding = 'blabla';
my $result_encoding = 'utf-8';
my $available_encodings = `iconv --list`; #Backticks return a string.
my @encodings_arr = split /\s+/, $available_encodings;
my %encodings_set = map {lc $_ => undef} @encodings_arr;
my $source_encoding_available = exists $encodings_set{$source_encoding};
my $result_encoding_available = exists $encodings_set{$result_encoding};
if($source_encoding_available
and $result_encoding_available) {
say "Ready to convert";
}
else {
if (not $source_encoding_available) {
say "'$source_encoding' encoding not available.";
}
if (not $result_encoding_available) {
say "'$result_encoding' encoding not available.";
}
}