Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Escaping a variable

by Draxter (Initiate)
on Nov 08, 2011 at 09:09 UTC ( [id://936686]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://936686]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 06:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found