Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Unicode Conversion

by panky245 (Initiate)
on Jun 04, 2008 at 10:15 UTC ( #690097=perlquestion: print w/replies, xml ) Need Help??
panky245 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am a newbie in Perl. Does anybody seems an error in my
following snippet. I am regularly getting the compilation error in it.
$char_lpar = "\x{00028}";
$char_rpar = "\x{00029}";
my $decval_1 = ord $char_lpar;
my $decval_2 = ord $char_rpar;

$data =~ s/$char_lpar/sprintf("&#x%.4x;", $decval_1)/ge;
$data =~ s/$char_rpar/sprintf("&#x%.4x;", $decval_2)/ge;


Pankaj Chaturvedi

Replies are listed 'Best First'.
Re: Unicode Conversion
by haoess (Curate) on Jun 04, 2008 at 10:30 UTC

    $char_lpar and $char_rpar get interpolated inside your substitution. You can quote them using the \Q\E construct:

    $data =~ s/\Q$char_lpar\E/sprintf("%.4x;", $decval_1)/ge; $data =~ s/\Q$char_rpar\E/sprintf("%.4x;", $decval_2)/ge;

    -- Frank

Re: Unicode Conversion
by graff (Chancellor) on Jun 05, 2008 at 00:10 UTC
    What you got was not a "compilation error" -- the code snippet compiles just fine. You got a "run-time error", which (as the name implies) only happens when you try to run the script.

    If you want to replace any character whatever with its hex-numeric entity reference, you can do it like this:

    my $num_ent_chars = "()"; # add other characters as needed, but ^ - [ + ] are special $data =~ s/([$num_ent_chars])/sprintf("&#x%4x;",ord($1))/ge;
    Since the string contained in $num_ent_chars is being used inside square brackets in the regex (to define a character class), most of the regex-magic meanings for things like ".", "*", "?", "+" etc do not apply. You just need to be careful when the character class has to include square brackets, hyphen and caret -- either escape these with "\", or place them carefully -- for example:
    perl -le '$_="me [ and ] you ^ and - him"; $c="][^-"; s/[$c]/xxx/g; + print'
    will print "me xxx and xxx you xxx and xxx him".
Re: Unicode Conversion
by ikegami (Pope) on Jun 04, 2008 at 21:38 UTC

    I am regularly getting the compilation error in it.

    There's a good reason why error message don't just say "An error happened!". Why don't you tell us what error you are getting.

    That said, haoess already identified a major source of error.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://690097]
Approved by Corion
and a log crumbles through the grate...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2018-03-19 17:35 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (245 votes). Check out past polls.