in reply to what is difference between chr and oct?

My question is why perl do not support chr('0b'.$1)
Because chr takes a number as an argument. You pass it a string - a string that doesn't look like a number. The "0b" only works for numeric literals, or arguments to oct and hex. '0b' . $1 is not a literal.
Is there a way to convert a string to number explicitly like perl6?
eval, as in
$string =~ s/ (\d+)/chr eval "0b$1"/egx;

Replies are listed 'Best First'.
Re^2: what is difference between chr and oct?
by xiaoyafeng (Deacon) on Jul 12, 2010 at 17:36 UTC
    Thanks! eval is a great trick!




    I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

      No, it's plain stupid. oct("0b".$1) is what you want. eval with a string argument starts the huge perl parser, whereas oct uses much more efficient code that can only convert strings to numbers.

      Using a string eval is not needed most of the times, and very often, it opens a security hole. JavaFans code does not open a security hole, because it allows only digits to be passed to eval. But it is still wrong, it should allow only 0 and 1, not all digits. And it does not remove the whitespace.

      $string=~s/\s*([01]+)/chr oct "0b$1"/eg;

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Thanks, That means it's sad to say perl5 has no a explicit way to convert number to string each other. sigh!




        I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

        But it is still wrong, it should allow only 0 and 1, not all digits.
        Not if the question is the more general Is there a way to convert a string to number explicitly like perl6?. Which was what I was answering.