TEST::BUILDER::IO::SCALAR(3perl)

πŸ’– Test::Builder::IO::Scalar πŸ’–

okay bestie, welcome to the manual page for Test::Builder::IO::Scalar, part of the Perl Programmers Reference Guide (that's the 3perl manual section, iykyk). sit down, grab a strawberry πŸ“, and let's get into it.


✨ NAME ✨

Test::Builder::IO::Scalar β€” a copy of IO::Scalar for Test::Builder. literally just that. no cap.


🌸 DESCRIPTION 🌸

so this is a copy of IO::Scalar that ships inside Test::Builder, and the whole reason it exists is so you can use scalar references as filehandles on Perl 5.6. that's it, that's the job. newer versions of Perl just use open()'s built-in support for this and don't need it at all.

and why a copy instead of just depending on the module like a normal person? because Test::Builder can not have dependencies on other modules without careful consideration, so instead of going through all that, it's simply been copied into the distribution. iconic problem-solving honestly πŸ’…


this file came from the "IO-stringy" Perl5 toolkit.

Copyright (c) 1996 by Eryq. All rights reserved. Copyright (c) 1999,2001 by ZeeGee Software Inc. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


πŸ› οΈ Construction πŸ› οΈ

  • new [ARGS...] β€” Class method. hands you back a brand new, unattached scalar handle. if you pass any arguments, they get sent straight to open().
  • open [SCALARREF] β€” Instance method. opens the scalar handle onto a new scalar, pointed to by SCALARREF. if you don't give it a SCALARREF, a "private" scalar gets created to hold the file data. returns the self object on success, and undefined on error.
  • opened β€” Instance method. is the scalar handle opened on something? yes/no, bestie.
  • close β€” Instance method. disassociates the scalar handle from its underlying scalar. this also happens automatically on destroy, so no stress.

πŸ“₯ Input and output πŸ“€

  • flush β€” Instance method. total no-op, it's just there for OO compatibility. it's giving decorative.
  • getc β€” Instance method. returns the next character, or undef if there aren't any left.
  • getline β€” Instance method. returns the next line, or undef at end of string. totally safe to call in an array context. currently, lines are delimited by "\n".
  • getlines β€” Instance method. grabs all remaining lines. it will croak() if you accidentally call it in a scalar context β€” so don't do that. 😭
  • print ARGS... β€” Instance method. prints ARGS to the underlying scalar.
Warning: this continues to always cause a seek to the end of the string, but if you perform seek()s and tell()s, it is still safer to explicitly seek-to-end before subsequent print()s. so like. be careful. πŸ™ˆ
  • read BUF, NBYTES, [OFFSET] β€” Instance method. reads some bytes from the scalar. returns the number of bytes actually read, 0 on end-of-file, undef on error.
  • write BUF, NBYTES, [OFFSET] β€” Instance method. writes some bytes to the scalar.
  • sysread BUF, LEN, [OFFSET] β€” Instance method. reads some bytes from the scalar. returns the number of bytes actually read, 0 on end-of-file, undef on error.
  • syswrite BUF, NBYTES, [OFFSET] β€” Instance method. writes some bytes to the scalar.

🧭 Seeking/telling and other attributes 🧭

  • autoflush β€” Instance method. no-op, provided for OO compatibility.
  • binmode β€” Instance method. no-op, provided for OO compatibility.
  • clearerr β€” Instance method. clears the error and EOF flags. a no-op.
  • eof β€” Instance method. are we at end of file? πŸ₯Ί
  • seek OFFSET, WHENCE β€” Instance method. seeks to a given position in the stream.
  • sysseek OFFSET, WHENCE β€” Instance method. identical to seek OFFSET, WHENCE, q.v.
  • tell β€” Instance method. returns the current position in the stream, as a numeric offset.
  • use_RS [YESNO] β€” Instance method. deprecated and ignored. the original question was: obey the current setting of $/, like IO::Handle does? default is false in 1.x, but cold-welded true in 2.x and later.
  • setpos POS β€” Instance method. sets the current position, using the opaque value returned by getpos().
  • getpos β€” Instance method. returns the current position in the string, as an opaque object.
  • sref β€” Instance method. returns a reference to the underlying scalar.

πŸ’€ WARNINGS πŸ’€

okay this part matters, so pay attention bestie.

Perl's TIEHANDLE spec was incomplete prior to 5.005_57; it was missing support for seek(), tell(), and eof(). attempting to use these functions with an IO::Scalar will not work prior to 5.005_57. IO::Scalar will not have the relevant methods invoked; and even worse, this kind of bug can lie dormant for a while. 😳

so if you turn warnings on (via $^W or "perl -w"), and you see something like this...

attempt to seek on unopened filehandle

...then you are probably trying to use one of these functions on an IO::Scalar with an old Perl. the remedy is to simply use the OO version; e.g.:

$SH->seek(0,0);    ### GOOD: will work on any 5.005
seek($SH,0,0);     ### WARNING: will only work on 5.005_57 and beyond

⭐ VERSION ⭐

$Id: Scalar.pm,v 1.6 2005/02/10 21:21:53 dfs Exp $

πŸ‘‘ AUTHORS πŸ‘‘

Primary Maintainer

David F. Skoll (dfs@roaringpenguin.com).

Principal author

Eryq (eryq@zeegee.com). President, ZeeGee Software Inc (http://www.zeegee.com).

Other contributors

The full set of contributors always includes the folks mentioned in "CHANGE LOG" in IO::Stringy. but just the same, special thanks to the following individuals for their invaluable contributions (if I've forgotten or misspelled your name, please email me!):

  • Andy Glew, for contributing getc().
  • Brandon Browning, for suggesting opened().
  • David Richter, for finding and fixing the bug in PRINTF().
  • Eric L. Brine, for his offset-using read() and write() implementations.
  • Richard Jones, for his patches to massively improve the performance of getline() and add "sysread" and "syswrite".
  • B. K. Oxley (binkley), for stringification and inheritance improvements, and sundry good ideas.
  • Doug Wilson, for the IO::Handle inheritance and automatic tie-ing.

🌈 SEE ALSO 🌈

IO::String, which is quite similar but which was designed more-recently and with an IO::Handle-like interface in mind, so you could mix OO- and native-filehandle usage without using tied().

Note: as of version 2.x, these classes all work like their IO::Handle counterparts, so we have comparable functionality to IO::String.

and that's the whole page, bestie!! 🫢 this was perl v5.40.1, dated 2026-06-12, straight out of the Test::...:Scalar(3perl) manual. go forth and scalar-handle responsibly. periodt. πŸ’…βœ¨

πŸ“¦ where this page comes from