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

Add /e to the substitution:

$var =~ s@$arg_1@$arg_2@e;

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
  • 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 21:17 UTC

    "Add /e to the substitution:"

    that didn't work for me.

      Then I guess I misunderstand what you are trying to do:

      $find = "\x20"; $rep = "\x30";; $s = "The quick brown fox jumps over the lazy dog";; $s =~ s[$find][$rep]eg;; print $s;; The0quick0brown0fox0jumps0over0the0lazy0dog

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I am trying to pass those expressions from the command line. It works fine the way you have your code written, but try this:
        my ($find, $rep) = @ARGV; $s = "The quick brown fox jumps over the lazy dog";; $s =~ s[$find][$rep]eg;; ./arg.pl '\x20' '\x30' #prints: The\x30quick\x30brown\x30fox\x30jumps\x30over\x30the\x30lazy\x30dog

        The /e option on your s/// makes no difference there for two reasons.

        First, you aren't testing what was asked about. $rep = "\x30"; is identical to $rep = "0";. "0" evaluates to "0" so /e makes no difference.

        Second, s/.../$rep/ (no /e) does string interpolation on the $rep part which results in the value from $rep being used. s/.../$rep/e (with /e) interpolates $rep as Perl code and the value of eval '$rep' is also just the value stored in $rep.

        - tye