in reply to Re: what is difference between chr and oct?
in thread what is difference between chr and oct?

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

  • Comment on Re^2: what is difference between chr and oct?

Replies are listed 'Best First'.
Re^3: what is difference between chr and oct?
by afoken (Chancellor) on Jul 13, 2010 at 02:24 UTC

    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.