SPUG: lenght of string

Yitzchak Scott-Thoennes sthoenna at efn.org
Tue Oct 5 17:12:58 CDT 2004


On Tue, Oct 05, 2004 at 02:31:01PM -0700, Ken Clarke <kenslinux at shaw.ca> wrote:
> While we're on the subject of the length function...
> 
> I've noticed that ActiveState Perl v 5.8.3 seems to ignore the use bytes 
> pragma mentioned in the docs.
> 
> This snippet reports the length as 3 bytes when in fact it's 4: 
> "\x31\x0D\x0A\x33"
> 
> my $string = "1\n2";
> do {
>   use bytes;
>   print length($string);
> };

I think you are under a misapprehension.

It really is just 3 bytes, but if you output it to a filehandle that
does crlf conversion, you will get 4 bytes of output.  Everything is
designed so that a program only has to worry about a single line-end
character; CR's get stripped off as you read things in and added as
you write things out.

If you are needing to count actual bytes of output, you may want to
use a binmode filehandle and just manually use "\r\n" yourself (or
"\015\012" if you have to worry about old Macs.)


More information about the spug-list mailing list