$_[1] is initialized, as you passed a parameter to sub color(), which takes Ev('A') containing the value of the last key stroke.

"how can I accomplish what I want to do in this sub if I cannot use $_?"

Why cannot you? That's you code, and you can always modify it to work ;-) I believe that you expect $_ to contain (part of) the content in your text box. You can always call get() method of the text box to get the content at certain position or a range of positions.

Slightly modified your code to demo a little bit more: (Played with this code, and I do see some text colored blue. Hey, that's mainly your code!)

use Tk; use strict; use warnings; my $mw = new MainWindow(title => "demo"); my $entry = $mw->Scrolled("Text", -scrollbars => 'e', -font => '12')-> +pack(-side => 'bottom', -fill => 'both', -expand => 1); #$entry->bind("<KeyRelease>", [sub {print $_[1];}, Ev('A')]); $entry->bind("<KeyRelease>", [\&color, Ev('A')]); $entry->tagConfigure("blue", -foreground => "blue"); MainLoop; sub color { my $content = $entry->get("1.0", "end"); my $count = 0; my @char = split(//, $content); my @word = split(/ /, $content); foreach my $char (@char) { $count++; } foreach my $word (@word) { if ($word =~ /color/) { my $len = length($word); my $from = "1\.$count"; my $next = $from + $len; $entry->tagAdd("blue", $from, $next); } } }

You still have to do some work to implement your application logic, but I have already given you the right tool. Have fun!


In reply to Re: Re: Re: Tk Scrolled textbox, parse on input? by pg
in thread Tk Scrolled textbox, parse on input? by Elijah

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.