Hex strings treated differently when read from STDIN?

Discussion in 'Perl Misc' started by fhscobey, Nov 1, 2005.

  1. fhscobey

    fhscobey Guest

    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
    fhscobey, Nov 1, 2005
    #1
    1. Advertising

  2. "fhscobey" <> wrote in
    news::

    > 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}

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Nov 1, 2005
    #2
    1. Advertising

  3. fhscobey <> wrote:

    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 1, 2005
    #3
  4. fhscobey <> wrote:

    > 'while(<STDIN>){print("$_");}'

    ^ ^
    ^ ^

    perldoc -q vars


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 1, 2005
    #4
  5. fhscobey

    Dave Guest

    "fhscobey" <> wrote in message
    news:...
    > 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.
    Dave, Nov 1, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Heavey
    Replies:
    1
    Views:
    361
    Oisín Grehan
    Dec 2, 2003
  2. Dmitriy Zakharov
    Replies:
    2
    Views:
    423
    Dmitriy Zakharov
    Jul 30, 2004
  3. Replies:
    10
    Views:
    6,133
    Neredbojias
    Aug 19, 2005
  4. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    763
    Dan Pop
    Jun 21, 2004
  5. Bengt Richter
    Replies:
    6
    Views:
    448
    Juha Autero
    Aug 19, 2003
Loading...

Share This Page