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

Is there a way to know how many bytes "\n" is on a system?
I tried something like length "\n" but that returns always 1, even on a Windows system where "\n" is actually 2 bytes.

gkinueliileunikg

Replies are listed 'Best First'.
Re: \n size
by chromatic (Archbishop) on Dec 16, 2001 at 00:04 UTC
    \n is one byte, even on Windows. \r\n is two bytes. I make no claims for anything where Unicode is involved (especially if it's UTF-32). Are you looking for binmode?

    Update: (Sat Dec 15 20:34:22 UTC 2001) Yes, that's basically right. Perl writes the appropriate line ending character. It's not just \n on DOS-derived systems, nor on the MacOS platform.

      So \n is always \n but it changes its behavior when you write it to a file not in binmode on a MSDOS/Windows system?

      gkinueliileunikg

Re: \n size
by jaldhar (Vicar) on Dec 16, 2001 at 00:34 UTC

    If your goal is to learn the length of the end of line character,

    perl -e 'print length $/;'
    should do the trick;

    જલધર

Re: \n size
by clintp (Curate) on Dec 16, 2001 at 03:55 UTC
    length $/ also returns 1 on a Windows system.

    What I think you'll need to do is to actually write "\n" to a file and check to see how many bytes got written. The translation happens beneath perl, so I don't think there's any way for perl to know (or at least, an easy way to interrogate perl for that info).

Re: \n size
by Zaxo (Archbishop) on Dec 16, 2001 at 00:35 UTC

    length $/; may be what you want, if nothing has treated global $/ with disrespect.

    After Compline,
    Zaxo