in reply to Re^2: hex code passed from command line is interpreted literally in substitution
in thread hex code passed from command line is interpreted literally in substitution

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
  • Comment on Re^3: hex code passed from command line is interpreted literally in substitution
  • Download Code

Replies are listed 'Best First'.
Re^4: hex code passed from command line is interpreted literally in substitution
by Allasso (Monk) on Mar 06, 2011 at 02:12 UTC
    \\x41 is not different from \x41 ?

      Correct. Perl string literal «"\\x41"» is the same as bash string literal «'\x41'». Both produce the string "\","x","4","1". Print $r if you don't believe me.

        I guess I am having difficulty with the interpolation thing. I'll keep working on it though.

        ps, I replied to bart below, that is also a reply to you. thanks.

        reply
        Okay, I guess I can see what you are saying when you say they are "not different", if I word it more like, "both expressions evaluate to the same thing when one is placed inside quotes in perl code, and the other is passed as an arg from the command line." Each string, "\", "x", "4", "1", and "\", "\", "x", "4", "1", are obviously treated in different ways in order to produce the same result. That is the difference I was asking about. I guess the answer is, one is interpolated, and the other isn't.

        I am wondering this: why can't I then just do:
        $r = "$r";
        Why doesn't putting the string, which is now, "\", "x", "4", "1", inside the quotes not cause perl to interpolate the string to the character with the value of 41 hex, just as if I wrote, "\x41"?


        Also, why is the behaviour different when a variable containing the string "\", "x", "4", "1" is placed inside the RHS (substitutes the literal string, \x41), than if the string is hardcoded in (substitutes the character 'A'? Obviously, some interpolation is going on, otherwise you would get the string, "$", "r". So why doesn't it go ahead and interpolate it to the char 'A'?

        Then there is my original question, about why it does it in the LHS. But maybe if I understand the above question, I will better understand the answer to this one. Obviously their nature is different, ie, one evaluates regex, and the other doesn't. Perhaps I am having some confusion about whether, \xNN is a regex or not. It is interpolated as a character in the RHS, (when hardcoded in), but other regexes are interpreted literally.
      No. \x41 is not different from \x41, or if you're interpolating, \\x41 is not different from \\x41