Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^4: Alternative to bytes::length()

by ikegami (Patriarch)
on Dec 23, 2009 at 02:15 UTC ( [id://814042]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Alternative to bytes::length()
in thread Alternative to bytes::length()

Actually, simply calling length on a scalar with UTF8=1 adds the magic.
>perl -MDevel::Peek -e"Dump $_=chr(0x2660)x100; length $_; Dump $_" SV = PV(0x2379ec) at 0x1845eec REFCNT = 1 FLAGS = (POK,pPOK,UTF8) PV = 0x184c5ec "..."\0 [UTF8 "..."] CUR = 300 LEN = 304 SV = PVMG(0x18242cc) at 0x1845eec REFCNT = 1 FLAGS = (SMG,POK,pPOK,UTF8) IV = 0 NV = 0 PV = 0x184c5ec "..."\0 [UTF8 "..."] CUR = 300 LEN = 304 MAGIC = 0x1824e64 MG_VIRTUAL = &PL_vtbl_utf8 MG_TYPE = PERL_MAGIC_utf8(w) MG_LEN = 100 <---------- char length

It's a pity that actions such as chop, appending a UTF8=0 string, etc void the count instead of updating it.

Note that $_ eq '' doesn't add the magic, so not only is it faster, it uses less memory.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-25 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found