Perl Regex将String转换为Hex未经初始化值的警告或使用/ x
发布时间:2020-12-15 23:20:19 所属栏目:大数据 来源:网络整理
导读:我在perl中使用 regex将字符串转换为十六进制,但是,当我这样做时,我会收到来自perl评论家或perl的警告: #$test is defined,i just put the regex code snippet here...#this will trigger a warning from perl critic#warning: use of regex expression wit
我在perl中使用
regex将字符串转换为十六进制,但是,当我这样做时,我会收到来自perl评论家或perl的警告:
#$test is defined,i just put the regex code snippet here... #this will trigger a warning from perl critic #warning: use of regex expression without "/x" expression.. $text =~ s/(.)/sprintf("%x",ord($1))/eg; #this will trigger a a warning at run time #warning: "uninitialized value $1 in regexp compilation" $text =~ m{s/(.)/sprintf("%x",ord($1))/eg}x; 有没有办法编写上面的代码,没有得到Perl评论家的警告或反馈? 我认为问题是因为ord正在处理未定义的值,当你输入/ x时,检查regex表达式认为$1的值是无效的. 解决方法
这种批评者被称为误报.这里没有/ x的需要或理由.如果你试图让每个评论家沉默,你最终会得到奇怪的代码.也就是说,评论家推荐
s/(.)/sprintf("%x",ord($1))/exg 此外,避免转换换行符可能没有意义.如果是这样,你也想使用/ s. s/(.)/sprintf("%x",ord($1))/sexg (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |