xhsoldier has asked for the wisdom of the Perl Monks concerning the following question:

#the list is consist of the hex numbers
#my @list = (0x0040,0x0041,...,0x000a,0x0065...);
#I translate the hex to the decimal using hex()
#now @list = (64,65,...,10,101,...);
then I using the follow code.
my $outputstring = pack("n*", @list); print TARGET_FILE $outputstring;
but each time it will add a carriage return character(0x000d) in front of the character(0x000a, line feed)
any method for delete this 0x000d?
I use the binmode
binmode TARGET_FILE; my $outputstring = pack("n*", @list); print TARGET_FILE $outputstring;
it works well for big-endian
but when comes to little-endian
binmode TARGET_FILE; my $outputstring = pack("v*", @list); print TARGET_FILE $outputstring;
it is still auto add a carriage return in front of a line feed, any ideas?
  • Comment on auto add a carriage return in front of a line feed, how to delete this auto character.
  • Select or Download Code

Replies are listed 'Best First'.
Re: auto add a carriage return in front of a line feed, how to delete this auto character.
by Util (Priest) on May 21, 2008 at 03:12 UTC
      All methods are works. that is great. thanks a lot, all.
Re: auto add a carriage return in front of a line feed, how to delete this auto character.
by pc88mxer (Vicar) on May 21, 2008 at 03:22 UTC
    I gather you are running ActiveState perl on a Windows box. A PerlIO layer is responsible for converting the newline into a cr-lf sequence. As others have suggested, you can read up on I/O layers via perldoc perlio.

    To solve your problem, try one of these:

    binmode STDOUT, ":unix"; binmode STDOUT, ":raw";
    To see what I/O layers are active for STDOUT, use:
    print "@{[PerlIO::get_layers(STDOUT)]}\n";
    My ActiveState Win32 perl reports the layers unix crlf for STDOUT. On the same box, however, Cygwin perl reports the single layer stdio.
Re: auto add a carriage return in front of a line feed, how to delete this auto character.
by Anonymous Monk on May 21, 2008 at 03:10 UTC
    perldoc -f binmode
      I use the binmode
      binmode TARGET_FILE; my $outputstring = pack("n*", @list); print TARGET_FILE $outputstring;
      it works well for big-endian
      but when comes to little-endian
      binmode TARGET_FILE; my $outputstring = pack("v*", @list); print TARGET_FILE $outputstring;
      it is still auto add a carriage return in front of a line feed, any ideas?

        but when comes to little-endian [...] it is still auto add a carriage return in front of a line feed

        No it doesn't. Either it adds CRs before LFs or it doesn't. It doesn't know or care how the string was constructed. Something else changed.

        open TARGET_FILE, '>', 'foo' or die; binmode TARGET_FILE; my @list = 10; my $outputstring = pack("v*", @list); print TARGET_FILE $outputstring;
        >perl script.pl >debug script.out -rcx CX 0002 : -d100 l2 0B02:0100 0A 00 .. -q

        PS — Why are you using a global variable (as in TARGET_FILE) for your file handle instead of a lexical? The former hasn't been required for 8 years.