Michael Carman


Tie::Tk::Text - Access Tk text widgets as arrays.


  use Tie::Tk::Text;

  my $w = $mw->Text()->pack();
  tie my @text, 'Tie::Tk::Text', $w;

  $w->insert('end', "foo\nbar\nbaz\n");
  print $text[1]; # "bar\n"


This module defines a class for tie()ing Tk text widgets to an array, allowing them to be accessed as if they were an array of lines.

It's not expected that anyone will actually want to populate and manipulate their text widgets this way, though you are of course free to do so. This module was created to make text widgets accessible to functions that expect an array reference as their input. (e.g. Algorithm::Diff::sdiff) You can do that with read-only support (FETCH and FETCHSIZE). All of the methods (PUSH, POP, STORE, etc.) are included for completeness.

Note: This documentation refers to "Tk text" widgets rather than "Tk::Text" ones. That's because it supports anything that uses the same API as a Tk text widget. It works with Perl/Tk and Tkx and should work with Tcl::Tk as well.


Arrays use zero-based indexing. Text widgets use one-based indexing. Ergo, line five is at index four.

Lines end in "\n". Be careful about what you add or you could get odd results. For example, doing $text[3] = 'foo' will replace the contents of line four with 'foo' but will join lines four and five because you didn't include a newline at the end of the string. Similarly, $text[3] = "foo\nbar\n" will replace line four with "foo\n" and insert "bar\n" before line five.


There's no support for tags or a lot of other things that you can do with text widgets. There isn't supposed to be. This is not a bug.


Michael J. Carman <mjcarman@cpan.org>


Copyright (C) 2006,2009 by Michael J. Carman

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