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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|