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

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.

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://814041]
[NetWallah]: /stevieb I dont understand - the poster had a very legitimate issue, and expressed it clearly. WHat did you have a problem with?

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (15)
As of 2017-02-23 14:53 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (347 votes). Check out past polls.