Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: How does the built-in function length work?

by PerlOnTheWay (Monk)
on Dec 02, 2011 at 14:12 UTC ( [id://941327]=note: print w/replies, xml ) Need Help??


in reply to Re: How does the built-in function length work?
in thread How does the built-in function length work?

But I don't have to decode the string before using it

  • Comment on Re^2: How does the built-in function length work?

Replies are listed 'Best First'.
Re^3: How does the built-in function length work?
by Corion (Patriarch) on Dec 02, 2011 at 14:21 UTC

    Maybe in your string then, the number of octets and number of characters is the same?

    The following shows that Perl does not guess the encoding of strings but assumes it:

    # Perl assumes it's a Latin-1 string > perl -MEncode -wle print+length(qq(\x{c3}\x{a4})) 2
    # Perl gets told to decode the string from UTF-8 > perl -MEncode -wle print+length(decode('UTF-8',qq(\x{c3}\x{a4}))) 1
    # My terminal is Latin-1, which happens to match Perls default assumpt +ion > perl -MEncode -wle print(length(decode('Latin-1',qq(ä)))) 1

    Update: choroba pointed out that I mispasted the second example - now corrected.

Re^3: How does the built-in function length work?
by Eliya (Vicar) on Dec 02, 2011 at 14:21 UTC

    It depends... sometimes you do have to decode them, sometimes you don't, because Perl (or some module etc.) has already done it for you.

    In any case, for Perl to be able to work with character strings (as opposed to byte/octet strings), the string must have been decoded somehow into Perl's internal Unicode representation.

Re^3: How does the built-in function length work?
by Anonymous Monk on Dec 02, 2011 at 14:19 UTC
    It also works on encoded strings, that is to say then it counts octets.

    Be aware of what you are feeding to the length function, you must keep track of the state of encoding yourself because Perl won't.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://941327]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (12)
As of 2024-04-23 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found