Chas Owens wrote:
On 3/13/07, Shawn Milo <[EMAIL PROTECTED]> wrote:
How can I get Perl to spit out control characters, such as ^J (the
linefeed) as the actual control character? Every time I try to print
it, Perl converts it to the ASCII \n character.

I need to process a  file, converting a \n to \cJ, AKA the ^J
character. I've tried various things with sprintf, hex(), and other
random things, but no luck.

Has anyone else had to deal with this before?

If I pipe the output of the file to "od -c" on the command line, the
other control characters (which are already correct in the file) seem
to come through as their octal value. However, most attempts to
convert the  \n into a ^J seem to end up with Perl interpreting it to
\n or a null character, [EMAIL PROTECTED] I think that if it's working 
correctly, I
should start seeing the ^J (\n) as the octal character "21."

Thanks for the help.

Shawn

\n is a special character that converts to linefeed on unix systems,
carriage return and linefeed on MS Windows systems, or carriage return
on Macintosh systems brefore Mac OS X (which I believe gets a
linefeed).  In text mode the preceding characters or character groups
get converted into \n on the relevant system.  If you wish to prevent
this behaviour then you need to call binmode with the file handle you
are reading from.  BTW, linefeed is octal 12 not octal 21, I assume
that was a typo.


"\n" is control-J. Do

perl -e "print ord qq(\n)"

and you will get 10, which is control-J.

If you output text to files in non-binary mode on non-Unix systems Perl
may convert "\n" to something different in the actual file, as is required
by the platform itself. By and large this is transparent as it will also
convert the same special sequence into "\n" on input so you can forget
about it altogether.

But the bottom line is "\n" is "\cJ" is "\012" is "\x0A".

Rob


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to