in reply to hex code passed from command line is interpreted literally in substitution

The LHS of s/// is a regular expression, but the RHS is not. So, you can not expect them to behave the same way.

Don't pass the '\x' on the command line for arg_2, then convert it to the character you want using hex and chr:

$arg_2 = chr hex $arg_2; ./arg.pl '\x61' 41
  • Comment on Re: hex code passed from command line is interpreted literally in substitution
  • Download Code

Replies are listed 'Best First'.
Re^2: hex code passed from command line is interpreted literally in substitution
by Allasso (Monk) on Mar 05, 2011 at 22:32 UTC
    how about:
    arg.pl '\x61' '\x41' $arg_2 =~ s@\\x([0-9a-fA-F]+)@chr hex $1@ge;
    thanks, I was trying to use chr after Corion's suggestion to use pack/unpack, but I couldn't figure out how to make it work with hex (easily).

    EDIT: added a-fA-F classes.
Re^2: hex code passed from command line is interpreted literally in substitution
by Allasso (Monk) on Mar 05, 2011 at 22:37 UTC
    "The LHS of s/// is a regular expression, but the RHS is not. So, you can not expect them to behave the same way."

    Nevertheless, RHS accepts the \xNN format if it is hardcoded in the script. It is only when it is passed from the terminal that it doesn't work. Why not? What makes it different when it is passed from the terminal?
      Why not? What makes it different when it is passed from the terminal?

      Why does interpolation happen only once? Because that is how it works

      my $stuff = "\n newline gets interpolated at compile time"; my $other = "$stuff \n gets interpolated at run time"; @ARGV = qw( $stuff $other NOINTERPOLATION ); print "$stuff\n$other\n@ARGV\n" __END__ newline gets interpolated at compile time newline gets interpolated at compile time gets interpolated at run time $stuff $other NOINTERPOLATION
      See, $stuff and $other and @ARGV all get interpolated in that print call, but the values in @ARGV won't get magically interpolated once again

      What makes it different when it is passed from the terminal?

      It's not different.

      $ perl -E'$_="a"; $r=$ARGV[0]; s/a/$r/; say;' '\x41' \x41 $ perl -E'$_="a"; $r="\\x41"; s/a/$r/; say;' \x41
        \\x41 is not different from \x41 ?