Lets call it an intentional bug then. Or at least it is a huge inconsistency. If you change the sub in anna_blacks program to:

sub replace { my ($text,$start_index,$end_index) = @_; my $old_data = $text->get($start_index,$end_index); print "<$old_data>\n"; $old_data =~ s/cdefg/12345/g; $text->delete($start_index,$end_index); $text->insert($start_index,$old_data); my $old_data = $text->get('3.0',$end_index); print "<$old_data>\n"; return; }

... you can see that the insert started one char too early.

It could be argued that you should not insert into lines that don't exist (i.e. after the delete of all lines starting from line 16 the location 16.0 doesn't really exist anymore). But then your 'end-1c' solution would be illegal as well, and it only works because the additional \n is confused with the \n of not-existant line 16.

If TK inserts spurious characters it is its job to make sure that those characters don't get in the way of the programmer.

PS: Why double quotes? They don't do anything different than a single quote here.

UPDATE: changed 2 sentences

In reply to Re^2: Indices and inserts in Tk::Text by jethro
in thread Indices and inserts in Tk::Text by anna_black

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.