如何在perl中解析°到ASCII度字符

时间:2012-11-28 15:31:45

标签: perl

我尝试过使用:

my $nomIHMBloc = $1;
print decode_entities($nomIHMBloc), "\n";
$nomIHMBloc = decode_entities($nomIHMBloc), "\n";

但没有运气。有什么不对的吗?我收到了错误:

Undefined subroutine &main::decode_entities called at "same perl file"

感谢您的帮助。

PS: 确切的代码在这里:

    while($blocVars =~ m/\[(.*?)\]/g){
                binmode STDOUT, ':utf8';
                my $nomIHMBloc = $1;
                print decode_entities($nomIHMBloc), "\n";
                $nomIHMBloc = decode_entities($nomIHMBloc);
                print "nomIHMBloc::::::::$nomIHMBloc=============$1\n";
                print "insert into ASSOC_VAR_BLOC (ID_BLOC, ID_VAR, DOC_ID_MAQUETTAGE) VALUES ($id_bloc, (SELECT ID_VAR FROM VARIABLE WHERE NOM_IHM='$nomIHMBloc'),'$docId')\n";
    }

1 个答案:

答案 0 :(得分:2)

在这里工作正常:

#!/usr/bin/env perl

use strict;
use warnings;
use open ':locale';
use HTML::Entities;

# example text
'42°' =~ /(.*)/; # 42°

# your code
my $nomIHMBloc = $1;
print decode_entities($nomIHMBloc), "\n";
#$nomIHMBloc = decode_entities($nomIHMBloc), "\n";

您的最后一行包含语法错误。如果要在分配标量时附加换行符,请使用字符串连接运算符.

$nomIHMBloc = decode_entities($nomIHMBloc) . "\n";

它适用于print因为它是一个列表运算符并接受一个参数列表,然后将它们与输出字段分隔符 $,连接起来(参见perlvar ),默认情况下包含空字符串,其作用类似于简单的字符串连接。但是,输出是

42°