Hello Monks, I need some guidance.
I am trying to write a little subroutine that takes before and after values and creates a substitution expression, so I can replace non-displayable characters in a file with displayable ones for example. However I cannot make this work with hex values; what I'm attempting looks like this (which replaces hex 'e9' with char '\x65'):
my $pre = '\xe9';
my $post = '\x65';
my $re = qr/s\/$pre\/$post\/g/;
my $path = 'C:\Scripts\Working2';
my $fileSep = "\\";
my $file = 'Users_0.xml';
my $tempFile = 'C:\Scripts\Working2\Users2.xml';
if (!open(IF, "<$path$fileSep$file")) {
die("Could not open file $path$fileSep$file $!");
}
if (!open(OF, ">$tempFile")) {
die("Could not open file $tempFile $!");
}
while($str = <IF>) {
$str =~ $re;
print OF $str;
}
close IF;
close OF;
I know the substitution '$str =~ s/\xe9/\x65/g;' works just fine but parameterising it is the problem. Straightforward for char strings but seemingly less so for hex ones ... Can someone please advise?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.