Hex strings treated differently when read from STDIN?

F

fhscobey

Hi,
Just curious if anyone knows why the following happens:

Example 1:
$ perl -e 'print("\x{4A}\x{65}\x{66}\x{66}","\n");'
Jeff

Example 2:
$ echo '\x{4A}\x{65}\x{66}\x{66}' | perl -e
'while(<STDIN>){print("$_");}'
\x{4A}\x{65}\x{66}\x{66}

Example 3:
$ echo '\x{4A}\x{65}\x{66}\x{66}' | perl -e 'while(<STDIN>){$line="$_";
chomp($line); @chars=split(//,"$line"); foreach $ch
(@chars){print($ch,"|");}}'
\|x|{|4|A|}|\|x|{|6|5|}|\|x|{|6|6|}|\|x|{|6|6|}|

Not sure why Perl doesn't recognize the string in Ex. 2 as a hex
string. Anyone know why? Also, how would one iterate over the
individual chars of the hex string in Ex. 3? I'm assuming the answer
to #2 has something to do with #3.

I'm using RedHat Linux 7.2, perl 5.8.1.

Thanks for any assistance you can offer!
- Jeff
 
A

A. Sinan Unur

Hi,
Just curious if anyone knows why the following happens:

Example 1:
$ perl -e 'print("\x{4A}\x{65}\x{66}\x{66}","\n");'
Jeff

Example 2:
$ echo '\x{4A}\x{65}\x{66}\x{66}' | perl -e
'while(<STDIN>){print("$_");}'
\x{4A}\x{65}\x{66}\x{66}

Example 3:
$ echo '\x{4A}\x{65}\x{66}\x{66}' | perl -e
'while(<STDIN>){$line="$_"; chomp($line); @chars=split(//,"$line");
foreach $ch (@chars){print($ch,"|");}}'
\|x|{|4|A|}|\|x|{|6|5|}|\|x|{|6|6|}|\|x|{|6|6|}|

Not sure why Perl doesn't recognize the string in Ex. 2 as a hex
string. Anyone know why?

From perldoc perlop:

The following escape sequences are available in constructs that
interpolate and in transliterations.

\t tab (HT, TAB)
\n newline (NL)
\r return (CR)
\f form feed (FF)
\b backspace (BS)
\a alarm (bell) (BEL)
\e escape (ESC)
\033 octal char (ESC)
\x1b hex char (ESC)
\x{263a} wide hex char (SMILEY)
\c[ control char (ESC)
\N{name} named Unicode character

A double-quoted string in program source interpolates. Input does not.

You can interpret the escape sequences yourself:

#!/usr/bin/perl

use strict;
use warnings;

while (my $line = <STDIN>) {
my $str;
while ($line =~ m/ \\x{([[:xdigit:]]+)} /xg) {
$str .= chr(hex $1);
}
print "$str\n";
}
__END__

D:\Home\asu1\UseNet\clpmisc> bug
\x{73}\x{69}\x{6e}\x{61}\x{6e}
sinan

Or:

#!/usr/bin/perl

use strict;
use warnings;

while (my $line = <DATA>) {
$line =~ s/ \\x{([[:xdigit:]]+)} / chr(hex $1) /xge;
print $line;
}

__DATA__
\x{53}\x{69}\x{6e}\x{61}\x{6e}
 
T

Tad McClellan

fhscobey said:
Example 2:
$ echo '\x{4A}\x{65}\x{66}\x{66}' | perl -e

Not sure why Perl doesn't recognize the string in Ex. 2 as a hex
string.


Probably because it isn't one.

Anyone know why?


Have a look at the data that you are passing to perl:

echo '\x{4A}\x{65}\x{66}\x{66}' | cat

and

echo '\x{4A}\x{65}\x{66}\x{66}' | wc -c

You are passing a 25-character long string to perl when I think
you were trying to pass it a 4-character long string.
 
D

Dave

fhscobey said:
Hi,
Just curious if anyone knows why the following happens:

Example 1:
$ perl -e 'print("\x{4A}\x{65}\x{66}\x{66}","\n");'
Jeff

Example 2:
$ echo '\x{4A}\x{65}\x{66}\x{66}' | perl -e
'while(<STDIN>){print("$_");}'
\x{4A}\x{65}\x{66}\x{66}

Example 3:
$ echo '\x{4A}\x{65}\x{66}\x{66}' | perl -e 'while(<STDIN>){$line="$_";
chomp($line); @chars=split(//,"$line"); foreach $ch
(@chars){print($ch,"|");}}'
\|x|{|4|A|}|\|x|{|6|5|}|\|x|{|6|6|}|\|x|{|6|6|}|

Not sure why Perl doesn't recognize the string in Ex. 2 as a hex
string. Anyone know why? Also, how would one iterate over the
individual chars of the hex string in Ex. 3? I'm assuming the answer
to #2 has something to do with #3.

I'm using RedHat Linux 7.2, perl 5.8.1.

Thanks for any assistance you can offer!
- Jeff

The short answer is that perl does not interpolate recursively. In the
second example $_ is interpolated as the literal text it contains. Perl does
not attempt a 'second round' of interpolation on this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top