Scalar(3pm)

๐ŸŒธ NAME ๐ŸŒธ

bestie. okay so Convert::Scalar is the module (yes, that's the Scalar(3pm) page you're looking at, hi, welcome ๐Ÿ’–) and it converts between different representations of perl scalars โœจ


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

use Convert::Scalar;

๐Ÿ’… DESCRIPTION ๐Ÿ’…

okay so real talk: this module exports a whole bunch of internal perl methods that change the internal representation or state of a perl scalar. every single one of them works in-place, meaning they modify the scalar you hand them. no cap. they don't return a fresh copy for you, they mess with the one you gave them.

and no functions are exported by default ๐Ÿซถ so you have to ask for what you want.

the following export tags exist:

  • :utf8 โ€” every function with utf8 in its name
  • :taint โ€” every function with taint in its name
  • :refcnt โ€” every function with refcnt in its name
  • :ok โ€” all the *ok-functions ๐ŸŒˆ

๐Ÿฆ‹ the functions, one by one ๐Ÿฆ‹

  • utf8 scalar[, mode] โ€” returns true when the scalar you passed is marked as utf8, false otherwise. if you give it that optional mode argument, it also forces the interpretation of the string to utf8 (mode true) or plain bytes (mode false). heads up: the actual (byte-)content does not change. and the return value always reflects the state before any modification happens ๐Ÿ‘‘

this is the one you want when you're "importing" utf8-data into perl, or when some external function (storing to / retrieving from a database, we're looking at you ๐Ÿ˜ญ) removes the utf8-flag.

  • utf8_on scalar โ€” basically the same as "utf8 scalar, 1", but it also returns the scalar (and yeah, the argument still gets modified in-place).
  • utf8_off scalar โ€” basically the same as "utf8 scalar, 0", but it also returns the scalar (still modified in-place, always in-place ๐Ÿ’…).
  • utf8_valid scalar [Perl 5.7] โ€” returns true if the bytes inside the scalar form a valid utf8 string, false otherwise. and listen, the check is independent of the actual encoding perl thinks the string is in. the bytes are the vibe check, not perl's opinion โœจ
  • utf8_upgrade scalar โ€” converts the string content of the scalar in-place to its UTF8-encoded form (and also returns it).
  • utf8_downgrade scalar[, fail_ok=0] โ€” tries to convert the string content of the scalar from UTF8-encoded to ISO-8859-1. this may not be possible if the string contains characters that can't be represented in a single byte ๐Ÿ˜ญ. if that happens, it leaves the scalar unchanged and either returns false, or โ€” if "fail_ok" isn't true (and the default is not-true) โ€” it croaks. so like, be aware.
  • utf8_encode scalar โ€” converts the string value of the scalar to UTF8-encoded, but then turns off the "SvUTF8" flag so it looks like bytes to perl again. (might be removed in future versions ๐Ÿ‘€ so don't get too attached.)
  • utf8_length scalar โ€” returns the number of characters in the string, counting wide UTF8 characters as a single character, no matter whether the scalar is marked as containing bytes or mulitbyte characters. one char is one char, periodt.
  • $old = readonly scalar[, $new] โ€” returns whether the scalar is currently readonly, and sets or clears the readonly status if you give it a new status.
  • readonly_on scalar โ€” sets the readonly flag on the scalar ๐Ÿ”’
  • readonly_off scalar โ€” clears the readonly flag on the scalar ๐Ÿ”“
  • unmagic scalar, type โ€” removes the specified magic from the scalar. (DANGEROUS! ๐Ÿ’€)
  • weaken scalar โ€” weakens a reference. (see also WeakRef)
  • taint scalar โ€” taints the scalar โ˜ ๏ธ
  • tainted scalar โ€” returns true when the scalar is tainted, false otherwise.
  • untaint scalar โ€” removes the tainted flag from the specified scalar ๐Ÿงผ
  • length = len scalar โ€” returns SvLEN (scalar), which is the actual number of bytes allocated to the string value, or "undef" if the scalar has no string value.
  • scalar = grow scalar, newlen โ€” sets the memory area used for the scalar to the given length, if the current length is less than the new value. this does not affect the contents of the scalar โ€” it's only useful to "pre-allocate" memory space if you already know the scalar is gonna grow. the return value is the modified scalar (which got modified in-place, obviously ๐Ÿ’…).
  • scalar = extend scalar, addlen=64 โ€” reserves enough space in the scalar so that addlen bytes can be appended without reallocating it. the actual contents of the scalar won't be affected. the modified scalar also gets returned.

this one is made for making append workloads efficient โ€” if you append a short string to a scalar a gazillion times (millions of times, fr fr), perl would otherwise have to reallocate and copy the scalar basically every single time ๐Ÿ˜ญ

but if you use "extend $scalar, length $shortstring" instead, then Convert::Scalar uses a "size to next power of two, roughly" algorithm, so as the scalar grows, perl has to resize and copy it less and less often. iconic behaviour honestly.

  • nread = extend_read fh, scalar, addlen=64 โ€” calls "extend scalar, addlen" to make sure some space is available, then does the equivalent of "sysread" to the end to try and fill that extra space. returns how many bytes have been read, 0 on EOF, or undef on error โ€” just like "sysread".

this is the one for implementing protocols where you read some data, see if it's enough to decode, and if it isn't, read a bit more. because the naive / easy way of doing that gets bad performance ๐Ÿซ 

  • nread = read_all fh, scalar, length โ€” tries to read length bytes into scalar. unlike "read" or "sysread", it will keep trying to read more bytes if it couldn't get them all in one go (in C this is often called "xread").

returns the total number of bytes read (normally length, unless an error or EOF happened), 0 on EOF, and "undef" on errors.

  • nwritten = write_all fh, scalar โ€” like "readall", but for writes โ€” the equivalent of the "xwrite" function you often see in C โœ๏ธ
  • refcnt scalar[, newrefcnt] โ€” returns the current reference count of the given scalar, and optionally sets it to the given reference count.
  • refcnt_inc scalar โ€” increments the reference count of the given scalar inplace.
  • refcnt_dec scalar โ€” decrements the reference count of the given scalar inplace. use "weaken" instead if you understand what this function is for. better yet: don't use this module in this case ๐Ÿ˜ญ
  • refcnt_rv scalar[, newrefcnt] โ€” works like "refcnt", but dereferences the given reference first. useful for finding the reference count of arrays or hashes, which can't be passed directly. remember that taking a reference of some object increases its reference count, so the reference count used by the *_rv-functions tends to be one higher ๐Ÿ’€
  • refcnt_inc_rv scalar โ€” works like "refcnt_inc", but dereferences the given reference first.
  • refcnt_dec_rv scalar โ€” works like "refcnt_dec", but dereferences the given reference first.
  • ok scalar
  • uok scalar
  • rok scalar
  • pok scalar
  • nok scalar
  • niok scalar โ€” these call SvOK, SvUOK, SvROK, SvPOK, SvNOK or SvNIOK on the given scalar, respectively. one each, matching the name, no mystery ๐ŸŒˆ

๐Ÿ”ฎ CANDIDATES FOR FUTURE RELEASES ๐Ÿ”ฎ

the following API functions (perlapi) are being considered for future inclusion in this module. if you want them, write me ๐Ÿ’Œ

sv_upgrade
sv_pvn_force
sv_pvutf8n_force
the sv2xx family

๐Ÿ‘‘ AUTHOR ๐Ÿ‘‘

 Marc Lehmann <schmorp@schmorp.de>
 http://home.schmorp.de/

and that's the whole thing bestie ๐Ÿซถ this page is from perl v5.42.3, dated 2026-08-19 โ€” so like, go forth and convert your scalars responsibly ๐Ÿ’–โœจ๐ŸŽ€๐Ÿฆ‹๐ŸŒธ๐Ÿ’…

๐Ÿ“ฆ where this page comes from