Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by ikegami (Pope)
on Dec 23, 2009 at 02:02 UTC ( #814041=note: print w/ replies, xml ) Need Help??


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

Do you have an example of this magic?

That would be an argument for creating a new function, not for keeping bytes.


Comment on Re^3: Alternative to bytes::length()
Replies are listed 'Best First'.
Re^4: Alternative to bytes::length()
by ikegami (Pope) on Dec 23, 2009 at 02:15 UTC
    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.

Re^4: Alternative to bytes::length()
by creamygoodness (Curate) on Dec 23, 2009 at 02:25 UTC
    Looks like you found the caching mechanism. From perlguts:
    w PERL_MAGIC_utf8 vtbl_utf8 UTF-8 length+offset cac +he

    As for keeping bytes... meh, my attachment to the bytes pragma extended only to that use case, as the efficiency of CORE::length() with SVf_UTF8 scalars is a bummer. I'm not even going to bother posting to p5p now that my concern has been addressed another way.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (14)
As of 2015-07-07 20:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls