in reply to cut of first char of a string

There's a whole aspect of this question which nobody has touched on yet. I'm talking about the precise meaning of "first char". Consider the situation when $s is a utf-8 string. The first "character" could be several bytes long.

This means that the regexp and chop methods will work on full "characters", but substr operates on "bytes".

I attended a talk recently at london.pm on this subject, the slides of which are here.

--
I'm Not Just Another Perl Hacker

Replies are listed 'Best First'.
Re^2: cut of first char of a string
by demerphq (Chancellor) on Mar 08, 2005 at 13:42 UTC

    This means that the regexp and chop methods will work on full "characters", but substr operates on "bytes".

    AFAIK unless the use bytes pragma is in effect substr operates on characters, not on bytes. Maybe the behaviour you describe is true pre 5.8.x but it isnt true in 5.8:

    D:\Development>perl5.8.5 -le "my $str='ba'.pack('U',0x0370).'!'; print +f qq'$_ : %d\n', ord(substr($str,$_,1)) for 0..length($str)-1" 0 : 98 1 : 97 2 : 880 3 : 33 D:\Development>perl5.8.5 -Mbytes -le "my $str='ba'.pack('U',0x0370).'! +'; printf qq'$_ : %d\n', ord(substr($str,$_,1)) for 0..length($str)-1 +" 0 : 98 1 : 97 2 : 205 3 : 176 4 : 33
    ---
    demerphq