Perl Regex To convert String to Hex Warnings of uninitialized value or use / x

I use regex in perl to convert the string to hex, however, when I do this, I get a warning from perl crit or 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 aa warning at run time #warning: "uninitialized value $1 in regexp compilation" $text =~ m{s/(.)/sprintf("%x",ord($1))/eg}x; 

Is there a way to write the above code that does not receive warnings or feedback from a critic of Perl?

I think the problem is that ord processes undefined values, and when you put in / x, checking the regex expression considers that the value $ 1 is not valid.

+4
source share
1 answer

This critic is what is called false positive. There is no need or reason for /x . If you try to silence every critic, you will get a strange code. However, the critic recommended

 s/(.)/sprintf("%x",ord($1))/exg 

In addition, it makes no sense to avoid converting newline strings. If so, you also want to use /s .

 s/(.)/sprintf("%x",ord($1))/sexg 
+8
source

Source: https://habr.com/ru/post/1440166/


All Articles