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

Hi,

I'm trying to escape a variable with the \x special character, so it will be printed as a hex character. I only manage to do this when I statically define variable, for example:

my $foo = "\x4D"; print "$bla"; M

But when i try to add the \x to an already existing variable, I always get an "Illegal hexadecimal digit '$' ignored" error message:

my $foo = "4D"; my $bar = "\x4D"; print $bar . "\n"; Illegal hexadecimal digit '$' ignored at roman.pl line 10. 4D.

What am I doing wrong here, and is it solvable?

Thanks,

Eran

Replies are listed 'Best First'.
Re: Escaping a variable
by davido (Cardinal) on Nov 08, 2011 at 09:16 UTC
    my $foo = "4D"; my $bar = "\x4D"; print $bar . "\n";

    Doesn't produce

    Illegal hexadecimal digit '$' ignored at roman.pl line 10. 4D.

    Why don't you please edit your post and include the actual code that is giving you that error message? I suspect I know what it looks like, but it's your job to actually show code that exhibits the problem you're describing.

    At any rate, hex will, given the string contained in $foo as a parameter, return the base ten integer value 77.


    Dave

      Hi,

      Sorry, you are right of course.

      This is what I do:

      my $foo = "4D"; my $bar = "\x$foo"; print $bar . "\n"; Illegal hexadecimal digit '$' ignored at roman.pl line 7. 4D

        print hex($foo), "\n";

        The output:

        77

        Dave