demoralizer has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

currently I have a problem with reading a string from command line and using it in a regular expression.

What I exactly want to do is a bit hard to explain what could be the problem that I cannot find anything with the search function here.

I want to read something like "foo\x32\x33\x34bar" via Term::ReadLine and convert it so as if I had entered directy in the code like my $foo = "foo\x32\x33\x34bar";

To make it a bit cleared this is what I currently get:
66 6F 6F 5C 78 33 32 5C 78 33 33 5C 78 33 34 62 61 72  |  foo\x32\x33\x34bar

But what I want is:
66 6F 6F 32 33 34 62 61 72  |  foo234bar

How can I pack/unpack/convert the string to handle this?

Any ideas will be welcomed!

Thanks in advance :)

Replies are listed 'Best First'.
Re: "pack" string read from command line
by choroba (Cardinal) on Nov 19, 2015 at 08:23 UTC
    If you just want \x escapes, you can use a regex:
    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my $string = 'foo\x32\x33\x34bar'; $string =~ s/\\x([0-9a-f][0-9a-f]?)/chr hex $1/gie; say $string;

    I wouldn't recommend eval.

    Update: For full functionality, see String::Interpolate.

    use String::Interpolate qw{ interpolate }; say interpolate($string);
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Thanks a lot for answering that fast!

      Doing it manually is a little bit to simple because the string could also contain stuff like "\n".

      String::Interpolate is exactly what I need.
      I wish that I had known this several years ago! I still may be able to use your advice to demoralizer to improve an existing program.
      Bill