perl -w -E "say hex(q{x})" 0 #### perl -w -E "say hex(q{t})" Illegal hexadecimal digit 't' ignored at -e line 1. 0 #### sub hex2 { my $str = shift; $str =~ s/^(0x|x)//; if (length $str) { if ($str =~ /([^0-9a-f])/i) { die "hex2: Illegal hexadecimal digit found: '$1'"; } else { return hex $str; } } else { die "hex2: No chars found after stripping leading 0x or x"; } }