1

I am new to perl, I am learning using O'reillys Learning Perl book. Unicode characters(as written in the book) like this can be printed using print "\x{2668}". I was using foreach loop to print a range of Unicode characters.

Example: from 2660 - 2670;

I have written the following code,

foreach $num (2660..2670)
{
        print "Character for $num  is:","\x{$num} \n";

}

Which was giving the following output,

Illegal hexadecimal digit '$' ignored at ./Printing_messages.pl line 10.
Character for 2660  is: 
Character for 2661  is: 
Character for 2662  is: 
Character for 2663  is: 
Character for 2664  is: 
Character for 2665  is: 
Character for 2666  is: 
Character for 2667  is: 
Character for 2668  is: 
Character for 2669  is: 
Character for 2670  is:

After that I've made some other tweaks, but nothing worked. What is the problem with the my program ? I don't understand. Is there how to make it work ?

2 Answers 2

4

You want

print "Character for $num is: ", chr($num), "\n";
Sign up to request clarification or add additional context in comments.

Comments

-3

You can write something like :

foreach $num (2660..2670)
{
        print "Character for $num  is:",eval(qq!\"\\x{$num} \\n\"!);
}

The problem with your original program, is that perl cannot evaluate "\x{...}" because this evaluation is done before the interpolation of $num.

4 Comments

Its Working. Thank you. is eval a function ?
Generating a Perl program, compiling it and running it is severe overkill.
Using eval() for this is seriously bad form. Consider the chr() function instead, as the other answer shows.
This program was for educative usage only. I was answering to the "I am learning..." thing. Of course char is a better solution, but his program was not meant to be production quality... I thought he was trying to use the \x{} syntax...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.